赵灵越 2023-07-17 14:39 采纳率: 99.3%
浏览 12
已结题

这jieba 分词代码在python上运行,有什么错误的地方吗?

# 机器人给我的代码,在安卓手机上termux使用

import jieba

def analyze_text(text):
    # 分词并拼接为字符串
    words = jieba.lcut(text)
    seg_result = ' '.join(words)

    # 统计词语出现次数
    word_count = {}
    for word in words:
        if word in word_count:
            word_count[word] += 1
        else:
            word_count[word] = 1

    # 统计负面词语
    negative_words = []
    for word in words:
        if word in ["不好", "糟糕", "差"]:
            negative_words.append(word)

    # 词语归纳
    categories = {}
    for word in words:
        category = ''

        if word == '苹果':
            category = '水果'
        elif word == '电视':
            category = '电子产品'

        # 根据其他需求进行更多归纳判断
        # ...

        if category:
            if category in categories:
                categories[category].append(word)
            else:
                categories[category] = [word]

    return {
        "segmented_text": seg_result,
        "word_count": word_count,
        "negative_words": negative_words,
        "categories": categories
    }

# 输入一句话
input_text = input("请输入一句话:")

# 调用函数进行分析
analysis_result = analyze_text(input_text)

# 打印结果
print("分词结果:", analysis_result["segmented_text"])
print("词语统计:", analysis_result["word_count"])
print("负面词语:", analysis_result["negative_words"])
print("词语归纳:", analysis_result["categories"])
>>> print("分词结果:", analysis_result["segmented_text"])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not subscriptable
>>> print("词语统计:", analysis_result["word_count"])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not subscriptable
>>> print("负面词语:", analysis_result["negative_words"])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not subscriptable
>>> print("词语归纳:", analysis_result["categories"])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not subscriptable

  • 写回答

4条回答 默认 最新

  • 技术宅program 2023-07-17 15:02
    关注

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 7月27日
  • 已采纳回答 7月19日
  • 创建了问题 7月17日

悬赏问题

  • ¥15 如何在vue.config.js中读取到public文件夹下window.APP_CONFIG.API_BASE_URL的值
  • ¥50 浦育平台scratch图形化编程
  • ¥20 求这个的原理图 只要原理图
  • ¥15 vue2项目中,如何配置环境,可以在打完包之后修改请求的服务器地址
  • ¥20 微信的店铺小程序如何修改背景图
  • ¥15 UE5.1局部变量对蓝图不可见
  • ¥15 一共有五道问题关于整数幂的运算还有房间号码 还有网络密码的解答?(语言-python)
  • ¥20 sentry如何捕获上传Android ndk 崩溃
  • ¥15 在做logistic回归模型限制性立方条图时候,不能出完整图的困难
  • ¥15 G0系列单片机HAL库中景园gc9307液晶驱动芯片无法使用硬件SPI+DMA驱动,如何解决?