A minimal example for creating a StanfordNLP Parser

本文通过一个示例展示了如何使用StanfordNLP工具库进行自然语言处理任务,包括分词、词性标注、依存句法分析等。通过Java代码实现了一个简单的文本分析流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Properties props = new Properties();
        props.put("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
        
        StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
        
        String test = "Stanford University is located in California. It is a great university.";
        
        Annotation doc = new Annotation(test);
        
        // run all Annotators on this text
        pipeline.annotate(doc);
        
        List<CoreMap> sentences = doc.get(SentencesAnnotation.class);
        
        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.toString());
            
            for (CoreLabel token: sentence.get(TokensAnnotation.class)) 
            {
                String word = token.get(TextAnnotation.class);
                String lemma = token.get(CoreAnnotations.LemmaAnnotation.class);
              
                System.out.println("word:" + word);
                System.out.println("lemma:" + lemma);
            }

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值