2

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, "@"));
Valijon
  • 12,667
  • 4
  • 34
  • 67

1 Answers1

3

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);
dedek
  • 7,981
  • 3
  • 38
  • 68
  • Thank you. You really helped me. *Lookup only* worked for me as I expected. The second one, adds into list, but does not load it (we need to combine both dynamic addition and lst update). – Valijon Jun 01 '15 at 15:16
  • How can I remove added Lookup? – Valijon Jun 02 '15 at 10:19
  • @Valijon - see my updated answer (added some comments about _need to combine both_ and Lookup remove). – dedek Jun 03 '15 at 07:30
  • Thank you. But, it still does not remove a Lookup. There is something strange: when I add a new Lookup via 1st method, it does not shown in `listsByNode` of gazetter instance (debug mode). Can it be the reason to not delete the Lookup? – Valijon Jun 03 '15 at 08:13
  • @Valijon - see my answer to your new question: http://stackoverflow.com/a/30623100/1857897 The **1st method will not modify the linear definition**. – dedek Jun 03 '15 at 14:40