Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
571 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...