Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import plotly.plotly as py
- from collections import Counter
- import plotly.graph_objs as go
- import matplotlib.pyplot as plt
- my_file = open("messages.txt", "r")
- lines = my_file.readlines()
- x = 0
- print("Total messages: ", len(lines))
- messages = [] #Messages is a list of dictionaries
- x = 0
- for i in lines:
- m = {'id': x}
- try:
- if i[0] in ['1','2','3','4','5','6','7','8','9']:
- m['date'] = i.split()[0].lower()
- m['time'] = i.split()[1].lower()
- m['hour'] = i.split()[1].split(':')[0].lower()
- m['name'] = i.split()[3].lower()
- if(i.split()[3].lower() == '\u200ecarla'):
- m['name'] ='carla'
- if(i.split()[3].lower() == '\u200emateu'):
- m['name'] ='mateu:'
- if(i.split()[3].lower() == '\u200eyara'):
- m['name'] ='yara'
- if(i.split()[3].lower() == '\u200erabo'):
- m['name'] ='rabo:'
- if(i.split()[3].lower() == '\u202a+34' or i.split()[3].lower() == '\u200e\u202a+34'):
- m['name'] ='helena'
- m['text'] = i.split(':')[2][1:].lower()#The second parameter is to omit the spaces
- x += 1
- messages.append(m)
- except:
- #print("Error at line ", x)
- pass
- lines = [] #Deletes the list to clear memory
- print("Total messages: ", len(messages))
- def mostCommon(lst, size):
- data = Counter(lst)
- return data.most_common(size)
- text = [] #A list with all the words
- for i in messages:
- for j in i['text'].split():
- text.append(j)
- #print("Most frequent words: ", mostCommon(text, 200)[:100])
- for i in mostCommon(text, 500):
- if len(i[0])>=4:
- print(i[0], " : ", i[1], end=' | ')
- for i in messages[:50]:
- print(i)
- ## Pie plot!!
- names = []
- for i in messages:
- if i['name'] not in names:
- names.append(i['name'])
- else:
- pass
- print(names)
- pie = {}
- for i in names:
- pie[i] = 0
- for i in messages:
- pie[i['name']] += 1
- print(pie)
- #labels = list(pie.keys())
- labels2 = ['Ramon', 'Yara', 'Marco', 'Alex', 'Girbau', 'Xabi', 'Claudia', 'Barri', 'Ferran', 'Helena', 'Biel', 'Carla', 'Diego', 'Mateu', 'Taba', 'Rius', 'Marc', 'Pol']
- values = list(pie.values())
- fig1, ax1 = plt.subplots()
- ax1.pie(values, labels=labels2, startangle=90,rotatelabels=True, autopct=lambda x: int(x*40000/100))
- ax1.axis('equal')
- plt.savefig('plot.png', dpi=1000)
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement