當前位置: 首頁>>代碼示例>>Java>>正文


Java StanfordCoreNLP.annotate方法代碼示例

本文整理匯總了Java中edu.stanford.nlp.pipeline.StanfordCoreNLP.annotate方法的典型用法代碼示例。如果您正苦於以下問題:Java StanfordCoreNLP.annotate方法的具體用法?Java StanfordCoreNLP.annotate怎麽用?Java StanfordCoreNLP.annotate使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在edu.stanford.nlp.pipeline.StanfordCoreNLP的用法示例。


在下文中一共展示了StanfordCoreNLP.annotate方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: extractSentences

import edu.stanford.nlp.pipeline.StanfordCoreNLP; //導入方法依賴的package包/類
/**
 * Split a document into array of sentences
 * 
 * @param text
 * @return
 * @throws Exception
 */
public static String[] extractSentences(String text) throws Exception {
	Properties props = new Properties();
	props.put("annotators", "tokenize, ssplit");
	StanfordCoreNLP pipeline = new StanfordCoreNLP();

	Annotation document = new Annotation(text);
	pipeline.annotate(document);

	List<CoreMap> sentences = document.get(SentencesAnnotation.class);
	String[] sentenceList = new String[sentences.size()];

	for (int i = 0; i < sentenceList.length; i++) {
		CoreMap sentence = sentences.get(i);
		sentenceList[i] = sentence.toString();
	}

	return sentenceList;
}
 
開發者ID:NLPReViz,項目名稱:emr-nlp-server,代碼行數:26,代碼來源:TextUtil.java

示例2: annotate

import edu.stanford.nlp.pipeline.StanfordCoreNLP; //導入方法依賴的package包/類
protected void annotate(StanfordCoreNLP pipeline, Annotation ann) {
	if (ann.get(CoreAnnotations.SentencesAnnotation.class) == null) {
		pipeline.annotate(ann);
	}
	else {
		if (ann.get(CoreAnnotations.SentencesAnnotation.class).size() == 1) {
			CoreMap sentence = ann.get(CoreAnnotations.SentencesAnnotation.class).get(0);

			for (CoreLabel token : sentence.get(CoreAnnotations.TokensAnnotation.class)) {
				token.remove(NaturalLogicAnnotations.OperatorAnnotation.class);
				token.remove(NaturalLogicAnnotations.PolarityAnnotation.class);
			}

			sentence.remove(NaturalLogicAnnotations.RelationTriplesAnnotation.class);
			sentence.remove(NaturalLogicAnnotations.EntailedSentencesAnnotation.class);
			sentence.remove(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class);
			sentence.remove(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation.class);
			sentence.remove(SemanticGraphCoreAnnotations.EnhancedPlusPlusDependenciesAnnotation.class);

			pipeline.annotate(ann);
		}
	}
}
 
開發者ID:igr,項目名稱:parlo,代碼行數:24,代碼來源:CoreNLPEngine.java

示例3: ExtractPosTagsFile

import edu.stanford.nlp.pipeline.StanfordCoreNLP; //導入方法依賴的package包/類
@Override
public List<ExtractPosTag> ExtractPosTagsFile(File filePath) throws Exception {
       List<String> lstData=ExtractData(filePath);
       List<ExtractPosTag> lstTaggedSentences = new ArrayList<>();
       Properties props = new Properties();
       props.setProperty("annotators", "tokenize,ssplit,pos");
       StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
       for(String str:lstData)
       {
       Annotation annotation = new Annotation(str);
       pipeline.annotate(annotation);
       List<CoreMap> senten=annotation.get(CoreAnnotations.SentencesAnnotation.class);
       for(CoreMap map:senten)
       {
           map.get(TokensAnnotation.class).stream().forEach((tok) -> {
               String PosTagg=tok.get(PartOfSpeechAnnotation.class);
               lstTaggedSentences.add(new ExtractPosTag(tok.originalText(),PosTagg));
           });
       }
     } 
    return lstTaggedSentences;
}
 
開發者ID:unsw-cse-soc,項目名稱:Data-curation-API,代碼行數:23,代碼來源:ExtractPosTagData.java

示例4: ExtractPosTags

