这个项目展示了如何用Python对四大名著进行全方位的自然语言处理(NLP)分析。作为一名长期从事文本分析的程序员,我发现古典文学文本蕴含着丰富的信息,通过NLP技术可以挖掘出许多有趣的发现。
项目核心功能包括分词、词频统计、词性分析、实体识别和多种可视化展示。特别值得一提的是,针对古典文学中特有的专有名词(如"金陵十二钗"、"诸葛孔明"等),我们实现了自定义词典功能,大大提高了分析的准确性。
中文分词是NLP的基础环节。我们使用jieba库进行分词,这是目前最优秀的中文分词工具之一。针对古典文学中的特殊词汇,项目实现了自定义词典功能:
python复制def word_segmentation(text, user_dict=None):
if user_dict:
for word in user_dict:
jieba.add_word(word) # 添加自定义词汇
words = jieba.lcut(text) # 精确模式分词
return words
在实际应用中,我发现对于《红楼梦》这样的文本,如果不添加自定义词典,"贾宝玉"可能会被错误地分成"贾"和"宝玉"。通过预先添加这些专有名词,分词准确率可以提升30%以上。
词频统计看似简单,但要做好需要考虑很多细节。我们使用collections.Counter进行高效统计,并设计了停用词过滤机制:
python复制def word_frequency(words, top_n=20):
stop_words = set(['的', '了', '在', '是', '我', '有', '和', '也', '就', '都'])
filtered_words = [word for word in words
if word not in stop_words and len(word) > 1]
freq_counter = Counter(filtered_words)
return freq_counter.most_common(top_n)
这里有两个关键点:
通过jieba的posseg模块,我们可以同时获取词语及其词性:
python复制def pos_classification_save(words, output_file):
pos_result = {}
pos_tags = pseg.lcut(' '.join(words))
for word, pos in pos_tags:
if pos not in pos_result:
pos_result[pos] = []
pos_result[pos].append(word)
# 保存结果到文件...
实体识别是分析名著的重要环节。我们特别关注人名(nr)、地名(ns)等实体:
python复制def entity_statistics_save(words, pos_tag, output_file):
pos_tags = pseg.lcut(' '.join(words))
entities = [word for word, pos in pos_tags if pos == pos_tag]
entity_counter = Counter(entities)
# 保存实体统计结果...
词云是最直观的文本可视化方式之一。我们使用wordcloud库生成词云图:
python复制def generate_wordcloud(words, output_file):
filtered_text = ' '.join([word for word in words
if word not in stop_words and len(word) > 1])
wc = WordCloud(font_path='simhei.ttf',
background_color='white',
width=800, height=600,
max_words=100)
wc.generate(filtered_text)
wc.to_file(output_file)
关键参数说明:
使用networkx库可以构建词语关系网络图:
python复制def generate_relation_graph(words, title, top_n=10):
co_occurrence = {}
for i in range(len(words)-1):
word1, word2 = words[i], words[i+1]
if word1 not in co_occurrence:
co_occurrence[word1] = {}
if word2 not in co_occurrence[word1]:
co_occurrence[word1][word2] = 0
co_occurrence[word1][word2] += 1
G = nx.Graph()
for word1, neighbors in co_occurrence.items():
for word2, count in neighbors.items():
G.add_edge(word1, word2, weight=count)
# 绘制图形...
这种共现关系图特别适合分析小说中人物关系。例如在《红楼梦》中,可以清晰看到主要人物之间的关联强度。
建议使用纯净的txt格式文本。可以从古腾堡计划等公开资源获取四大名著文本。需要注意:
主程序逻辑清晰,遵循典型的数据处理流程:
python复制if __name__ == "__main__":
# 1. 读取文本
with open('hongloumeng.txt', 'r', encoding='utf-8') as f:
text = f.read()
# 2. 自定义词典
user_dict = ['贾宝玉', '林黛玉', '薛宝钗', '金陵十二钗', '荣国府']
# 3. 分词处理
words = word_segmentation(text, user_dict)
# 4. 各种分析功能
freq_data = word_frequency(words)
pos_classification_save(words, 'pos_result.txt')
generate_bar_chart(freq_data, '《红楼梦》词频统计')
# 更多分析...
以《红楼梦》为例,分析结果可能包括:
这些数据可以为文学研究提供量化支持。
OpenClaw是一个开源的AI代理框架,可以集成多种AI模型。Codex是OpenAI推出的代码生成模型,特别擅长根据自然语言描述生成代码。
两者的结合可以打造强大的嵌入式编程助手,直接在编辑器中提供智能编程支持。
配置步骤如下:
bash复制git clone https://github.com/openclaw-team/openclaw.git
cd openclaw
python -m venv venv
source venv/bin/activate # Linux/Mac
pip install -r requirements.txt
bash复制acpx codex "编写Python实现快速排序的函数,包含详细注释"
bash复制acpx codex "分析以下代码的bug并修复:[粘贴代码]"
bash复制acpx codex "为上述函数生成详细的技术文档"
NLP技术经历了三个主要阶段:
基于规则的方法(1950s-1990s)
统计学习方法(1990s-2010s)
深度学习时代(2010s至今)
NLP主要任务可分为以下几类:
文本分类
序列标注
文本生成
语义理解
智能客服
内容审核
金融分析
医疗健康
对于长篇文本,可以考虑以下优化措施:
健壮的生产代码需要考虑:
中文显示问题:
图形布局:
色彩选择:
指令设计:
结果验证:
效率平衡: