如何使用Google NLP API检测专有名词?

2024-05-20 01:51:46 发布

您现在位置:Python中文网/ 问答频道 /正文

抱歉,如果这不是完全清楚-我是一个Python复制代码,并试图让它工作的开发人员。在

我在python2.7中使用googlenlpapi。在

使用analyze_entities()时,可以获取并打印名称、实体类型和显著性。在

提及应包含名词类型:普通或普通,每页:

https://cloud.google.com/natural-language/docs/reference/rest/v1beta1/Entity#EntityMention

我从退回的字典里找不到提及类型。在

这是我可怕的密码:

def entities_text(text, client):
"""Detects entities in the text."""
language_client = client

# Instantiates a plain text document.
document = language_client.document_from_text(text)

# Detects entities in the document. You can also analyze HTML with:
#   document.doc_type == language.Document.HTML
entities = document.analyze_entities()
return entities
articles = os.listdir('articles')
for f in articles:
    language_client = language.Client()
    fname = "articles/" + f
    thisfile = open(fname,'r')
    content = thisfile.read()
    entities = entities_text(content, language_client)
    for e in entities:
        name = e.name.strip()
        type = e.entity_type.strip()
        if e.name.strip()[0].isupper() and len(e.name.strip()) > 2:
            print name, type, e.salience, e.mentions

返回这个:

相关其他0.0019081507[u'RELATED'] Zoe 3人0.0016676666[u'Zoe 3']

式中,[]中的值为提及次数。在

如果我想提及.类型,我得到一个属性未找到错误。在

如果有任何意见,我将不胜感激。在


Tags: thetextnameinclient类型htmltype
1条回答
网友
1楼 · 发布于 2024-05-20 01:51:46

1)不要调用“AnalyzeEntities”函数,而是调用“AnnotateText”函数。在

2)检查“正确”。考察其价值,应是“适当”而不是“适当的未知”或“不适当”。在

相关问题 更多 >