import edu.stanford.nlp.pipeline.StanfordCoreNLP; //導入方法依賴的package包/類
@Override
public List<ExtractPosTag> ExtractPosTags(List<String> inputData) 
{
	List<ExtractPosTag> lstTaggedSentences = new ArrayList<>();		
       Properties props = new Properties();
       props.setProperty("annotators", "tokenize,ssplit,pos");
       StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
       for(String str:inputData)
       {
       Annotation annotation = new Annotation(str);
       pipeline.annotate(annotation);
       List<CoreMap> senten=annotation.get(CoreAnnotations.SentencesAnnotation.class);
       for(CoreMap map:senten)
       {
           map.get(TokensAnnotation.class).stream().forEach((tok) -> {
               String getPosTag=tok.get(PartOfSpeechAnnotation.class);
               lstTaggedSentences.add(new ExtractPosTag(tok.originalText(),getPosTag));
           });
       }
     } 
    return lstTaggedSentences;
}
 
開發者ID:unsw-cse-soc,項目名稱:Data-curation-API,代碼行數:23,代碼來源:ExtractPosTagData.java

示例5: ExtractPosTagsSentence

import edu.stanford.nlp.pipeline.StanfordCoreNLP; //導入方法依賴的package包/類
@Override
public List<ExtractPosTag> ExtractPosTagsSentence(String sentence) 
{
       List<ExtractPosTag> lstTaggedSentences = new ArrayList<>();
       Properties props = new Properties();
       props.setProperty("annotators", "tokenize,ssplit,pos");
       StanfordCoreNLP pipeline = new StanfordCoreNLP(props);

       Annotation annotation = new Annotation(sentence);
       pipeline.annotate(annotation);
       List<CoreMap> senten=annotation.get(CoreAnnotations.SentencesAnnotation.class);
       for(CoreMap map:senten)
       {
           map.get(TokensAnnotation.class).stream().forEach((tok) -> {
               String getPosTag=tok.get(PartOfSpeechAnnotation.class);
               lstTaggedSentences.add(new ExtractPosTag(tok.originalText(),getPosTag));
           });
       }        
       return lstTaggedSentences;
}
 
開發者ID:unsw-cse-soc,項目名稱:Data-curation-API,代碼行數:21,代碼來源:ExtractPosTagData.java

示例6: main

import edu.stanford.nlp.pipeline.StanfordCoreNLP; //導入方法依賴的package包/類
public static void main(String[] args){
    try{
      Properties props = StringUtils.argsToProperties(args);
//      props.setProperty("annotators", "tokenize,ssplit,lemma,pos,parse,ner");
      StanfordCoreNLP pipeline = new StanfordCoreNLP();
      String sentence = "John Gerspach was named Chief Financial Officer of Citi in July 2009.";
      Annotation doc = new Annotation(sentence);
      pipeline.annotate(doc);
      RelationExtractorAnnotator r = new RelationExtractorAnnotator(props);
      r.annotate(doc);

      for(CoreMap s: doc.get(CoreAnnotations.SentencesAnnotation.class)){
        System.out.println("For sentence " + s.get(CoreAnnotations.TextAnnotation.class));
        List<RelationMention> rls  = s.get(RelationMentionsAnnotation.class);
        for(RelationMention rl: rls){
          System.out.println(rl.toString());
        }
      }
    }catch(Exception e){
      e.printStackTrace();
    }
  }
 
開發者ID:intel-analytics,項目名稱:InformationExtraction,代碼行數:23,代碼來源:JavaReExTest.java

示例7: main

import edu.stanford.nlp.pipeline.StanfordCoreNLP; //導入方法依賴的package包/類
public static void main(String[] s) {
    Properties props = new Properties();
    props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props);

    // read some text in the text variable
    String text = "\"But I do not want to go among mad people,\" Alice remarked.\n" +
            "\"Oh, you can not help that,\" said the Cat: \"we are all mad here. I am mad. You are mad.\"\n" +
            "\"How do you know I am mad?\" said Alice.\n" +
            "\"You must be,\" said the Cat, \"or you would not have come here.\" This is awful, bad, disgusting";

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

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

    List<CoreMap> sentences = document.get(CoreAnnotations.SentencesAnnotation.class);
    for (CoreMap sentence : sentences) {
        String sentiment = sentence.get(SentimentCoreAnnotations.SentimentClass.class);
        System.out.println(sentiment + "\t" + sentence);
    }
}
 
