Advertisement
Guest User

Untitled

a guest
Feb 16th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. import plotly.plotly as py
  2. from collections import Counter
  3. import plotly.graph_objs as go
  4. import matplotlib.pyplot as plt
  5.  
  6.  
  7. my_file = open("messages.txt", "r")
  8.  
  9. lines = my_file.readlines()
  10. x = 0
  11. print("Total messages: ", len(lines))
  12.  
  13. messages = [] #Messages is a list of dictionaries
  14.  
  15. x = 0
  16. for i in lines:
  17. m = {'id': x}
  18. try:
  19. if i[0] in ['1','2','3','4','5','6','7','8','9']:
  20. m['date'] = i.split()[0].lower()
  21. m['time'] = i.split()[1].lower()
  22. m['hour'] = i.split()[1].split(':')[0].lower()
  23. m['name'] = i.split()[3].lower()
  24. if(i.split()[3].lower() == '\u200ecarla'):
  25. m['name'] ='carla'
  26. if(i.split()[3].lower() == '\u200emateu'):
  27. m['name'] ='mateu:'
  28. if(i.split()[3].lower() == '\u200eyara'):
  29. m['name'] ='yara'
  30. if(i.split()[3].lower() == '\u200erabo'):
  31. m['name'] ='rabo:'
  32. if(i.split()[3].lower() == '\u202a+34' or i.split()[3].lower() == '\u200e\u202a+34'):
  33. m['name'] ='helena'
  34. m['text'] = i.split(':')[2][1:].lower()#The second parameter is to omit the spaces
  35. x += 1
  36. messages.append(m)
  37. except:
  38. #print("Error at line ", x)
  39. pass
  40.  
  41. lines = [] #Deletes the list to clear memory
  42.  
  43. print("Total messages: ", len(messages))
  44.  
  45. def mostCommon(lst, size):
  46. data = Counter(lst)
  47. return data.most_common(size)
  48.  
  49. text = [] #A list with all the words
  50. for i in messages:
  51. for j in i['text'].split():
  52. text.append(j)
  53.  
  54. #print("Most frequent words: ", mostCommon(text, 200)[:100])
  55. for i in mostCommon(text, 500):
  56. if len(i[0])>=4:
  57. print(i[0], " : ", i[1], end=' | ')
  58.  
  59. for i in messages[:50]:
  60. print(i)
  61.  
  62. ## Pie plot!!
  63.  
  64. names = []
  65. for i in messages:
  66. if i['name'] not in names:
  67. names.append(i['name'])
  68. else:
  69. pass
  70. print(names)
  71.  
  72. pie = {}
  73. for i in names:
  74. pie[i] = 0
  75.  
  76. for i in messages:
  77. pie[i['name']] += 1
  78.  
  79. print(pie)
  80.  
  81. #labels = list(pie.keys())
  82. labels2 = ['Ramon', 'Yara', 'Marco', 'Alex', 'Girbau', 'Xabi', 'Claudia', 'Barri', 'Ferran', 'Helena', 'Biel', 'Carla', 'Diego', 'Mateu', 'Taba', 'Rius', 'Marc', 'Pol']
  83. values = list(pie.values())
  84. fig1, ax1 = plt.subplots()
  85. ax1.pie(values, labels=labels2, startangle=90,rotatelabels=True, autopct=lambda x: int(x*40000/100))
  86. ax1.axis('equal')
  87. plt.savefig('plot.png', dpi=1000)
  88. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement