How to add new Lookup into DefaultGazetteer programatically

孤街浪徒 提交于 2019-11-28 10:06:32

问题


I want to add new Lookup into loaded DefaultGazetteer programatically.

If I add this string via file, it works perfectly

Any help will be much welcomed. Thanks

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, "@"));

回答1:


This will add a Lookup only:

    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);

This will update the linear definition (.def & .lst files):

    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();

If your gazetteer configuration is consistent (mainly the separator), then

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

will load the updated configuration. You do not necessarily have to combine the second approach with the first one. But because store() and reInit() are very expensive operations compared to Lookup addition, I do not recommend calling it frequently. I would prefer some kind of combination (as you mentioned in the comments) or do the Lookup addition only if you do not care about the .def & .lst files (you may be persisting your lookups already somehow/somewhere).

Removing Lookup(s)

Lookup only:

//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);

Linear definition only:

theList.remove(gn);


来源:https://stackoverflow.com/questions/30569624/how-to-add-new-lookup-into-defaultgazetteer-programatically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!