注册网站查询官网易语言如何建设网站
注册网站查询官网,易语言如何建设网站,兰州网站开发在哪里,网站开发人员岗位自然语言处理编程文档#xff1a;中文文本分词与词云生成程序
一、程序实现的功能本程序实现中文文本分词、关键词提取、词云可视化三大核心功能#xff1a;1. 读取中文文本文件#xff0c;进行分词与停用词过滤。2. 统计词频#xff0c;提取高频关键词。3. 生成美观的词云…自然语言处理编程文档中文文本分词与词云生成程序一、程序实现的功能本程序实现中文文本分词、关键词提取、词云可视化三大核心功能1. 读取中文文本文件进行分词与停用词过滤。2. 统计词频提取高频关键词。3. 生成美观的词云图直观展示文本主题。二、设计思想1. 模块化设计将功能拆分为「文本读取→分词处理→词频统计→词云生成」四个独立模块便于维护和扩展。2. 工具选型◦ 分词使用jieba结巴分词实现高效中文分词。◦ 可视化使用wordcloud生成词云matplotlib展示结果。◦ 数据处理使用collections.Counter统计词频。3. 流程逻辑输入文本 → 分词 → 过滤停用词 → 统计词频 → 生成词云 → 展示结果三、用到的主要库和函数介绍1. jieba结巴分词• 功能中文分词、添加自定义词典、停用词过滤。• 核心函数◦ jieba.cut()对文本进行分词返回生成器。◦ jieba.load_userdict()加载自定义词典优化分词效果。◦ jieba.add_word()添加单个自定义词汇。2. wordcloud词云生成库• 功能根据词频生成词云图。• 核心类◦ WordCloud()词云对象可设置画布大小、字体、最大词数、背景色等参数。◦ generate_from_frequencies()根据词频字典生成词云。3. matplotlib.pyplot• 功能可视化展示词云图。• 核心函数◦ plt.imshow()显示词云图像。◦ plt.axis(off)隐藏坐标轴。◦ plt.show()展示图像。4. collections.Counter• 功能统计词频返回词频字典。• 核心方法◦ Counter()接收分词列表统计每个词的出现次数。四、测试数据输入文本示例人工智能是研究、开发用于模拟、和人工智能扩展超越人类智能的理论、方法、技术及应用系统的一门新的技术科学。人工智能的核心包括机器学习、深度学习、自然语言处理等领域。近年来人工智能在医疗、金融、交通等行业得到广泛应用极大地提升了生产效率和服务质量。停用词部分的、是、在、等、了、及、用于、一门、新的、近年来五、输出结果1. 分词结果[人工智能, 研究, 开发, 模拟, 延伸, 超越, 人类智能, 理论, 方法, 技术, 应用系统, 技术科学, 人工智能, 核心, 包括, 机器学习, 深度学习, 自然语言处理, 领域, 人工智能, 医疗, 金融, 交通, 行业, 广泛应用, 极大, 提升, 生产效率, 服务质量]2. 词频统计前5人工智能: 3技术: 2应用: 2机器学习: 1深度学习: 13. 词云图生成以「人工智能」为最大词「技术」「应用」等为次大词的词云图背景为白色字体为微软雅黑。六、完整代码示例import jiebafrom wordcloud import WordCloudimport matplotlib.pyplot as pltfrom collections import Counter# 1. 读取文本def read_text(file_path):with open(file_path, r, encodingutf-8) as f:return f.read()# 2. 分词与停用词过滤def process_text(text, stopwords_path):# 加载停用词with open(stopwords_path, r, encodingutf-8) as f:stopwords set(f.read().splitlines())# 分词words jieba.cut(text)# 过滤停用词filtered_words [word for word in words if word.strip() and word not in stopwords]return filtered_words# 3. 统计词频def count_words(filtered_words):return Counter(filtered_words)# 4. 生成词云def generate_wordcloud(word_freq):wc WordCloud(font_pathmsyh.ttc, # 字体路径需根据系统调整width800,height600,max_words50,background_colorwhite)wc.generate_from_frequencies(word_freq)return wc# 主程序if __name__ __main__:# 路径设置text_path test.txtstopwords_path stopwords.txt# 执行流程text read_text(text_path)filtered_words process_text(text, stopwords_path)word_freq count_words(filtered_words)wc generate_wordcloud(word_freq)# 展示词云plt.imshow(wc)plt.axis(off)plt.show()完善后的自然语言处理程序一、程序功能模块拆分将所有功能拆分为独立函数主程序统一调用结构清晰、易于维护。1. 分词功能import jiebaimport jieba.posseg as psegfrom collections import Counterimport matplotlib.pyplot as pltimport pandas as pddef cut_text(text, user_dict_pathNone):# 加载自定义词典if user_dict_path:jieba.load_userdict(user_dict_path)# 分词words jieba.cut(text)return list(words)2. 词频统计功能def count_word_freq(words):return Counter(words)3. 词性分类保存TXT功能def save_pos_classification(text, save_path):words pseg.cut(text)pos_dict {}for word, flag in words:if flag not in pos_dict:pos_dict[flag] []pos_dict[flag].append(word)# 写入文件with open(save_path, w, encodingutf-8) as f:for pos, words_list in pos_dict.items():f.write(f{pos}: {,.join(words_list)}\n)4. 读取TXT生成饼状图def generate_pie_chart(txt_path):with open(txt_path, r, encodingutf-8) as f:text f.read()words cut_text(text)freq count_word_freq(words)# 取前5高频词top5 freq.most_common(5)labels [item[0] for item in top5]sizes [item[1] for item in top5]plt.pie(sizes, labelslabels, autopct%1.1f%%)Multiple) Fed) plugin))plt.title(Top 5 Word Frequency)plt.show()5. 柱状图可视化功能def generate_bar_chart(txt_path):with open(txt_path, r, encodingutf-8) as f:text f.read()words cut_text(text)freq count_word_freq(words)top10 freq.most_common(10)df pd.DataFrame(top10, columns[Word, Frequency])df.plot(kindbar, xWord, yFrequency, titleTop 10 Word Frequency)plt.show()6. 词云可视化功能from wordcloud import WordClouddef generate_wordcloud(txt_path, save_img_pathwordcloud.png):with open(txt_path, r, encodingutf-8) as f:text f.read()words cut_text(text)wc WordCloud(font_pathmsyh.ttc,width800,height600,background_colorwhite)wc.generate( .join(words))wc.to_file(save_img_path)plt.imshow(wc)plt.axis(off)plt.show()7. 手动添加自定义词典功能def add_custom_word(word):jieba.add_word(word)print(f已添加自定义词汇{word})8. 人名/地名/武器统计并保存TXTdef save_named_entity(text, entity_type, save_path):words pseg.cut(text)entities []# 词性标注对应人名(nr)、地名(ns)、可自定义武器相关词性pos_map {人名: nr, 地名: ns, 武器: n}target_pos pos_map.get(entity_type)for word, flag in words:if flag target_pos:entities.append(word)# 去重并保存entities list(set(entities))with open(save_path, w, encodingutf-8) as f:f.write(\n.join(entities))二、主程序调用示例if __name__ __main__:# 1. 读取文本txt_path input.txtwith open(txt_path, r, encodingutf-8) as f:text f.read()# 2. 手动添加自定义词典add_custom_word(人工智能)# 3. 分词words cut_text(text)# 4. 词频统计freq count_word_freq(words)# 5. 词性分类保存save_pos_classification(text, pos_classification.txt)# 6. 生成饼状图generate_pie_chart(txt_path)# 7. 生成柱状图generate_bar_chart(txt_path)# 8. 生成词云generate_wordcloud(txt_path)# 9. 统计并保存人名、地名、武器save_named_entity(text, 人名, names.txt)save_named_entity(text, 地名, places.txt)save_named_entity(text, 武器, weapons.txt)三、功能说明• 所有功能均封装为独立函数主程序按业务逻辑调用。• 支持自定义词典、多类型可视化、实体抽取与保存。• 代码模块化可单独扩展或替换某一功能。