Example usage for edu.stanford.nlp.ling CoreLabel get

List of usage examples for edu.stanford.nlp.ling CoreLabel get

Introduction

In this page you can find the example usage for edu.stanford.nlp.ling CoreLabel get.

Prototype

@Override
@SuppressWarnings("unchecked")
public <VALUE> VALUE get(Class<? extends Key<VALUE>> key) 

Source Link

Usage

From source file:DateRecognitionFunction.java

License:Apache License

public static Set<String> ner(String text) {

    Annotation document = new Annotation(text);

    // run all Annotators on this text
    pipeline.annotate(document);/*www .j  av  a 2 s . co m*/

    // these are all the sentences in this document
    // a CoreMap is essentially a Map that uses class objects as keys and has values with custom types
    List<CoreMap> sentences = document.get(CoreAnnotations.SentencesAnnotation.class);

    Set<String> entities = new HashSet<>();

    for (CoreMap sentence : sentences) {
        // traversing the words in the current sentence
        // a CoreLabel is a CoreMap with additional token-specific methods
        for (CoreLabel token : sentence.get(CoreAnnotations.TokensAnnotation.class)) {
            // this is the text of the token
            String word = token.get(CoreAnnotations.TextAnnotation.class);

            // this is the NER label of the token
            String ne = token.get(CoreAnnotations.NamedEntityTagAnnotation.class);

            if (ne.equals("DATE")) {
                entities.add(word);
            }
        }
    }

    return entities;

}

From source file:PersonRecognitionFunction.java

License:Apache License

public static Set<String> ner(String text) {

    Annotation document = new Annotation(text);

    // run all Annotators on this text
    pipeline.annotate(document);//from  w ww  .  ja va  2 s.co  m

    // these are all the sentences in this document
    // a CoreMap is essentially a Map that uses class objects as keys and has values with custom types
    List<CoreMap> sentences = document.get(CoreAnnotations.SentencesAnnotation.class);

    Set<String> locations = new HashSet<>();

    for (CoreMap sentence : sentences) {
        // traversing the words in the current sentence
        // a CoreLabel is a CoreMap with additional token-specific methods
        for (CoreLabel token : sentence.get(CoreAnnotations.TokensAnnotation.class)) {
            // this is the text of the token
            String word = token.get(CoreAnnotations.TextAnnotation.class);

            // this is the NER label of the token
            String ne = token.get(CoreAnnotations.NamedEntityTagAnnotation.class);

            if (ne.equals("PERSON")) {
                locations.add(word);
            }
        }
    }

    return locations;

}

From source file:NERServer.java

License:Open Source License

public static void main(String[] args) throws Exception {
    if (args.length != 1) {
        System.err.println("usage: java NERServer modelpath");
        System.exit(1);/*from   w w  w  .j  a v  a  2 s  .  c  om*/
    }

    CRFClassifier crf = CRFClassifier.getClassifier(args[0]);
    BufferedReader input = new BufferedReader(new InputStreamReader(System.in), 1);

    for (;;) {
        String ln = input.readLine();
        if (ln == null) {
            break;
        }

        List<List<CoreLabel>> out = crf.classify(ln);
        for (List<CoreLabel> sentence : out) {
            for (CoreLabel word : sentence) {
                String label = word.get(CoreAnnotations.AnswerAnnotation.class);
                System.out.print(word.word() + '/' + label + ' ');
            }
        }
        System.out.print('\n');
    }
}

From source file:Treeparse.java

public static void main(String[] args) {
    // TODO code application logic here

    Properties props = new Properties();
    props.setProperty("annotators", "tokenize, ssplit, pos, lemma,parse");
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
    System.out.println("Enter the text:");
    Scanner sc = new Scanner(System.in);
    text = sc.nextLine();/* w  w  w.  j a  v a  2s  .  c  o m*/

    //while(text!="exit")
    //{
    Annotation document = new Annotation(text);
    pipeline.annotate(document);

    List<CoreMap> sentences = document.get(SentencesAnnotation.class);

    for (CoreMap sentence : sentences) {

        token_length = sentence.get(TokensAnnotation.class).size();
        arr1 = new String[POSTagger.token_length];
        arr2 = new String[POSTagger.token_length];
        int i = 0, j = 0;

        // System.out.println("Size"+token_length);
        for (CoreLabel token : sentence.get(TokensAnnotation.class)) {

            String word = token.get(TextAnnotation.class);

            String pos = token.get(PartOfSpeechAnnotation.class);
            //    String ner = token.get(NamedEntityTagAnnotation.class);

        }
        Tree tree = sentence.get(TreeAnnotation.class);
        // System.out.println(tree);
        List<Tree> x = GetNounPhrases(tree);
        System.out.println(x);

        // Print words and Pos Tags
        /*for (Tree leaf : leaves) { 
            Tree parent = leaf.parent(tree);
            System.out.print(leaf.label().value() + "-" + parent.label().value() + " ");
        }*/
    }
    //System.out.println("Enter the text:");
    //text=sc.nextLine();  

}

