Skip to content

Commit 5363196

Browse files
committed
Changed up how bias detection works in polarityRating: if a sentence is rated bias, the program will also check whether more positive or negative words are used. Fixed up highRatedSent to be compatible with changes in polarityRating.
1 parent e08988e commit 5363196

File tree

2 files changed

+29
-61
lines changed

2 files changed

+29
-61
lines changed

db.sqlite3

0 Bytes
Binary file not shown.

nlpFiles/getTextFromWeb.py

+29-61
Original file line numberDiff line numberDiff line change
@@ -74,85 +74,53 @@ def highRatedSent(paraList):
7474
highList = []
7575
polarityList = paraList[1]
7676
avgPol = percentToInt(paraList[0])
77-
avgNegative = avgPol[0]
78-
avgPositive = avgPol[2]
77+
avgBias = avgPol[0]
78+
avgNon = avgPol[1]
7979
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:
8581
highList.append(paraList[tupIndex + 2])
8682
return highList
8783
def polarityRating(list, link):
8884
analyzer = SentimentIntensityAnalyzer()
8985

9086
wantedText = webScrape(link)
9187
# Declare variables for average polarity
92-
totNegative = 0
93-
totNeutral = 0
94-
totPositive = 0
9588
count = 0
96-
total = 0.00
97-
val = 0.00
89+
polarityCount = 0
9890
temp=''
99-
sentPolarityList = []
91+
totBias = 0
92+
totNon = 0
93+
totPos = 0
94+
totNeg = 0
95+
sentBiasList = []
10096
for para in wantedText:
10197
sentences = sent_tokenize(para.text.strip())
10298
sentInPara = ""
10399
for sent in sentences:
104-
# tagged = nltk.pos_tag(word_tokenize(sent))
105100
if sent != "":
106-
# tagged = analyzer.polarity_scores(sent)
107-
tagged = {'pos': [], 'neu': [], 'neg': []}
108101
list.append(sent)
109102
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"]
113107
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
142118
# Adds to the total polarity based on the key of the dictionary
143119
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)) + "%"]
156122
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

Comments
 (0)