Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
573 views
in Technique[技术] by (71.8m points)

java - How to add new Lookup into DefaultGazetteer programatically

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, "@"));
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...