From source file:unCompressedIndex.java

public static String lemmatize(String documentText) {
    List<String> lemmas = new LinkedList<String>();
    // Create an empty Annotation just with the given text
    Annotation document = new Annotation(documentText);
    // run all Annotators on this text
    pipeline.annotate(document);//from   w  w w  .  j  a va2  s.c  o m
    // Iterate over all of the sentences found
    String temp = "";
    List<CoreMap> sentences = document.get(SentencesAnnotation.class);
    for (CoreMap sentence : sentences) {
        // Iterate over all tokens in a sentence
        for (CoreLabel token : sentence.get(TokensAnnotation.class)) {
            temp = token.get(LemmaAnnotation.class);
            lemmas.add(temp);

        }
    }
    return lemmas.get(0).toString();
    //System.out.println("lemmas:"+lemmas.get(0));
}

From source file:BuildBinarizedDataset.java

public static boolean setSpanLabel(Tree tree, Pair<Integer, Integer> span, String value) {
    if (!(tree.label() instanceof CoreLabel)) {
        throw new AssertionError("Expected CoreLabels");
    }//from   ww w. j  ava  2 s .co  m
    CoreLabel label = (CoreLabel) tree.label();
    if (label.get(CoreAnnotations.BeginIndexAnnotation.class).equals(span.first)
            && label.get(CoreAnnotations.EndIndexAnnotation.class).equals(span.second)) {
        label.setValue(value);
        return true;
    }
    if (label.get(CoreAnnotations.BeginIndexAnnotation.class) > span.first
            && label.get(CoreAnnotations.EndIndexAnnotation.class) < span.second) {
        return false;
    }
    for (Tree child : tree.children()) {
        if (setSpanLabel(child, span, value)) {
            return true;
        }
    }
    return false;
}

From source file:LocationRecognitionFunction.java

License:Apache License

public static Set<String> ner(String text) {

    Annotation document = new Annotation(text);

    // run all Annotators on this text
    pipeline.annotate(document);/*from ww w .  ja v  a  2s.  com*/

    // these are all the sentences in this document
    // a CoreMap is essentially a Map that uses class objects as keys and has values with custom types
    List<CoreMap> sentences = document.get(CoreAnnotations.SentencesAnnotation.class);

    Set<String> locations = new HashSet<>();

    for (CoreMap sentence : sentences) {
        // traversing the words in the current sentence
        // a CoreLabel is a CoreMap with additional token-specific methods
        for (CoreLabel token : sentence.get(CoreAnnotations.TokensAnnotation.class)) {
            // this is the text of the token
            String word = token.get(CoreAnnotations.TextAnnotation.class);

            // this is the NER label of the token
            String ne = token.get(CoreAnnotations.NamedEntityTagAnnotation.class);

            if (ne.equals("LOCATION")) {
                locations.add(word);
            }
        }
    }

    return locations;

}

From source file:agk.chatbot.nlp.StanfordAnnotator.java

License:Open Source License

@Override
public NLPText processString(String content) {
    NLPText txt = new NLPText(content);
    Annotation doc = new Annotation(content);
    pipeline.annotate(doc);//from  w  ww  . j a  v a  2  s.  c  o  m

    for (CoreMap sentence : doc.get(SentencesAnnotation.class)) {
        String text = sentence.get(TextAnnotation.class);
        NLPSentence s = new NLPSentence(text);

        for (CoreLabel token : sentence.get(TokensAnnotation.class)) {
            String word = token.get(TextAnnotation.class);
            String pos = token.get(PartOfSpeechAnnotation.class);
            String lemma = token.get(LemmaAnnotation.class);
            String ner = token.get(NamedEntityTagAnnotation.class);

            NLPToken t = new NLPToken(word);
            t.setNerTag(ner);
            t.setPostag(pos);
            t.setStem(lemma);
            t.setMainName(!ner.equals("O"));
            t.setStopWord(NLPText.isStopWord(word));
            s.addToken(t);
        }
        txt.addSentence(s);
    }

    return txt;
}

From source file:analytics.weka.EnglishTextAnnotations.java

License:Apache License

