In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to use Python to export and analyze chat records, with a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
Export chat records and generate word clouds to see what you talked about with your date.
1. Export chat history to open message manager
The format of the export is in txt format (the path I choose to export here is the desktop, so I generate a .txt file containing chat records on the desktop)
two。 The text that is framed in the code diagram is something we don't need (for example, the picture will be shown as [picture] and the emoticon will be displayed as [emoji]), so we replace it, and I use the rule here:
String = open (ritual C:\\ Users\\ 11768\\ Desktop\\ message record .txt', 'rushing Gravity encodingskills ℳ8'). Read () s = re.compile (' 2020.+ Yanzi | 2020.+ ✎ "ℳ" big big pot, | facial expression | picture | 2019.+ foreign boy | 2019.+ ✎ "ℳbig big pot, | withdraw a message | system message') # write the regular expression message = re.sub String) # replace the corresponding string as an empty string
Then we remove the special characters from the processed text.
Def getText (text): # this function is used to replace the special characters txt = textfor ch in'! "# $% & () * +, -. /:; @ [\\] ^'{|} ~,.,:': txt = txt.replace (ch,") # replace the special characters in the text with spaces return txtmessage = getText (message)
Use jieba word segmentation and generate word clouds
Split_message = jieba.lcut (message) wordcloud_txt = '.join (split_message) w=wordcloud.WordCloud (background_color= "white", font_path='./fonts/simhei.ttf',width=1600,height=800,max_words=2000) # set the parameters to generate the word cloud, background_color specifies the background color of the image, and # font_path sets the Chinese font, otherwise the Chinese will not be displayed. # width=1600,height=800 specifies the width and height pixels of the picture, respectively. # max_words specifies that the maximum number of words to generate Ciyun is 2000 words # and there are many optional parameters. You can pass the text w.to_file ("chat transcript Ciyun .png") # to Ciyun on your own Baidu w.generate (wordcloud_txt) # and finally generate pictures of Ciyun.
3. The resulting result:
4. Complete code:
Import reimport jiebaimport wordclouddef getText (text): # this function is used to replace the special character txt = textfor ch in'! "# $% & () * +, -. /:; @ [\] ^ _'{|} ~, which appears in the text. ,:': txt = txt.replace (ch, ") # replace the special characters in the text with the space return txtstring = open (ritual C:\ Users\ 11768\ Desktop\ message record .txt','r' Encoding='utf-8') .read () s = re.compile ('2020.+ foreign boy | 2020.+ ✎ "ℳ" big big pot, | expression | Picture | 2019.+ foreign boy | 2019.+ ✎ "ℳ" big big pot, | withdraw a message | system message') message = re.sub (sjingzhongjia string) message = getText (message) split_message = jieba.lcut (message) wordcloud_txt = '.join (split_message) w=wordcloud.WordCloud (background_color= "white") Font_path='./fonts/simhei.ttf',width=1600,height=800,max_words=2000) # set parameters for generating word cloud w.generate (wordcloud_txt) # pass text w.to_file ("chat transcript word cloud. PNG") # finally generate pictures of word cloud thank you for reading this article carefully I hope the article "how to use Python to export and analyze chat records" shared by the editor will be helpful to you. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.