@@ -74,85 +74,53 @@ def highRatedSent(paraList):
74
74
highList = []
75
75
polarityList = paraList [1 ]
76
76
avgPol = percentToInt (paraList [0 ])
77
- avgNegative = avgPol [0 ]
78
- avgPositive = avgPol [2 ]
77
+ avgBias = avgPol [0 ]
78
+ avgNon = avgPol [1 ]
79
79
for tupIndex in range (len (polarityList )):
80
- if polarityList [tupIndex ][2 ] * 100 > avgPositive :
81
- highList .append ("+" + "Above average positive text" )
82
- highList .append (paraList [tupIndex + 2 ])
83
- elif polarityList [tupIndex ][0 ] * 100 > avgNegative :
84
- highList .append ("-" + "Above average negative text" )
80
+ if polarityList [tupIndex ][0 ] == "Bias" and polarityList [tupIndex ][1 ] * 100 > avgBias :
85
81
highList .append (paraList [tupIndex + 2 ])
86
82
return highList
87
83
def polarityRating (list , link ):
88
84
analyzer = SentimentIntensityAnalyzer ()
89
85
90
86
wantedText = webScrape (link )
91
87
# Declare variables for average polarity
92
- totNegative = 0
93
- totNeutral = 0
94
- totPositive = 0
95
88
count = 0
96
- total = 0.00
97
- val = 0.00
89
+ polarityCount = 0
98
90
temp = ''
99
- sentPolarityList = []
91
+ totBias = 0
92
+ totNon = 0
93
+ totPos = 0
94
+ totNeg = 0
95
+ sentBiasList = []
100
96
for para in wantedText :
101
97
sentences = sent_tokenize (para .text .strip ())
102
98
sentInPara = ""
103
99
for sent in sentences :
104
- # tagged = nltk.pos_tag(word_tokenize(sent))
105
100
if sent != "" :
106
- # tagged = analyzer.polarity_scores(sent)
107
- tagged = {'pos' : [], 'neu' : [], 'neg' : []}
108
101
list .append (sent )
109
102
temp = classifier (sent )[0 ]
110
- if temp ["label" ]== "Non-biased" :
111
- total += temp ["score" ]
112
- val = temp ["score" ]
103
+ if temp ['label' ] == "Non-biased" :
104
+ sentBiasList .append (("Non-biased" , temp ["score" ]))
105
+ totBias += 1 - temp ["score" ]
106
+ totNon += temp ["score" ]
113
107
else :
114
- total += 1 - temp ["score" ]
115
- val = 1 - temp ["score" ]
116
- if val < .40 :
117
- if 'pos' in tagged .keys ():
118
- tagged ['pos' ].append (0.00 )
119
- if 'neg' in tagged .keys ():
120
- tagged ['neg' ].append (val )
121
- if 'neu' in tagged .keys ():
122
- tagged ['neu' ].append (0.00 )
123
- elif val >= .40 and val <= .60 :
124
- if 'pos' in tagged .keys ():
125
- tagged ['pos' ].append (0.00 )
126
- if 'neg' in tagged .keys ():
127
- tagged ['neg' ].append (0.00 )
128
- if 'neu' in tagged .keys ():
129
- tagged ['neu' ].append (val )
130
- elif val > .60 :
131
- if 'pos' in tagged .keys ():
132
- tagged ['pos' ].append (val )
133
- if 'neg' in tagged .keys ():
134
- tagged ['neg' ].append (0.00 )
135
- if 'neu' in tagged .keys ():
136
- tagged ['neu' ].append (0.00 )
137
- totNegative += tagged ["neg" ][- 1 ]
138
- totNeutral += tagged ["neu" ][- 1 ]
139
- totPositive += tagged ["pos" ][- 1 ]
140
- #Appends polarity of each sentence
141
- sentPolarityList .append ((tagged ["neg" ][- 1 ], tagged ["neu" ][- 1 ], tagged ["pos" ][- 1 ]))
108
+ tagged = analyzer .polarity_scores (sent )
109
+ if tagged ["pos" ] > tagged ['neg' ]:
110
+ sentBiasList .append (("Bias" , temp ["score" ], "pos" ))
111
+ else :
112
+ sentBiasList .append (("Bias" , temp ["score" ], "neg" ))
113
+
114
+ totBias += temp ["score" ]
115
+ totNon += 1 - temp ["score" ]
116
+ totPos , totNeg = totPos + tagged ["pos" ], totNeg + tagged ["neg" ]
117
+ polarityCount += 1
142
118
# Adds to the total polarity based on the key of the dictionary
143
119
count += 1
144
- #Will stop once reaches the end of the article (Reuters)
145
- if "Reporting by" in sentInPara or "Get all the stories you need" in sentInPara :
146
- break
147
-
148
- rtotal = total / count
149
- list .insert (0 , sentPolarityList )
150
- k = totPositive + totNegative + totNeutral
151
- scale_value = (2 + (2 * (100 - k ))/ k )/ 2
152
- #In the end, the average polarity score of the article is added (variables for polarity meter)
153
- list1 = [str (round ((totNegative * scale_value ), 1 )) + "%" ,str (round ((totNeutral * scale_value ), 1 )) + "%" ,str (round ((totPositive * scale_value ), 1 )) + "%" ]
154
- #list1 = [str(l) + "%",str(k) + "%",str(m) + "%"]
155
- list1 .append (round ((1 - rtotal ),4 ))
120
+ list .insert (0 , sentBiasList )
121
+ overallList = [str (round (totBias / count * 100 , 1 )) + "%" , str (round (totNon / count * 100 , 1 )) + "%" , str (round (totPos / polarityCount * 100 , 1 )) + "%" , str (round (totNeg / polarityCount * 100 , 1 )) + "%" ]
156
122
if count != 0 :
157
- list .insert (0 ,list1 )
158
-
123
+ list .insert (0 ,overallList )
124
+ list1 = []
125
+ polarityRating (list1 , "https://abcnews.go.com/Politics/trump-defendants-post-bond-full-464-million-judgment/story?id=108031376" )
126
+ print (list1 )
0 commit comments