首页 > 解决方案 > 使用 Watson SDK API 的主题建模示例

问题描述

我有一份将在不同时间点在线上传的文件列表。我没有关于内容的任何先验信息,我也没有关于可以分配给文档的可能标签的任何信息,我也没有任何历史数据(因此我无法使用 Watson 训练分类器自然语言分类服务)。我想要的是对这些文档进行实时分类/主题分配。例如,我正在搜索以下一些 API:

service.getTopics('some text')

实时返回如下内容

"categories": [
          {
            "score": 0.949576,
            "label": "/technology and computing/networking"
          },
          {
            "score": 0.911692,
            "label": "/technology and computing/networking/network monitoring and management"
          },
          {
            "score": 0.879639,
            "label": "/business and industrial/business operations/management"
          }
]

是否可以使用 Watson 发现或 NLU 服务?我正在使用 python SDK API,一个示例/任何相关链接都会非常有帮助。谢谢

标签: pythonmachine-learningnlpibm-watsontopic-modeling

解决方案


我认为 Watson Natural Language Understanding 服务的categoriesconcepts特性是最合适的。您不能直接使用 API 发送文档,因此您需要提取文本,但如果您能够这样做,那么:

摘自 API Docs 页面的示例


from ibm_watson import NaturalLanguageUnderstandingV1
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
from ibm_watson.natural_language_understanding_v1 
    import Features, ConceptsOptions, CategoriesOptions

authenticator = IAMAuthenticator('{apikey}')
natural_language_understanding = NaturalLanguageUnderstandingV1(
    version='2019-07-12',
    authenticator=authenticator)

natural_language_understanding.set_service_url('{url}')

response = natural_language_understanding.analyze(
    text='IBM is an American multinational technology company '
    'headquartered in Armonk, New York, United States, '
    'with operations in over 170 countries.',
    features=Features(
        categories=CategoriesOptions(limit=5),
        concepts=ConceptsOptions(limit=5))).get_result()



更多信息在 API 文档中 - https://cloud.ibm.com/apidocs/natural-language-understanding/natural-language-understanding?code=python#categories


推荐阅读