NLP提取相关短语

时间:2017-05-17 11:22:34

标签: nlp stanford-nlp

使用NLP从给定的句子中,我能够使用Core NLP轻松提取所有形容词和名词。 但是我努力做的事实上是从句子中提取短语。

例如,我有以下句子:

  1. 这个人值得信赖。
  2. 此人不具判断力。
  3. 这个人讲得很好。
  4. 对于使用NLP的所有这些句子,我想提取值得信赖,非评判,口语良好等等的短语。我想提取所有这些相关的词。

    我该怎么做?

    谢谢,

3 个答案:

答案 0 :(得分:1)

我认为您首先需要考虑这些特定示例,并仔细考虑要提取的内容的结构。例如,在您的情况下,您可以使用一些简单的启发式方法来查找任何一个小孩子及其所有修饰符。

如果您要提取的内容的范围大于此范围,则可以返回到绘图板,然后根据可使用的基本语言功能重新考虑一些规则。 Stanford CoreNLP或作为其他海报已链接的spaCy。

最后,如果您需要概括其他未知示例的能力,则可能希望通过提供相关的语言功能并将句子中的每个标记标记为相关标记来训练分类器(也许从简单的逻辑回归分类器开始)。还是不相关。

答案 1 :(得分:0)

对于您的特定用例Open Information Extraction似乎是一个合适的解决方案。它提取包含主题,关系和对象的三元组。你的关系似乎总是的不定式是)而你的主题似乎总是 person ,所以我们只对这个对象感兴趣。

ngAfterContentChecked()

输出如下:

import edu.stanford.nlp.ie.util.RelationTriple;
import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.ling.CoreAnnotations.TextAnnotation;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import edu.stanford.nlp.naturalli.NaturalLogicAnnotations;
import edu.stanford.nlp.util.CoreMap;
import java.util.Collection;
import java.util.Properties;

public class OpenIE {

    public static void main(String[] args) {
        // Create the Stanford CoreNLP pipeline
        Properties props = new Properties();
        props.setProperty("annotators", "tokenize,ssplit,pos,lemma,depparse,natlog,openie");
        StanfordCoreNLP pipeline = new StanfordCoreNLP(props);

        // Annotate your sentences
        Annotation doc = new Annotation("This person is trust worthy. This person is non judgemental. This person is well spoken.");
        pipeline.annotate(doc);

        // Loop over sentences in the document
        for (CoreMap sentence : doc.get(CoreAnnotations.SentencesAnnotation.class)) {
          // Get the OpenIE triples for the sentence
          Collection<RelationTriple> triples = sentence.get(NaturalLogicAnnotations.RelationTriplesAnnotation.class);
          // Print the triples
          for (RelationTriple triple : triples) {
              triple.object.forEach(object -> System.out.print(object.get(TextAnnotation.class) + " "));
              System.out.println();
          }
       }
    }
}

OpenIE算法可能会为每个句子提取多个三元组。对于您的用例,解决方案可能是使用对象中具有最大字数的三元组。

另一件值得提及的是,你的第一句话的对象不是“正确”提取的,至少不是你想要的方式。发生这种情况是因为 trust 是一个名词而值得是一个形容词。 最简单的解决方案是使用连字符(信任值)来编写它。 另一种可能的解决方案是检查Part of Speech标签,并在遇到名词后跟一个形容词时执行一些额外的步骤。

答案 2 :(得分:0)

要检查相似短语之间的相似性,可以使用单词嵌入,例如GLOVE。一些NLP库附带嵌入,例如Spacy。 https://spacy.io/usage/vectors-similarity

注意:Spacy在令牌级别和短语级别上使用余弦相似性,Spacy还为更大的短语/句子提供便利相似性功能。

例如: (使用spacy&amp; python)

doc1 = nlp(u"The person is trustworthy.")
doc2 = nlp(u"The person is non judgemental.")
cosine_similarity = doc1.similarity(doc2)

cosine_similarity可用于显示两个短语/单词/句子的相似程度,范围从0到1,其中1非常相似。