Search code examples
javalocationstanford-nlpopennlp

How to get location from text using OpenNLP?


I am using chunking for tagging the data and get the location from the text initially i try to extract noun phrase from next when we use noun phrase name also mentioned as noun phrase so it can't use.then i moved to location ner of core nlp i try to run the below code

 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    try {

InputStream inputStreamTokenizer = new FileInputStream("D:\project\Relation Extraction in Text Document\Libraray\parsing/en-token.bin"); TokenizerModel tokenModel = new TokenizerModel(inputStreamTokenizer);

  //String paragraph = "Mike and Smith are classmates"; 
  String paragraph = "Tutorialspoint is located in Hyderabad"; 

  //Instantiating the TokenizerME class 
  TokenizerME tokenizer = new TokenizerME(tokenModel); 
  String tokens[] = tokenizer.tokenize(paragraph); 

  //Loading the NER-location moodel 
  InputStream inputStreamNameFinder = new 
     FileInputStream("D:\\project\\Relation Extraction in Text Document\\Libraray\\parsing/en-ner-location.bin");       
  TokenNameFinderModel location = new TokenNameFinderModel(inputStreamNameFinder); 

  //Instantiating the NameFinderME class 
  NameFinderME nameFinder;      
        nameFinder = new NameFinderME(location);

  //Finding the names of a location 
  Span nameSpans[] = nameFinder.find(tokens);        
  //Printing the spans of the locations in the sentence 
 for(Span s: nameSpans)        
     System.out.println(s.toString()+"  "+tokens[s.getStart()]);

I Got an error that "java.lang.UnsupportedOperationException: Not supported yet."

An error symbol at" nameFinder = new NameFinderME(location);" saying that "exmp.TokenNameFinderModel cannot be converted to opennlp.tools.namefind.TokenNameFinderModel" what is reson for it


Solution

  • You have incorrect imports, here is a working version:

    import java.io.FileInputStream;
    import java.io.InputStream;
    
    import opennlp.tools.namefind.NameFinderME;
    import opennlp.tools.namefind.TokenNameFinderModel;
    import opennlp.tools.tokenize.TokenizerME;
    import opennlp.tools.tokenize.TokenizerModel;
    import opennlp.tools.util.Span;
    

    and the output: [4..5) location Hyderabad