Search code examples
parsingnlpopennlp

How to extract entity using stanford parser?


I am using OpenNLP but it is not giving me exact name,location,organization entity. How to extract entity using stanford parser?


Solution

  • My code might help you.

    Here is my Code

    public class stanfrdIntro {
    
        public static void main(String[] args) throws IOException, SAXException,
        TikaException {
    
            String serializedClassifier = "classifiers/english.all.3class.distsim.crf.ser.gz";
    
    
            AbstractSequenceClassifier<CoreLabel> classifier = CRFClassifier
                    .getClassifierNoExceptions(serializedClassifier);
    
            if (args.length > 1) {
             String fileContents = IOUtils.slurpFile(args[1]);
            List<List<CoreLabel>> out = classifier.classify(fileContents);
            for (List<CoreLabel> sentence : out) {
            for (CoreLabel word : sentence) {
            System.out.print(word.word() + '/' +
             word.get(CoreAnnotations.AnswerAnnotation.class) + ' ');
             }
             System.out.println();
            }
     out = classifier.classifyFile(args[1]);
            for (List<CoreLabel> sentence : out) {
            for (CoreLabel word : sentence) {
            System.out.print(word.word() + '/' +
            word.get(CoreAnnotations.AnswerAnnotation.class) + ' ');
            }
             System.out.println();
             }
    
             } else {
            String s1 = "Good afternoon Rahul Kulhari, how are you today?";
             String s2 =
             "I go to school at Stanford University, which is located in California.";
            stanfrdIntro si = new stanfrdIntro();
            String s1 = si.contentEx();
            s1 = s1.replaceAll("\\s+", " ");
            System.out.println(s1);
            String  t=classifier.classifyWithInlineXML(s1);
            System.out.println(Arrays.toString(getTagValues(t).toArray()));
    
            XPath xpath = XPathFactory.newInstance().newXPath();
            XPathExpression expr = xpath.compile("//PERSON/text()");
            Object value = expr.evaluate(doc, XPathConstants.STRING);
            System.out.println(t);
            System.out.println(classifier.classifyToString(s1));
            Set<String> s=classifier.;
            for(String s13: s)
            {
                System.out.println(s13);
            }
            System.out.println(classifier.classifyWithInlineXML(s1));
            System.out.println(classifier.classifyToString(s1, "xml", true));
             int i=0;
             for (List<CoreLabel> lcl : classifier.classify(s2)) {
            for (CoreLabel cl : lcl) {
             System.out.println(i++ + ":");
             System.out.println(cl);
     }
     }
     }
        }
    
    
    }