Skip to main content
 首页 » 编程设计

python之WatsonException : Error: unsupported text language, 代码:400

2024年10月01日33hnrainll

我正在使用自然语言理解 api。 文本我正在使用“hmmmm nawa ohh wen am I gona win ds tin”,它给出了错误

WatsonException: Error: unsupported text language, Code: 400  

我的代码是:

response = natural_language_understanding.analyze( 
    text='hmmmm nawa ohh wen am I gona win ds tin', 
    features=[features.Sentiment(), features.Keywords(), features.Emotion(), features.Categories()]) 

如何将这些类型的文本传递给 NLU api。 需要帮助。

请您参考如下方法:

它失败了,因为在尝试确定特征之前,它会尝试猜测语言是什么。设置语言将防止这种情况。

例如:

question = 'hmmmm nawa ohh wen am I gona win ds tin' 
 
f = [ 
    features.Categories(), 
    features.Concepts(), 
    features.Emotion(), 
    features.Entities(),  
    features.Relations(), 
    features.SemanticRoles(), 
    features.Sentiment() 
] 
r = nlu.analyze(text=question, features=f, language='en') 
 
print(json.dumps(r, indent=2)) 

输出这个:

{ 
  "sentiment": { 
    "document": { 
      "score": 0.0, 
      "label": "neutral" 
    } 
  }, 
  "semantic_roles": [ 
    { 
      "subject": { 
        "text": "I" 
      }, 
      "sentence": "hmmmm nawa ohh wen am I gona win ds tin", 
      "object": { 
        "text": "ds tin" 
      }, 
      "action": { 
        "verb": { 
          "text": "win", 
          "tense": "present" 
        }, 
        "text": "win", 
        "normalized": "win" 
      } 
    } 
  ], 
  "relations": [], 
  "language": "en", 
  "entities": [], 
  "emotion": { 
    "document": { 
      "emotion": { 
        "sadness": 0.193275, 
        "joy": 0.309168, 
        "fear": 0.167981, 
        "disgust": 0.06316, 
        "anger": 0.130959 
      } 
    } 
  }, 
  "concepts": [], 
  "categories": [ 
    { 
      "score": 0.899547, 
      "label": "/art and entertainment" 
    }, 
    { 
      "score": 0.365657, 
      "label": "/hobbies and interests/reading" 
    }, 
    { 
      "score": 0.189432, 
      "label": "/art and entertainment/movies and tv/movies" 
    } 
  ] 
} 

虽然这不是正确的英语,所以我不希望结果很好。

您可以在此处查看支持的语言功能:

https://www.ibm.com/watson/developercloud/doc/natural-language-understanding/index.html#supported-languages