public static void main(String[] args) {
    // creates a StanfordCoreNLP object, with POS tagging, lemmatization,
    // NER, parsing, and coreference resolution
    Properties props = new Properties();
    props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner");//, parse, sentiment
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props);

    // read some text in the text variable
    String text = "jackie brown ( miramax - 1997 ) starring pam grier , samuel l . jackson , robert forster , bridget fonda , michael keaton , robert de niro , michael bowen , chris tucker screenplay by quentin tarantino , based on the novel rum punch by elmore leonard produced by lawrence bender directed by quentin tarantino running time : 155 minutes note : some may consider portions of the following text to be spoilers . be forewarned . ------------------------------------------------------------- during the three years since the release of the groundbreaking success pulp fiction , the cinematic output from its creator , quentin tarantino , has been surprisingly low . oh , he\'s been busy -- doing the talk show circuit , taking small roles in various films , overseeing the production of his screenplay from dusk till dawn , making cameo appearances on television shows , providing a vignette for the ill-fated anthology four rooms -- everything , it seems , except direct another feature-length film . it\'s been the long intermission between projects as well as the dizzying peak which pulp fiction reached which has made mr . tarantino\'s new feature film , jackie brown , one of the most anticipated films of the year , and his third feature film cements his reputation as the single most important new american filmmaker to emerge from the 1990s . things aren\'t going well for jackie brown ( pam grier ) . she\'s 44 years old , stuck at a dead-end job ( \" $16 , 000 a year , plus retirement benefits that aren\'t worth a damn \" ) as a flight attendant for the worst airline in north america -- and she\'s just been caught at the airport by atf agent ray nicolette ( portrayed with terrific childlike enthusiasm by michael keaton ) and police officer mark dargus ( michael bowen ) smuggling $50 000 from mexico for gun-runner ordell robbie ( samuel l . jackson ) , who has her bailed out by unassuming bail bondsman max cherry ( robert forster ) . the loquacious ordell , based out of a hermosa beach house where his horny , bong-hitting surfer girl melanie ( bridget fonda ) and agreeable crony louis gara ( robert de niro ) hang out , operates under the policy that the best rat is a dead rat , and he\'s soon out to silence jackie brown . meanwhile , the authorities\' target is ordell , and they want jackie to help them by arranging a sting to the tune of a half-million dollars . only through a series of clever twists , turns , and double-crosses will jackie be able to gain the upper hand on both of her nemeses . although jackie brown marks mr . tarantino\'s first produced screenplay adaptation ( based on the elmore leonard novel \" rum punch \" ) , there\'s no mistaking his distinctive fingerprints all over this film . while he\'s adhered closely to the source material in a narrative sense , the setting has been relocated to los angeles and the lead character\'s now black . in terms of ambiance , the film harkens back to the 1970s , from the wall-to-wall funk and soul music drowning the soundtrack to the nondescript look of the sets -- even the opening title credit sequence has the echo of vintage 1970s productions . the opening sequence featuring ms . grier wordlessly striding through the lax , funky music blaring away on the speakers , is emblematic of films of that era . the timeframe for the film is in fact 1995 , but the atmosphere of jackie brown is decidedly retro . of course , nothing in the film screams 1970s more than the casting of pam grier and robert forster as the two leads , and although the caper intrigue is fun to watch as the plot twists , backstabbing , and deceptions deliciously unfold , the strength of jackie brown is the quiet , understated relationship developed between jackie and max ; when they kiss , it\'s perhaps the most tender scene of the year . tenderness ? in a quentin tarantino film ? sure , there\'ve been moments of sweetness in his prior films -- the affectionate exchanges between the bruce willis and maria de madeiros characters in pulp fiction and the unflagging dedication shared by the characters of tim roth and amanda plummer , or even in reservoir dogs , where a deep , unspoken bond develops between the harvey keitel and tim roth characters -- but for the most part , mr . tarantino\'s films are typified by manic energy , unexpected outbursts of violence , and clever , often wordy , banter . these staples of his work are all present in jackie brown , but what\'s new here is a different facet of his storytelling -- a willingness to imbue the film with a poignant emotional undercurrent , and a patience to draw out several scenes with great deliberation . this effective demonstration of range prohibits the pigeonholing of mr . tarantino as simply a helmer of slick , hip crime dramas with fast-talking lowlifes , and heralds him as a bonafide multifaceted talent ; he\'s the real deal . this new aspect of mr . tarantino\'s storytelling is probably best embodied in a single character -- that of the world-weary , sensitive , and exceedingly-professional max cherry , whose unspoken attraction to jackie is touching . mr . forster\'s nuanced , understated performance is the best in the film ; he creates an amiable character of such poignancy that when he gazes at jackie , we smile along with him . much press has been given about the casting of blaxploitation-era icon pam grier in the lead , with the wags buzzing that mr . tarantino may do for her what his pulp fiction did to bolster john travolta\'s then-sagging career . as it turns out , ms . grier is solid in the film\'s title role , although nothing here forces her to test her range . i do have to take exception to the claim that this film marks her career resurrection , though -- she\'s been working steadily over the years , often in direct-to-video action flicks , but also in such recent theatrical releases as tim burton\'s mars attacks ! and larry cohen\'s original gangstas ( where she first teamed up with mr . forster . ) of course , it\'s true that her role here was a godsend -- a meaty a part as this is rarity for * any * actress , let alone one of her age and current status in the industry . while jackie brown may disappoint those looking for another pulp fiction clone , it marks tremendous growth of mr . tarantino as a director whose horizons are rapidly expanding , and whose characterizations have never been better . and while the film\'s narrative doesn\'t really warrant a running time of 155 minutes , it\'s filled with such sumptuous riches , ranging from the brashness of the vivid soundtrack to entertaining , inconsequential conversations between the characters , that there wasn\'t an unengaging moment . with an impressive trio of feature films under his belt , it\'ll be interesting to see what he tries next . \r\n";

    // create an empty Annotation just with the given text
    Annotation document = new Annotation(text);

    // run all Annotators on this text
    pipeline.annotate(document);/*ww w .j a  va  2 s  .co m*/

    System.out.println(text);
    System.out.println(document.get(TextAnnotation.class));

    // these are all the sentences in this document
    // a CoreMap is essentially a Map that uses class objects as keys and
    // has values with custom types
    List<CoreMap> sentences = document.get(SentencesAnnotation.class);

    StringBuilder lemmas = new StringBuilder();
    for (CoreMap sentence : sentences) {
        // traversing the words in the current sentence
        // a CoreLabel is a CoreMap with additional token-specific methods
        //System.out.println("sentence: "+sentence.get(SentenceBeginAnnotation.class));
        for (CoreLabel token : sentence.get(TokensAnnotation.class)) {

            String word = token.get(TextAnnotation.class);
            //System.out.println("this is the text of the token: "+word);
            // 
            String pos = token.get(LemmaAnnotation.class);

            // 
            String ne = token.get(NamedEntityTagAnnotation.class);
            //System.out.println("this is the NER label of the token: "+ne);
            if ("O".equals(ne))
                lemmas.append(pos).append(" ");
        }

        //System.out.println("sentence: "+sentence.get(SentenceEndAnnotation.class));
    }

    System.out.println("this is the lemma tag of the token: " + lemmas);
}

