python activity1

Use Twitter Messages to analyze people’s emotions

Introduction and my thoughts

Automatic analysis of the emotions people express in their blogs seems to be a distant topic, which involves not only programming to control machines and language recognition, but also how to combine the two. And when the topic came up with Python, it became easier. The method we use is to look for emotive keywords in a blog and assign a score to them, then calculate the score to determine the mood of the blogger. Although it is the simplest model, it is a useful method; And, at the same time, it’s a great way to familiarize myself with Python – something I can only master with practice. When I pressed the “run” button, thousands of scores appeared in front of my eyes, and all the emotional or humorous or excited words were digitized and turned into scores.

If you think about it, what’s the use of analyzing your mood? It has many USES, such as analyzing the comments on an article, whether they are positive or negative, so that we can quickly figure out how well the article is doing, and then we can easily improve it. The same is true for a commodity, and so on and so forth. But it also begs the question: Can machines really analyze people?It’s just that these simple words don’t even make it clear whether the word describes them or something else.

More deeply, even though the same words may have different meanings, like irony, the model is indistinguishable. So if you want to refine this function, you have to have other discriminative conditions, not just simple words. Not only is there a long way to go, but the only way to enhance its function is to accumulate experience, and participating in such activities is a good way.

steps

1
2
3
4
5
6
7
8
9
10
11
jing=[]
wei=[]
words=[]
with open(".\tweets") as f: #open the file and read in the messages with their position
tweets=f.readlines()
for i in range(len(tweets)):
tweets[i]=tweets[i].replace("\n","").split(" ")
tweets[i]=[s.lower() for s in tweets[i]]
jing.append(tweets[i][1].replace("]","")) #get rid of some useless sign
wei.append(tweets[i][0].replace("[","").replace(",",""))
words.append(tweets[i][5:])
1
2
3
4
5
6
7
8
#read in the keywords with the value they have to make a dictionary
with open(".\keywords") as f:
lines=f.readlines()
keywords={}
for i in range(len(lines)):
lines[i]=lines[i].replace("\n","").split(",")
keywords[lines[i][0]] = int(lines[i][1])
print(keywords)

{‘alone’: 1, ‘amazed’: 10, ‘amazing’: 10, ‘bad’: 1, ‘best’: 10, ‘better’: 10, ‘excellent’: 10, ‘excited’: 10, ‘excite’: 10, ‘excites’: 10, ‘exciting’: 10, ‘glad’: 10, ‘god’: 5, ‘good’: 7, ‘great’: 7, ‘greatest’: 10, ‘haha’: 5, ‘hahaha’: 5, ‘happy’: 10, ‘hate’: 1, ‘hurt’: 1, ‘hurts’: 1, ‘hurting’: 1, ‘like’: 7, ‘likes’: 7, ‘liked’: 7, ‘lol’: 5, ‘lonely’: 1, ‘love’: 10, ‘loves’: 10, ‘loved’: 10, ‘loving’: 10, ‘lovin’: 10, ‘need’: 1, ‘needs’: 1, ‘negative’: 1, ‘please’: 5, ‘positive’: 7, ‘pretty’: 10, ‘prettiest’: 10, ‘regret’: 1, ‘regrets’: 1, ‘ruin’: 1, ‘ruins’: 1, ‘ruined’: 1, ‘sad’: 1, ‘save’: 5, ‘saves’: 5, ‘saved’: 5, ‘sorry’: 1, ‘thank’: 5, ‘thanks’: 5, ‘tired’: 1, ‘worst’: 1}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#compare each words with keywords and calculate the score each message gets
for i in range(len(words)):
for j in range(len(words[i])):
w=list(words[i][j])
for n in w:
if not n.isalpha():
w.remove(n)
words[i][j]="".join(w)

grade=[]
for i in range(len(words)):
s=0
for j in range(len(words[i])):
if words[i][j] in keywords:
s=s+keywords[words[i][j]]
grade.append(s)
1
2
3
4
#get their longitude and latitude in one list
total=[]
for i in range(len(words)):
total.append([wei[i],jing[i],grade[i]])

finally we can calculate the average score in different areas:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
s1=0
s2=0
s3=0
s4=0
s5=0
c1=0
c2=0
c3=0
c4=0
c5=0
for i in range(len(tweets)):
if float(total[i][0])<49.189787 and float(total[i][0])>24.660845:
if float(total[i][1])<-67.444574 and float(total[i][1])>-87.518395:
s1=s1+total[i][2]
c1=c1+1
elif float(total[i][1])<-87.518395 and float(total[i][1])>-101.998892:
s2=s2+total[i][2]
c2=c2+1
elif float(total[i][1])<-101.998892 and float(total[i][1])>-115.236428:
s3=s3+total[i][2]
c3=c3+1
elif float(total[i][1])<-115.236428 and float(total[i][1])>-125.242264:
s4=s4+total[i][2]
c4=c4+1
else:
s5=s5+total[i][2]
c5=c5+1
else:
s5=s5+total[i][2]
c5=c5+1

a1=s1/c1
a2=s2/c2
a3=s3/c3
a4=s4/c4
a5=s5/c5
print(s1,s2,s3,s4,s5)

6920 2970 653 1767 501