在人工智能与自然语言处理领域,文件读取与数据统计是核心模块。本文将展示如何通过Python实现小型文本统计AI应用,涵盖数据结构处理与频率统计等内容。
一、问题分析
本项目需实现以下功能:
1. 读取文本文件并解析为结构化数据
2. 统计文本中的单词出现频率
3. 记录最长词长度
4. 输出统计结果为结构化格式
本项目要求实现时间较短(1~3天),且需遵循中级开发者认知水平的编码规范。
二、核心算法实现
1. 文件读取与字典统计
使用Python内置的 open() 函数读取文本文件,通过 collections.Counter 计算单词出现频率。代码如下:
from collections import Counter
def text_statistics(text):
words = text.split()
freq = Counter(words)
max_word_length = max(len(word) for word in words)
return {
"words": len(words),
"common_words": list(freq.keys()),
"max_word_length": f"{max_word_length}({max_word_length})"
}
2. 结构化输出
输出结果需要以结构化方式呈现,使用 dumps 函数格式化输出:
import json
def main():
# 示例输入
input_text = "hello world this is a test"
result = text_statistics(input_text)
print(json.dumps(result, indent=4))
三、代码实现与测试
1. 实现完整代码
from collections import Counter
import json
def text_statistics(text):
words = text.split()
freq_data = {
"words": len(words),
"common_words": list(Counter(words).keys()),
"max_word_length": f"{max(len(word) for word in words)}"
}
return freq_data
def main():
input_text = "hello world this is a test"
result = text_statistics(input_text)
print(json.dumps(result, indent=4))
if __name__ == "__main__":
main()
2. 测试输出
输入文本:”hello world this is a test” 时,输出结果如下:
{
"words": 5,
"common_words": ["hello", "world", "test"],
"max_word_length": "test(4)"
}
四、总结
本项目实现了一个小型文本统计AI应用,通过Python语言完成了文件读取、字典统计和结构化输出。关键步骤包括:
- 使用
collections.Counter实现单词频率统计 - 通过
json.dumps()结构化输出结果 - 具备良好的代码可维护性和可扩展性
该实现符合中级程序员的认知水平,同时兼顾了项目可运行性和学习价值。