【问题标题】:Adjectives used with named entities与命名实体一起使用的形容词
【发布时间】:2014-02-01 00:29:46
【问题描述】:

我使用下面给出的 python 代码来提取文本中存在的命名实体。现在我需要从文本中存在命名实体的那些句子中获取形容词。即与命名实体一起使用的形容词。如果有'NE',我可以更改我的代码以检查树是否有'JJ',或者有其他方法吗??

def tokenize(text): 
sentences = nltk.sent_tokenize(text) 
sentences = [nltk.word_tokenize(sent) for sent in sentences] 
sentences = [nltk.pos_tag(sent) for sent in sentences] 
return sentences 

text=open("file.txt","r").read() 
sentences=tokenize(text) 
chunk_sent=nltk.batch_ne_chunk(sentences,binary=True)
print chunk_sent[1]

输出:

Tree('S', [("'", 'POS'), ('Accomplished', 'NNP'), ('in', 'IN'), ('speech', 'NN'), (',', ','), 树('NE', [('Gautam', 'NNP')]), (',', ' ,'), ('thus', 'RB'), ('questioned', 'VBD'), (',', ','), ('gave', 'VBD'), ('in', ' IN'), ('the', 'DT'), ('midst', 'NN'), ('of', 'IN'), ('that', 'DT'), ('big', 'JJ'), ('assemblage', 'NN'), ('of', 'IN'), ('contemplative', 'JJ'), ('sages' 'NNP'), ('a', 'DT'), ('full', ' JJ'), ('and', 'CC'), ('proper', 'NN'), ('answer', 'NN'), ('in', 'IN'), ('words', ' NNS'), ('辅音', 'JJ'), ('with', 'IN'), ('他们的 ', 'PRP$'), ('mode', 'NN'), ('of', 'IN'), ('life', 'NN'), ('.', '.')])

虽然这句话在NE之前没有JJ。我怎样才能得到与NE一起使用的JJ?

def ne(tree):
    names = []
    if hasattr(tree, 'node') and tree.node:
      if tree.node == 'NE':
        names.append(' '.join([child[0] for child in tree]))
    else:
        for child in tree:
            names.extend(ne(child))

return names

names = []
for item in chunk_sent:
   names.extend(ne(item))
print names

【问题讨论】:

  • 那是什么语言?
  • @Rob 看起来像 Python
  • 是的,它是 Python 代码。
  • NLTK 在 python 中,但 OP 的缩进在帖子中是一团糟......

标签: python entity-framework nltk named-entity-recognition


【解决方案1】:
>>> from nltk.corpus import brown
>>> from nltk import batch_ne_chunk as bnc
>>> from nltk.tree import Tree
>>> sentences = brown.tagged_sents()[0:5]
>>> chunk_sents = bnc(sentences)
>>> 
>>> for sent in chunk_sents:
...     for i,j in zip(sent[:-1], sent[1:]):
...             if type(j) is Tree and i[1].startswith("JJ"):
...                     print i,j
... 
('Grand', 'JJ-TL') (PERSON Jury/NN-TL)
('Executive', 'JJ-TL') (ORGANIZATION Committee/NN-TL)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-12
    • 2011-11-28
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    相关资源
    最近更新 更多