当前位置: 首页 > 面试题库 >

使用nltk改进对人名的提取

周楷
2023-03-14
问题内容

我正在尝试从文本中提取人名。

有人有推荐的方法吗?

这就是我尝试过的(下面的代码):我正在使用nltk查找所有标记为人的东西,然后生成该人所有NNP部分的列表。我正在跳过只有一个NNP可以避免抓住一个姓氏的人。

我得到了不错的结果,但是想知道是否有更好的方法来解决这个问题。

码:

import nltk
from nameparser.parser import HumanName

def get_human_names(text):
    tokens = nltk.tokenize.word_tokenize(text)
    pos = nltk.pos_tag(tokens)
    sentt = nltk.ne_chunk(pos, binary = False)
    person_list = []
    person = []
    name = ""
    for subtree in sentt.subtrees(filter=lambda t: t.node == 'PERSON'):
        for leaf in subtree.leaves():
            person.append(leaf[0])
        if len(person) > 1: #avoid grabbing lone surnames
            for part in person:
                name += part + ' '
            if name[:-1] not in person_list:
                person_list.append(name[:-1])
            name = ''
        person = []

    return (person_list)

text = """
Some economists have responded positively to Bitcoin, including 
Francois R. Velde, senior economist of the Federal Reserve in Chicago 
who described it as "an elegant solution to the problem of creating a 
digital currency." In November 2013 Richard Branson announced that 
Virgin Galactic would accept Bitcoin as payment, saying that he had invested 
in Bitcoin and found it "fascinating how a whole new global currency 
has been created", encouraging others to also invest in Bitcoin.
Other economists commenting on Bitcoin have been critical. 
Economist Paul Krugman has suggested that the structure of the currency 
incentivizes hoarding and that its value derives from the expectation that 
others will accept it as payment. Economist Larry Summers has expressed 
a "wait and see" attitude when it comes to Bitcoin. Nick Colas, a market 
strategist for ConvergEx Group, has remarked on the effect of increasing 
use of Bitcoin and its restricted supply, noting, "When incremental 
adoption meets relatively fixed supply, it should be no surprise that 
prices go up. And that’s exactly what is happening to BTC prices."
"""

names = get_human_names(text)
print "LAST, FIRST"
for name in names: 
    last_first = HumanName(name).last + ', ' + HumanName(name).first
        print last_first

输出:

LAST, FIRST
Velde, Francois
Branson, Richard
Galactic, Virgin
Krugman, Paul
Summers, Larry
Colas, Nick

除了维珍银河,这都是有效的输出。当然,在本文中了解维珍银河不是人的名字是很困难的(也许是不可能的)部分。


问题答案:

必须同意“使我的代码更好”这个网站不太适合的建议,但是我可以为您提供一些 尝试的 途径。

看看斯坦福命名实体识别器(NER)。它的绑定已包含在NLTK v
2.0中,但是您必须下载一些核心文件。这是可以为您完成所有操作的脚本。

我写了这个脚本:

import nltk
from nltk.tag.stanford import NERTagger
st = NERTagger('stanford-ner/all.3class.distsim.crf.ser.gz', 'stanford-ner/stanford-ner.jar')
text = """YOUR TEXT GOES HERE"""

for sent in nltk.sent_tokenize(text):
    tokens = nltk.tokenize.word_tokenize(sent)
    tags = st.tag(tokens)
    for tag in tags:
        if tag[1]=='PERSON': print tag

并没有那么糟糕的输出:

(’Francois’,’PERSON’)(’R.’,’PERSON’)(’Velde’,’PERSON’)(’Richard’,’PERSON’)(’Branson’,’PERSON’)(’Virgin’
,’PERSON’)(’Galactic’,’PERSON’)(’Bitcoin’,’PERSON’)(’Bitcoin’,’PERSON’)(’Paul’,’PERSON’)(’Krugman’,’PERSON’)
(’Larry’,’PERSON’)(’Summers’,’PERSON’)(’Bitcoin’,’PERSON’)(’Nick’,’PERSON’)(’Colas’,’PERSON’)

希望这会有所帮助。



 类似资料:
  • 我是新的Python和nltk。我已经将代码从https://gist.github.com/alexbowe/879414转换为下面给定的代码,使其运行于许多文档/文本块。但我得到了以下错误 有人能帮我解决这个问题吗。我必须从数以百万计的产品评论中提取名词短语。我使用了使用Java的Standford NLP工具包,但速度非常慢,所以我认为在python中使用nltk会更好。如果有更好的解决方案

  • 问题内容: 我正在尝试使用Python NLTK中的斯坦福命名实体识别器(NER)提取人员和组织的列表。当我跑步时: 输出为: 我想要的是从此列表中以这种形式提取所有人员和组织: 我试图遍历元组列表: 但是此代码仅每行打印一个实体: 有了真实的数据,一个句子中可以有多个组织,一个人,那么我该如何限制不同实体之间的界限呢? 问题答案: 感谢@Vaulstein发现的链接,很显然,受过训练的Stanf

  • 问题内容: 有更有效的方法吗?我的代码读取一个文本文件并提取所有名词。 如何减少此代码的时间复杂度?有没有办法避免使用嵌套的for循环? 提前致谢! 问题答案: 如果您不接受其他选项,请签出。它可以轻松提取所有名词和名词短语:

  • 本文向大家介绍在Python中使用NLTK库实现对词干的提取的教程,包括了在Python中使用NLTK库实现对词干的提取的教程的使用技巧和注意事项,需要的朋友参考一下 什么是词干提取? 在语言形态学和信息检索里,词干提取是去除词缀得到词根的过程─—得到单词最一般的写法。对于一个词的形态词根,词干并不需要完全相同;相关的词映射到同一个词干一般能得到满意的结果,即使该词干不是词的有效根。从1968年开

  • 问题内容: 是否可以使用NLTK WordNet查找专有名词?即可以使用nltk Wordnet标记所有名词吗? 问题答案: 我认为您不需要WordNet来查找专有名词,我建议使用词性标记器。 要查找专有名词,请寻找标签: 您可能没有,因为很满意,并分裂成2个令牌,则可能需要更复杂的东西,如名称实体恶搞。 如标签集所记录的那样,对于所有格名词而言,只要找到标签,您就可以轻松找到http://www

  • 在网络请求中使用和有什么好处。我见过许多使用的示例,但我想明白为什么。 示例情形: 为每个工作单元创建一个新线程。将使用线程池 但这种争论对应用程序有什么影响呢?还有哪些方面?