获取副词和形容词的相应动词和名词

时间:2015-09-01 10:15:26

标签: python nlp nltk stanford-nlp

如何在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

2 个答案:

答案 0 :(得分:7)

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

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

这是我们句子的解析树:

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

正如你所看到的,“美丽而快速的汽车”是包含确定者(DT)的NounPhrase(NP)和AdjectivalPhrase(ADJP,“美丽而快速”)和名词(NN,“car”)。一段时间使用的一种方法是创建一个基于规则的系统,该系统从该解析树中提取对。幸运的是,已经开发出更好的解决方案来直接解决您的问题。

DependencyGraph

依赖关系对是:

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)

正如您所看到的,这正是您所需要的。这些是类型化的依赖项,因此您还需要过滤您感兴趣的项目( amod advmod 在您的案例中)

您可以在此处找到完整的依赖关系类型列表:http://nlp.stanford.edu/software/dependencies_manual.pdf Stanford Parser Demo: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

答案 1 :(得分:0)

使用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})

enter image description here

相关问题