開發者ID:Vedenin,項目名稱:java_in_examples,代碼行數:24,代碼來源:StanfordCoreNLPTest.java

示例8: main

import edu.stanford.nlp.pipeline.StanfordCoreNLP; //導入方法依賴的package包/類
public static void main(String[] args) throws InterruptedException {
    Properties props = new Properties();
    props.setProperty("annotators", "tokenize, ssplit, pos, ita_morpho, ita_lemma");
    props.setProperty("customAnnotatorClass.ita_lemma", "eu.fbk.dh.digimorph.annotator.DigiLemmaAnnotator");
    props.setProperty("customAnnotatorClass.ita_morpho", "eu.fbk.dh.digimorph.annotator.DigiMorphAnnotator");

    props.setProperty("tokenize.language", "Spanish");
    props.setProperty("ssplit.newlineIsSentenceBreak", "always");

    props.setProperty("pos.model", "/Users/alessio/Documents/Resources/ita-models/italian5.tagger");

    props.setProperty("ita_morpho.model", "/Users/alessio/Documents/Resources/ita-models/italian.db");

    StanfordCoreNLP pipeline = new StanfordCoreNLP(props);

    LOGGER.info("Annotating");

    for (int i = 0; i < 50; i++) {
        Annotation annotation = new Annotation("Questa è una prova.");
        pipeline.annotate(annotation);
    }

    LOGGER.info("Finishing");

    Thread.sleep(60000);
}
 
開發者ID:dkmfbk,項目名稱:pikes,代碼行數:27,代碼來源:MorphTest.java

示例9: tokenizeFromFile

import edu.stanford.nlp.pipeline.StanfordCoreNLP; //導入方法依賴的package包/類
/**
 * Reads stdin through a buffer and runs only the tokenize annotator to the
 * StanfordCoreNLP pipeline. Tokens are printed to stdout one per line.
 * 
 * @param filePath
 *            The file to run the tagger on
 */