From source file:Anaphora_Resolution.Coref.java

/**
 * @param args the command line arguments
 *///w  w w . j a  va 2s.co  m
public static void main(String[] args) throws IOException, ClassNotFoundException {
    // creates a StanfordCoreNLP object, with POS tagging, lemmatization, NER, parsing, and coreference resolution 
    Properties props = new Properties();
    props.put("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
    props.put("pos.model",
            "H:\\nlp jar files\\stanford-postagger-2014-08-27\\stanford-postagger-2014-08-27\\models\\english-left3words-distsim.tagger");
    //props.put("dcoref.big.gender.number", "edu/stanford/nlp/models/dcoref/gender.data.gz");
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props);

    // read some text in the text variable
    String text = "Mary has a little lamb. She is very cute."; // Add your text here!

    // create an empty Annotation just with the given text
    Annotation document = new Annotation(text);

    // run all Annotators on this text
    pipeline.annotate(document);

    // these are all the sentences in this document
    // a CoreMap is essentially a Map that uses class objects as keys and has values with custom types
    List<CoreMap> sentences = document.get(SentencesAnnotation.class);

    for (CoreMap sentence : sentences) {
        // traversing the words in the current sentence
        // a CoreLabel is a CoreMap with additional token-specific methods
        for (CoreLabel token : sentence.get(TokensAnnotation.class)) {
            // this is the text of the token
            String word = token.get(TextAnnotation.class);
            // this is the POS tag of the token
            String pos = token.get(PartOfSpeechAnnotation.class);
            // this is the NER label of the token
            String ne = token.get(NamedEntityTagAnnotation.class);
        }

        // this is the parse tree of the current sentence
        Tree tree = sentence.get(TreeAnnotation.class);
        System.out.println(tree);

        // this is the Stanford dependency graph of the current sentence
        SemanticGraph dependencies = sentence.get(CollapsedCCProcessedDependenciesAnnotation.class);
    }

    // This is the coreference link graph
    // Each chain stores a set of mentions that link to each other,
    // along with a method for getting the most representative mention
    // Both sentence and token offsets start at 1!
    Map<Integer, CorefChain> graph = document.get(CorefChainAnnotation.class);
    System.out.println(graph);
}