2017-07-17 21 views
0

我看起來NLU沒有識別出我提供的全部數據。我在代碼中做錯了什麼,或者錯誤地假設api應該如何工作?包含api的響應,幷包含已分析的代碼以及完整提交的文本。有一個三角洲,我不確定這是爲什麼。爲什麼NLU不能讀取數據的全部文本

這裏是我的代碼:

def nlu(text): 
print("Calling NLU") 
url = "https://gateway.watsonplatform.net/natural-language-understanding/api/v1/analyze?version=2017-02-27" 
data = { 
    'text': text, 
    'language': "en", 
    'return_analyzed_text': True, 
    'clean': True, 
    'features': { 
     'entities': { 
      'emotion': True, 
      'sentiment': True, 
      'limit': 2 
     }, 
     "concepts": { 
      "limit": 15 
     }, 
     'keywords': { 
      'emotion': True, 
      'sentiment': True, 
      'limit': 2 
     } 
    } 
} 
headers = { 
    'content-type': "application/json" 
} 
username = os.getenv("nlu-username") 
password = os.getenv("nlu-password") 
print("NLU", username, password) 
print("data", json.dumps(data)) 
response = requests.request("POST", url, data=json.dumps(data), headers=headers, auth=(username, password)) 
print("Done calling NLU") 
print(response.text) 

這裏的請求/響應:

