【问题标题】:Get corresponding verbs and nouns for adverbs and adjectives获取副词和形容词对应的动词和名词
【发布时间】:2015-11-26 12:55:07
【问题描述】:

如何在python中获取副词和形容词对应的动词和名词?看起来简单的继承和优先级可能不是很准确。可能有停用词,例如。在我很高兴学习...

我无法将任何库甚至问题陈述形式化。

立即编码。现在我想返回句子中每个形容词的副词和名词对应的动词。 请帮忙。

Code:
def pos_func(input_text):
    #pos tagging code:
    text=input_text
    tokens=tokenize_words(text)
    tagged=pos_tag(tokens)
    pos_store(tagged)

def pos_store(tagged):
    verbs=[]
    adjectives=[]
    adverbs=[]
    nouns=[]
    for tag in tagged:
        pos=tag[1]
        if pos[0]=='V':
            verbs.append(tag[0])
        elif pos[0]=='N':
            nouns.append(tag[0])
        elif pos[0]=='J':
            adjectives.append(tag[0])
        elif pos[0:2]=='RB':
            adverbs.append(tag[0])


def tokenize_words(text):
    tokens = TreebankWordTokenizer().tokenize(text)
    contractions = ["n't", "'ll", "'m"]
    fix = []
    for i in range(len(tokens)):
        for c in contractions:
            if tokens[i] == c: fix.append(i)
    fix_offset = 0
    for fix_id in fix:
        idx = fix_id - 1 - fix_offset
        tokens[idx] = tokens[idx] + tokens[idx+1]
        del tokens[idx+1]
        fix_offset += 1
    return tokens

【问题讨论】:

  • 不确定我是否理解这个问题。如果您的问题是如何将形容词转换为相应的名词,这可能会有所帮助:stackoverflow.com/questions/14489309/…。你也可以查看这篇博文以获得更详尽的解释:tech.swamps.io/…
  • 感谢您的回复。但我试图解决一个不同的问题。我想通过句子和单词(形容词)并得到对应的形容词在那个无处不在的句子中描述的名词。这有意义吗?
  • 考虑到这句话:“delighted to learn”,你要提取什么(“delighted”,“learn”)?
  • 是的,完全成对!

标签: python nlp nltk stanford-nlp


【解决方案1】:

您尝试解决的一般问题称为依赖解析。要提取单词之间的这种关系,您需要的不仅仅是简单的 POS 标记分析提供的线性单词序列。考虑以下句子:

“他买了一辆漂亮又快的车。”您将提取 (beautiful, car) 和 (fast, car)。您面临的问题不仅仅是过滤名词和副词之间的停用词。使用解析树分析可以让您更好地了解为什么这不是您可以使用单词序列来解决的问题。

这是我们句子的解析树:

(ROOT
  (S
    (NP (PRP He))
    (VP (VBD bought)
      (NP (DT a)
        (ADJP (JJ beautiful)
          (CC and)
          (JJ fast))
        (NN car)))
    (. .)))

如您所见,“a beautiful and fast car”是一个名词短语(NP),包含一个限定词(DT)、形容词短语(ADJP,“beautiful and fast”)和名词(NN,“car”)。使用了一段时间的一种方法是创建一个基于规则的系统,从这个解析树中提取对。幸运的是,已经开发出更好的东西来直接解决您的问题。

依赖对是:

nsubj(bought-2, He-1)
root(ROOT-0, bought-2)
det(car-7, a-3)
amod(car-7, beautiful-4)
cc(beautiful-4, and-5)
conj:and(beautiful-4, fast-6)
amod(car-7, fast-6)
dobj(bought-2, car-7)

如您所见,这正是您所需要的。这些是类型化的依赖项,因此您还需要过滤您感兴趣的那些(amodadvmod 在您的情况下)

你可以在这里找到完整的依赖类型列表:http://nlp.stanford.edu/software/dependencies_manual.pdf 斯坦福解析器演示:http://nlp.stanford.edu:8080/parser/ 斯坦福核心 NLP 演示(用于炫酷的可视化):http://nlp.stanford.edu:8080/corenlp/

您可以在此处阅读一篇关于在 Python 中创建依赖项解析器的精彩文章(不过您需要训练数据):https://honnibal.wordpress.com/2013/12/18/a-simple-fast-algorithm-for-natural-language-dependency-parsing/

CoreNLP 的 Python 接口:https://github.com/dasmith/stanford-corenlp-python

你也可以尝试编写你自己的依赖语法,NLTK 提供了一个 API (查找章节“5 Dependencies and Dependency Grammar”):http://www.nltk.org/book/ch08.html

【讨论】:

    【解决方案2】:

    使用 SpaCy 库和bogs' answer 中的例句,我得到了一些接近斯坦福的东西。

    >>> import spacy
    >>> nlp = spacy.load("en_core_web_sm")
    >>> doc = nlp("He bought a beautiful and fast car.")
    # to match the output style of the Stanford library for comparison...
    >>> for token in doc:
            print(f"{token.dep_}({token.head.text}-{token.head.i+1}, {token.text}-{token.i+1})")
    
    nsubj(bought-2, He-1)
    ROOT(bought-2, bought-2)
    det(car-7, a-3)
    amod(car-7, beautiful-4)
    cc(beautiful-4, and-5)
    conj(beautiful-4, fast-6)
    dobj(bought-2, car-7)
    punct(bought-2, .-8)
    

    有趣的是,它错过了与汽车快速的直接amod 连接。

    displacy.render(doc, style="dep", jupyter=True, options={'distance': 100})
    

    【讨论】:

      猜你喜欢
      • 2018-07-12
      • 1970-01-01
      • 1970-01-01
      • 2018-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多