private static void tokenizeFromFile(String filePath) {
	Properties properties = new Properties();
	properties.setProperty("annotators", "tokenize");
	StanfordCoreNLP pipeline = new StanfordCoreNLP(properties);
	PrintWriter out = new PrintWriter(System.out);
	File dataFile = new File(filePath);

	Annotation annotation = null;
	try {
		annotation = new Annotation(IOUtils.slurpFile(dataFile));
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	pipeline.annotate(annotation);
	
	for (CoreMap token : annotation
			.get(CoreAnnotations.TokensAnnotation.class)) {
		ArrayCoreMap aToken = (ArrayCoreMap) token;
		out.println(aToken.toShortString(' ', "Text"));
		out.flush();
	}
}
 
開發者ID:Pro-Nouns,項目名稱:StanfordCoreNLPUtility,代碼行數:31,代碼來源:StanfordCoreNLPUtility.java

示例10: en

import edu.stanford.nlp.pipeline.StanfordCoreNLP; //導入方法依賴的package包/類
public static void en(final String[] a) {

    /*
     * for (Entity e : new NERStanford().retrieve(FoxConst.EXAMPLE_1)) NERStanford.LOG.info(e);
     */

    final Properties props = new Properties();
    props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref, relation");
    final StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
    final Annotation ann =
        new Annotation("Stanford University is located in California. It is a great university.");
    pipeline.annotate(ann);

    for (final CoreMap sentence : ann.get(SentencesAnnotation.class)) {
      for (final CoreLabel token : sentence.get(TokensAnnotation.class)) {
        System.out.println(token.get(NamedEntityTagAnnotation.class));
        System.out.println(token.get(CoreAnnotations.AnswerAnnotation.class));
        /*
         * Tree tree = sentence.get(TreeAnnotation.class); System.out.println(tree);
         * System.out.println(tree.score());
         */
      }
    }

  }
 
開發者ID:dice-group,項目名稱:FOX,代碼行數:26,代碼來源:StanfordENOldVersion.java

示例11: getLemmaCoreNLP

import edu.stanford.nlp.pipeline.StanfordCoreNLP; //導入方法依賴的package包/類
static String getLemmaCoreNLP (StanfordCoreNLP pipeline, String form) {
    String tokenLemma =form;
    //System.out.println("form = " + form);
    Annotation tokenAnnotation = new Annotation(form);
    if (tokenAnnotation == null) {
        //System.out.println("form = " + form);
    } else {
        try {
            pipeline.annotate(tokenAnnotation);  // necessary for the LemmaAnnotation to be set.
            List<CoreMap> list = tokenAnnotation.get(CoreAnnotations.SentencesAnnotation.class);
            tokenLemma = list
                    .get(0).get(CoreAnnotations.TokensAnnotation.class)
                    .get(0).get(CoreAnnotations.LemmaAnnotation.class);
        } catch (Exception e) {
            //e.printStackTrace();
            //System.out.println("form = " + form);
        }

    }
    return tokenLemma;
}
 
開發者ID:cltl,項目名稱:EventCoreference,代碼行數:22,代碼來源:Util.java

示例12: parse

import edu.stanford.nlp.pipeline.StanfordCoreNLP; //導入方法依賴的package包/類
/**
 * Given a CoreNLP pipeline and an input sentence, generate dependency parse for the sentence and return
 * the SemanticGraph object as a result
 * @param pipeline - CoreNLP pipeline
 * @param snt - input sentence
 * @return dependency parse in SemanticGraph object
 */
public static SemanticGraph parse(StanfordCoreNLP pipeline, String snt) {
    Annotation document = new Annotation(snt);
    pipeline.annotate(document);
    
    //A CoreMap is a sentence with annotations
    List<CoreMap> sentences = document.get(SentencesAnnotation.class);
    SemanticGraph semanticGraph = null;
    for(CoreMap sentence: sentences) {
        semanticGraph = sentence.get(BasicDependenciesAnnotation.class);
    }
    
    return semanticGraphUniversalEnglishToEnglish(semanticGraph);
}
 
開發者ID:gkiril,項目名稱:minie,代碼行數:21,代碼來源:CoreNLPUtils.java

示例13: main

import edu.stanford.nlp.pipeline.StanfordCoreNLP; //導入方法依賴的package包/類
public static void main (String[] args) {
    // Create the Stanford CoreNLP pipeline
    Properties props = PropertiesUtils.asProperties("annotators", "tokenize,ssplit,pos,lemma,depparse,natlog,openie",
            "parse.model", "edu/stanford/nlp/models/parser/nndep/english_SD.gz",
            "depparse.model", "edu/stanford/nlp/models/parser/nndep/english_SD.gz");
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
    // Annotate an example document.
    String text = "I went into my bedroom and flipped the light switch. Oh, I see that the ceiling lamp is not turning on." +
            " It must be that the light bulb needs replacement. I go through my closet and find a new light bulb that will fit" +
            " this lamp and place it in my pocket. I also get my stepladder and place it under the lamp. I make sure the light" +
            " switch is in the off position. I climb up the ladder and unscrew the old light bulb. I place the old bulb in my " +
            "pocket and take out the new one. I then screw in the new bulb. I climb down the stepladder and place it back into " +
            "the closet. I then throw out the old bulb into the recycling bin. I go back to my bedroom and turn on the light switch." +
            " I am happy to see that there is again light in my room.";
    Annotation doc = new Annotation(text);
    pipeline.annotate(doc);
    // Loop over sentences in the document
    int sentNo = 0;
    for (CoreMap sentence : doc.get(CoreAnnotations.SentencesAnnotation.class)) {
        System.out.println("Sentence #" + ++sentNo + ": " + sentence.get(CoreAnnotations.TextAnnotation.class));
        // Get the OpenIE triples for the sentence
        Collection<RelationTriple> triples = sentence.get(NaturalLogicAnnotations.RelationTriplesAnnotation.class);
        // Print the triples
        for (RelationTriple triple : triples) {
            System.out.println(triple.confidence + "\t" + triple.subjectLemmaGloss() + "\t" + triple.relationLemmaGloss() + "\t" + triple.objectLemmaGloss());
        }
        System.out.println("\n");
    }

}
 
開發者ID:IsaacChanghau,項目名稱:Word2VecfJava,代碼行數:31,代碼來源:StanfordOpenIEExample.java

示例14: main

import edu.stanford.nlp.pipeline.StanfordCoreNLP; //導入方法依賴的package包/類
public static void main (String[] args) {
    String string = "I went into my bedroom and flipped the light switch. Oh, I see that the ceiling lamp is not turning on." +
            " It must be that the light bulb needs replacement. I go through my closet and find a new light bulb that will fit" +
            " this lamp and place it in my pocket. I also get my stepladder and place it under the lamp. I make sure the light" +
            " switch is in the off position. I climb up the ladder and unscrew the old light bulb. I place the old bulb in my " +
            "pocket and take out the new one. I then screw in the new bulb. I climb down the stepladder and place it back into " +
            "the closet. I then throw out the old bulb into the recycling bin. I go back to my bedroom and turn on the light switch." +
            " I am happy to see that there is again light in my room.";
    Properties prop = new Properties();
    prop.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
    //prop.setProperty("parse.model", "edu/stanford/nlp/models/parser/nndep/english_SD.gz");
    StanfordCoreNLP pipeline = new StanfordCoreNLP(prop);
    Annotation annotation = new Annotation(string);
    pipeline.annotate(annotation); // add annotation to pipeline
    // a CoreMap is essentially a Map that uses class objects as keys and has values with custom types
    List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);
    for(CoreMap sentence: sentences) {
        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 POS tag of the token
            String pos = token.get(CoreAnnotations.PartOfSpeechAnnotation.class);
            // this is the NER label of the token
            //String ne = token.get(CoreAnnotations.NamedEntityTagAnnotation.class);
            System.out.print(word + "/" + pos);
        }
        System.out.println("\n");
        // this is the parse tree of the current sentence
        Tree tree = sentence.get(TreeCoreAnnotations.TreeAnnotation.class);
        System.out.println("parse tree:\n" + tree);

        SemanticGraph dependencies = sentence.get(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation.class);
        System.out.println("dependency graph:\n" + dependencies);
    }

}
 
