【问题标题】:How to add new Lookup into DefaultGazetteer programatically如何以编程方式将新查找添加到默认地名词典
【发布时间】:2015-08-14 16:24:50
【问题描述】:

我想以编程方式将新的查找添加到加载的DefaultGazetteer

如果我通过文件添加这个字符串,它可以完美地工作

我们将非常欢迎任何帮助。谢谢

String test="hello@code=555.5@code_asociated_description=World@code1=@code2=@code3=@code4=@code5=@code6=@code7=";
gazetter.add(test, new Lookup("glossary.lst", "test", "test", "en"));
theList.add(new GazetteerNode(test, "@"));

【问题讨论】:

    标签: java gate


    【解决方案1】:

    这将只添加一个查找:

        Lookup l = new Lookup("glossary.lst", "major", "minor", "en", "AnnotType");
        l.features = new HashMap<>();
        l.features.put("someFeatureName", "some value");
        gazetter.add("string to be found", l);
    

    这将更新线性定义(.def & .lst 文件):

        LinearDefinition ld = gazetter.getLinearDefinition();
    
        //add .lst record
        LinearNode ln = new LinearNode("glossary.lst", "minor", "major", "en", "AnnotType");
        ld.add(ln);
    
        //add Lookup record
        Map<String, Object> features = new HashMap<>();
        features.put("someFeatureName", "some value");
        GazetteerNode gn = new GazetteerNode("string to be found", features);
        gn.setSeparator("@");
        GazetteerList theList = ld.getListsByNode().get(ln);
        theList.add(gn);
    
        //save updated files 
        theList.store();
        ld.store();
    
        //optionally re-init the gazetteer to make changes to work 
        gazetter.reInit();
    

    如果你的地名词典配置是一致的(主要是分隔符),那么

    theList.store(); ld.store(); gazetter.reInit();
    

    将加载更新的配置。您不一定必须将第二种方法与第一种方法结合起来。但是因为store()reInit() 相比 Lookup 加法来说是非常昂贵的操作,所以我不建议经常调用它。如果您不关心 .def.lst 文件(您可能会持续查找已经不知何故/某处)。

    删除查找

    仅查找

    //This will remove all Lookups for given string
    gazetteer.remove("string to be found");
    
    //This will remove a specific Lookup only
    //The method is not included in the Gazetteer interface
    ((DefaultGazetteer) gazetter).removeLookup("string to be found", l);
    

    线性定义

    theList.remove(gn);
    

    【讨论】:

    • 谢谢。你真的帮了我。 仅查找正如我所料为我工作。第二个,添加到列表中,但不加载它(我们需要结合动态添加和lst更新)。
    • 如何删除添加的查找?
    • @Valijon - 查看我更新的答案(添加了一些关于需要结合和查找删除的 cmets)。
    • 谢谢。但是,它仍然没有删除查找。有一点奇怪:当我通过第一种方法添加新的查找时,它没有显示在地名词典实例的listsByNode 中(调试模式)。这可能是不删除查找的原因吗?
    • @Valijon - 请参阅我对您的新问题的回答:stackoverflow.com/a/30623100/1857897 第一种方法不会修改线性定义
    猜你喜欢
    • 2015-08-16
    • 2014-03-09
    • 1970-01-01
    • 2018-12-01
    • 1970-01-01
    • 2023-04-08
    • 2014-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多