2017-01-17 42 views
1

道歉,如果這不是完全清楚 - 我是一個Python複製代碼和嘗試工作的開發人員。如何檢測Google NLP API中的專有名詞?

我在Python 2.7中使用Google NLP API。

當我使用analyze_entities()時,我可以獲取並打印名稱,實體類型和顯着性。

提及應該包含名詞類型:正確或COMMON,每本頁面:

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 '] 佐伊3人0.0016676666 [u'Zoe 3']

在哪裏在[]中的值是提及的。

如果我嘗試獲取mentions.type,我得到一個屬性找不到錯誤。

我會很感激任何輸入。

+1

從源代碼看來,Google Python庫從提及的文件中刪除了「type」值... https://github.com/GoogleCloudPlatform/google-cloud-python/blob/a02ac500548cf9fc37f4d81033e696a0efb53f99/language/ google/cloud/language/entity.py#L106-L107 – user

+0

恩,當然。有沒有很好的理由,還是應該砍掉價值迴歸? – user1066609

+0

我不知道他們爲什麼這樣做......你不得不問一個開發者。你總是可以分解回購商品並對其進行破解......如果對其他人看起來有用,則可以製作PR。 – user

回答

0

1)不要調用「AnalyzeEntities」函數,而是調用「AnnotateText」函數。

2)檢查「正確」。檢查它的值,它應該是「PROPER」而不是「PROPER_UNKNOWN」和「NOT_PROPER」。