Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to sort out the relationship in A Dream of Red Mansions

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to use Python to sort out the relationship in A Dream of Red Mansions, the content is concise and easy to understand, it can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Recently, I took the time to watch the Dream of Red Mansions again. I really can't praise her with words because of the classics in the classics. Today, I would like to use Python to sort out the relationships in the Dream of Red Mansions.

Do not ask me why it is a Dream of Red Mansions, rather than the three Kingdoms of the Water margin or Journey to the West, are all classics, but I personally prefer more classical books. A Dream of Red Mansions is also one of the few novels I have read many times, and I have the deepest feelings for it.

All right, these are not important, the important thing is that we are going to use Python to manage the relationship of Dream of Red Mansions!

Data preparation

A TXT document of A Dream of Red Mansions

Jinling Twelve Women + Jia Baoyu list of character names

The list of people is as follows:

Baoyu nr

Daiyu nr

Baochai nr

Xiangyun nr

Sister Feng nr

Li Wan nr

Yuanchun nr

Welcome Spring nr

Exploring the Spring nr

Xichun nr

Miaoyu nr

Sister Qiao nr

Qin's nr

This list, which is also used for participle, is followed by nr, which means a person's name.

Data processing.

Read the data and load the dictionary

With open ("A Dream of Red Mansions .txt", encoding='gb18030') as f: honglou = f.readlines () jieba.load_userdict ("renwu_forcut") renwu_data = pd.read_csv ("renwu_forcut", header=-1) mylist = [k [0] .split (") [0] for k in renwu_data.values.tolist ()]

In this way, we read the Dream of Red Mansions into the variable honglou, and also load our custom dictionary into the jieba library through load_userdict.

Segmenting and extracting the text

TmpNames = [] names = {} relationships = {} for h in honglou: h.replace ("Jia Fei", "Yuanchun") h.replace ("Li Gongcai" "Li Wan") poss = pseg.cut (h) tmpNames.append ([]) for w in poss: if w.flag! = 'nr' or len (w.word)! = 2 or w.word not in mylist: continue tmpNames [- 1] .append (w.word) if names.get (w.word) is None: names [w.word] = 0 relationships [w.word] = {} names [w.word] + = 1

First of all, because of the serious mixed use of "Jia Fei", "Yuan Chun", "Li Gong Cai" and "Li Wan" in the article, the replacement is done directly here.

Then use the pseg tool provided by the jieba library to do word segmentation, which returns the part of speech of each word.

After that, only the participles that meet the requirements and are in the list of dictionaries we provide will be retained.

Every time a person appears, one will be added to facilitate the determination of the node size of the character when drawing the diagram later.

For names that exist in our custom dictionary, save them to a temporary variable called tmpNames.

Deal with the relationship between people

For name in tmpNames: for name1 in name: for name2 in name: if name1 = = name2: continue if shipments [name1] .get (name2) is None: relationships [name1] [name2] = 1 else: relationships [name1] [name2] + = 1

For the characters who appear in the same paragraph, we think they are closely related, and each time they appear at the same time, the relationship increases by 1. 5%.

Save to Fil

With open ("relationship.csv", "w", encoding='utf-8') as f: f.write ("Source,Target,Weight\ n") for name, edges in relationships.items (): for v, w in edges.items (): f.write (name + "," + v + "," + str (w) + "\ n") with open ("NameNode.csv", "w", encoding='utf-8') as f: f.write ("ID,Label,Weight\ n") for name Times in names.items (): f.write (name + "," + name + "," + str (times) + "\ n")

File 1: the character relationship table, which contains the characters that appear first, the characters that appear later, and the number of times they appear together

Document 2: the figure proportion table, including the total number of occurrences of the character, the more the number of occurrences, the greater the proportion.

Make a relationship chart

Drawing with pyecharts

Def deal_graph (): relationship_data = pd.read_csv ('relationship.csv') namenode_data = pd.read_csv (' NameNode.csv') relationship_data_list = relationship_data.values.tolist () namenode_data_list = namenode_data.values.tolist () nodes = [] for node in namenode_data_list: if node [0] = "Baoyu": node [2] = node [2] / 3 nodes.append ({"name": node [0] "symbolSize": node [2] / 30}) links = [] for link in relationship_data_list: links.append ({"source": link [0], "target": link [1], "value": link [2]}) g = (Graph () .add (", nodes, links, repulsion=8000) .set _ global_opts (title_opts=opts.TitleOpts (title=" people Relations ")) return g

First, read the two files into a list.

For Baoyu, because its proportion is too large, if you scale it uniformly, it will cause the node of other characters to be too small and unattractive, so here is a zoom first.

The final diagram

The above content is how to use Python to sort out the relationship in A Dream of Red Mansions. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report