開發者ID:IsaacChanghau,項目名稱:Word2VecfJava,代碼行數:37,代碼來源:StanfordCoreNLPExample.java

示例15: main

import edu.stanford.nlp.pipeline.StanfordCoreNLP; //導入方法依賴的package包/類
public static void main(String[] args) {
        try {
            Properties properties = new Properties();
            properties.setProperty("annotators", "ita_toksent, udpipe, ita_verb");
            properties.setProperty("customAnnotatorClass.udpipe", "eu.fbk.fcw.udpipe.api.UDPipeAnnotator");
            properties.setProperty("customAnnotatorClass.ita_toksent",
                    "eu.fbk.dh.tint.tokenizer.annotators.ItalianTokenizerAnnotator");
            properties.setProperty("customAnnotatorClass.ita_verb",
                    "eu.fbk.dh.tint.verb.VerbAnnotator");

            properties.setProperty("udpipe.server", "gardner");
            properties.setProperty("udpipe.port", "50020");
            properties.setProperty("udpipe.keepOriginal", "1");

//        properties.setProperty("udpipe.model", "/Users/alessio/Desktop/model");
            StanfordCoreNLP pipeline = new StanfordCoreNLP(properties);

            Annotation annotation = new Annotation("Il caporale alpino Giampietro Civati caduto in combattimento il 5 dicembre 1944, come racconta Silvestri, ha scritto questo mirabile testamento: «sono figlio d’Italia, d’anni 21, non di Graziani e nemmeno di Badoglio, ma sono italiano e seguo la via che salverà l’onore d’Italia».");
            pipeline.annotate(annotation);
            String out = JSONOutputter.jsonPrint(annotation);
            System.out.println(out);
//            for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) {
//                System.out.println(sentence.get(VerbAnnotations.VerbsAnnotation.class));
//            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
開發者ID:dhfbk,項目名稱:tint,代碼行數:30,代碼來源:VerbTest.java


注:本文中的edu.stanford.nlp.pipeline.StanfordCoreNLP.annotate方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。