0

我正在通过遵循示例 python 代码来学习谷歌自然语言 API,并在谷歌 colab 中运行相同的代码,但是,我遇到了错误。

# Create the document
document = { 
    "type_": "PLAIN_TEXT",
    "language": "en",
    "content": "Hello World. I love you! I hate you!"
}

results = client.analyze_sentiment(
    document = document,
    encoding_type = language_v1.EncodingType.UTF8)

而且我得到以下错误

AttributeError                            Traceback (most recent call last)
<ipython-input-26-17157fa1dfc4> in <module>()
      8 results = client.analyze_sentiment(
      9     document = document,
---> 10     encoding_type = language_v1.EncodingType.UTF8)

AttributeError: module 'google.cloud.language_v1' has no attribute 'EncodingType'

这似乎是一个直截了当的错误,但在尝试了不同的选项后,比如删除 encoding_type,它仍然不起作用

4

1 回答 1

0

我找到了根本原因。示例代码中有两个问题:

  1. “type_”无效。应该改成“类型”
  2. encoding_type 应该直接设置为“UTF8”。如 encoding_type = "UTF8"
于 2021-12-30T06:33:36.147 回答