gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
hi
- User
hi
- Assistant
Hello! How may I assist you today?
- User
统计词语频率
- Assistant
好的,您需要统计哪个文本的词语频率呢?
- User
招标项目
- Assistant
好的,请问您是需要统计某个特定招标项目的词语频率,还是需要统计多个招标项目的词语频率呢?
- User
统计存在我电脑桌面的文件《招标项目》
- Assistant
非常好,以下是使用Python语言统计文本中词语频率的代码:
# 导入模块 import os # 读取文件 path = os.path.expanduser("~/Desktop/招标项目.txt") with open(path, 'r', encoding='UTF-8') as f: lines = f.readlines() # 统计词频 word_count = {} for line in lines: words = line.strip().split(' ') for word in words: if word != '': word_count[word] = word_count.get(word, 0) + 1 # 打印词频结果 sorted_word_count = sorted(word_count.items(), key=lambda x: x[1], reverse=True) for word, count in sorted_word_count: print(word, count)请您将代码保存为一个.py文件并放置在桌面,然后将《招标项目》文件命名为“招标项目.txt”并放置在桌面,最后双击运行.py文件即可输出结果。结果会按照词语出现次数从高到低排序,便于您了解招标项目中哪些词语出现的频率较高。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.