# 机器人给我的代码,在安卓手机上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