【问题标题】:StanfordCoreNLP does not work in my wayStanfordCoreNLP 不能以我的方式工作
【发布时间】:2014-04-15 14:39:54
【问题描述】:

我使用下面的代码。然而,结果并不是我所期望的。结果是[machine, Learning] 但我想得到[machine, learn]。我怎样才能做到这一点?另外,当我的输入是"biggest bigger"时,我想得到[big, big]这样的结果,但结果只是[biggest bigger]

(PS:我只是在我的eclipse中添加了这四个jar:joda-time.jar, stanford-corenlp-3.3.1-models.jar, stanford-corenlp-3.3.1.jar, xom.jar我还需要添加一些吗?)

import java.util.LinkedList;
import java.util.List;
import java.util.Properties;

import edu.stanford.nlp.ling.CoreAnnotations.LemmaAnnotation;
import edu.stanford.nlp.ling.CoreAnnotations.SentencesAnnotation;
import edu.stanford.nlp.ling.CoreAnnotations.TokensAnnotation;
import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import edu.stanford.nlp.util.CoreMap;

public class StanfordLemmatizer {

    protected StanfordCoreNLP pipeline;

    public StanfordLemmatizer() {
        // Create StanfordCoreNLP object properties, with POS tagging
        // (required for lemmatization), and lemmatization
        Properties props;
        props = new Properties();
        props.put("annotators", "tokenize, ssplit, pos, lemma");


        this.pipeline = new StanfordCoreNLP(props);
    }

    public List<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
        this.pipeline.annotate(document);
        // Iterate over all of the sentences found
        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)) {
                // Retrieve and add the lemma for each word into the
                // list of lemmas
                lemmas.add(token.get(LemmaAnnotation.class));
            }
        }
        return lemmas;
    }


    // Test
    public static void main(String[] args) {
        System.out.println("Starting Stanford Lemmatizer");
        String text = "Machine Learning\n";
        StanfordLemmatizer slem = new StanfordLemmatizer();
        System.out.println(slem.lemmatize(text));
    }

}

【问题讨论】:

    标签: java nlp stanford-nlp stemming lemmatization


    【解决方案1】:

    Lemmatization 应该理想地返回一组单词的规范形式(称为“引理”或“标题”)。然而,这种规范形式并不总是我们直觉所期望的。例如,您希望“学习”产生引理“学习”。但是名词“learning”有引理“learning”,而只有现在进行时动词“learning”有引理“learn”。如果有歧义,词形还原器应该依赖于词性标签中的信息。

    嗯,这就解释了机器学习,但是又大又大呢?

    词形还原依赖于形态分析。斯坦福形态学课程通过仅删除屈折变化(而不是派生形态)来计算英语单词的基本形式。也就是说,它只处理名词复数、代词格和动词结尾,而不包括比较形容词或派生名词之类的东西。它基于 John Carroll 等人用 flex 编写的有限状态转换器。我找不到原始版本,但是Java版本似乎是available here

    这就是为什么 biggest 不会产生 big

    不过,WordNet 词法数据库会解析到正确的引理。我通常使用 WordNet 进行词形还原任务,到目前为止还没有发现任何重大问题。正确处理您的示例的另外两个众所周知的工具是

    1. CST Lemmatizer
    2. MorphAdorner

    【讨论】:

    • 我不知道为什么它显示[machine, Learning]而不是[machine, learning]?为什么 L 还是要大写?
    • 我猜“学习”的 POS 标签是“NNP”(专有名词),这就是它返回大写单词的原因。你能把POS标签打印出来检查一下吗?
    • 对不起!我是这里的新手。我不知道如何打印 POS 标签。你能告诉我怎么做吗?
    • token.get(PartOfSpeechAnnotation.class)
    • 它显示NNP JJS JJR。我不知道这是什么意思..你知道如何改变以使学习成为学习吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多