"keywords": [ 
{ 
    "text": "anthropologists study skeletons", 
    "sentiment": { 
    "score": 0.0 
    },"analyzed_text": "move between two thousand eight and two thousand twelve archaeologists excavated the rubble of an ancient hospital in England in the process they uncovered a number of skeletons one in particular belong to a wealthy Mel who lived in the eleventh or twelfth century and died of leprosy between the ages of eighteen and twenty five how do we know all this simply by examining some old soil Kate bones even centuries after death skeletons carry unique features that tell us about their identities and using modern tools and techniques we can read those features as clues this is a branch of science known as biological anthropology it allows researchers to piece together details about Incheon individuals and identify historical events that affected whole populations when researchers uncover a skeleton some of the first clues they gather like age and gender line its morphology which is the structure appearance and size of a skeleton mostly the clavicle stop growing at age twenty five so a skeleton with the clavicle that hasn't fully formed must be younger than similarly the plates in the cranium can continue fusing up to age forty and sometimes beyond by combining these with some microscopic skeletal clues physical anthropologists can estimate an approximate age of death meanwhile pelvic bones reveal gender biologically female palaces are wider allowing women to give birth whereas males are narrower those also betrayed the signs of aging disease disorders like anemia leave their traces on the bones and the condition of teeth can reveal clues to factors like diet and malnutrition which sometimes correlate with wealth or poverty a protein called collagen can give us even more profound details the air we breathe water we drink and food we eat leaves permanent traces in our bones and teeth in the form of chemical compounds these compounds contain measurable quantities called isotopes stable isotopes in bone collagen and tooth enamel varies among mammals dependent on where they lived and what they eat so but analyzing these isotopes we can draw direct inferences regarding the diet and location of historic people not only that but during life bones undergo a constant cycle of remodeling so if someone moves from one place to another bones synthesized after that move will also reflect the new isotopic signatures of the surrounding environment that means that skeletons can be used like migratory maps for instance between one and six fifty A. D. the great city of TOT Makana Mexico bustled with thousands of people researchers examined the isotope ratios and skeletons to the now which held details of their diets when they were young they found evidence for significant migration into the city a majority of the individuals were born elsewhere with further geological and skeletal analysis they may be able to map where those people came from that work in tier two Akon is also an example of how bio anthropologists study skeletons in cemeteries and mass graves and analyze their similarities and differences from not information they can learn about cultural beliefs social norms wars and what caused their deaths today we use these tools to answer big questions about how forces like migration and disease shape the modern world DNA analysis is even possible in some relatively well preserved ancient remains that's helping us understand how diseases like tuberculosis have evolved over the centuries so we can build better treatments for people today ocean skeletons can tell us a surprisingly great deal about the past two of your remains are someday buried intact what might archaeologists of the distant future learn from them" 

回答

0

我只是試圖在自然語言理解與你的文字和獲取適當的響應。檢查下面的結果。我認爲你應該首先嚐試Watson API Explorer與您的服務憑據。它還將幫助您修復錯誤放置的標題或錯過了API調用的參數。

注:進行POST調用,因爲它是對URL和HTML前的參數對象{}:只需卸下「元數據」。

{ 
"semantic_roles": [{ 
    "subject": { 
     "text": "anthropologists" 
    }, 
    "sentence": "anthropologists study skeletons", 
    "object": { 
     "text": "skeletons" 
    }, 
    "action": { 
     "verb": { 
      "text": "study", 
      "tense": "present" 
     }, 
     "text": "study", 
     "normalized": "study" 
    } 
}], 
"language": "en", 
"keywords": [{ 
     "text": "anthropologists", 
     "relevance": 0.966464 
    }, 
    { 
     "text": "skeletons", 
     "relevance": 0.896147 
    } 
], 
"entities": [], 
"concepts": [{ 
    "text": "Cultural studies", 
    "relevance": 0.86926, 
    "dbpedia_resource": "http://dbpedia.org/resource/Cultural_studies" 
}], 
"categories": [{ 
     "score": 0.927751, 
     "label": "/science/social science/anthropology" 
    }, 
    { 
     "score": 0.219365, 
     "label": "/education/homework and study tips" 
    }, 
    { 
     "score": 0.128377, 
     "label": "/science" 
    } 
], 
"warnings": [ 
    "emotion: cannot locate keyphrase", 
    "relations: Not Found", 
    "sentiment: cannot locate keyphrase" 
] 

}

+0

您列出的網站非常有用。它與官方文檔有關嗎? [Watson api post comman](https://www.ibm.com/watson/developercloud/natural-language-understanding/api/v1/?curl#post-analyze)你是怎麼找到它的?我認爲一個修補程序被推入是因爲它在我發佈到SO後第二天就開始工作。 –

+0

大部分API調用都與官方文檔相關聯。某些服務已棄用,但其API調用仍可在此應用中使用,如Alchemy。而對於NLU,我在一週之前檢查了此應用程序的API調用,然後纔看到您的查詢。對我來說,它在那裏。 –

+0

非常酷。直到你寄出它,我甚至都沒有看到它 - 感謝你的分享。 –

0

在你的代碼有

data=json.dumps(data)

這是整個JSON對象轉換爲字符串。這應該僅僅是:

data=data

而且我會建議使用Python WDC SDK,因爲它會使你更容易。

與上述相同的例子。

import json 
from watson_developer_cloud import NaturalLanguageUnderstandingV1 
import watson_developer_cloud.natural_language_understanding.features.v1 as Features 

username = os.getenv("nlu-username") 
password = os.getenv("nlu-password") 

nluv1 = NaturalLanguageUnderstandingV1(
    username=username, 
    password=password) 

features = [ 
    Features.Entities(), 
    Features.Concepts(), 
    Features.Keywords() 
] 

def nlu(text): 
    print('Calling NLU') 
    response = nluv1.analyze(text,features=features, language='en') 
    print('Done calling NLU') 
    print(json.dumps(response, indent=2)) 
+0

偉大的建議,我很樂意但由於各種原因SDK無法安裝軟件包。 [堆棧溢出問題](https://stackoverflow.com/questions/45154082/why-does-pip-install-fail-for-the-python-watson-developer-cloud/45159403#45159403)爲什麼不只是發佈所需的請求/響應在api文檔中作爲示例? –

+0

該問題不適用於WDC SDK。這是一個已知的安裝問題,有六個和OSX/python 2.7。無論如何,上面的示例代碼是python 3.6,它安裝SDK罰款。 –

+0

謝謝你的建議Simon。有沒有一種方法可以將SDK中的這些依賴關係重構或者將客戶端拆分爲可以單獨導入和安裝的獨立依賴項? –

相關問題