-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
145 lines (109 loc) · 3.63 KB
/
app.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# -*- coding: utf-8 -*-
import time
import argparse
import io
import json
import os
from google.cloud import language
from google.cloud.language import enums
from google.cloud.language import types
poffensive = 0
loffensive = 0
poffensive_list = []
loffensive_list = []
client = language.LanguageServiceClient()
file1 = open("final.txt","w")
file1.close()
f=open("lastoutput.txt","w+")
f.close()
fo=open("lastoutput.txt","w")
fo.close()
def classify(text, verbose=True):
language_client = language.LanguageServiceClient()
document = language.types.Document(
content=text,
type=language.enums.Document.Type.PLAIN_TEXT)
response = language_client.classify_text(document)
categories = response.categories
result = {}
for category in categories:
# Turn the categories into a dictionary of the form:
# {category.name: category.confidence}, so that they can
# be treated as a sparse vector.
result[category.name] = category.confidence
return result
with open("outputone.txt","r") as o:
r=o.readlines()
i=0
total = 0
neutral=0
while i<len(r):
text= str(r[i]).rstrip('\n')
s = text.split(" ")
text2=""
for v in s:
if "@" in v:
s.remove(v)
for v in s:
if "@" in v:
s.remove(v)
for v in s:
text2=text2+" "+v
text=text2[1:]
text1 = text
while len(text.split(" "))<=20:
text = text+" "+text1
document = types.Document(
content=text,
type=enums.Document.Type.PLAIN_TEXT)
sentiment = client.analyze_sentiment(document=document).document_sentiment
analysis = classify(text)
if analysis:
for key in analysis:
with open("lastoutput.txt","a+") as w:
print "category: "+key+" "
print "confidence: "+str(analysis[key])
w.write("category: "+key+" ")
w.write("confidence: "+str(analysis[key]))
w.write("\n")
if (("people & society" in key.lower() or "sensitive subjects" in key.lower()) and (float(analysis[key])<0.5 and (sentiment.score>-0.25 and sentiment.score<0.25))):
poffensive+=1
poffensive_list.append(text1)
elif (("people & society" in key.lower() or "sensitive subjects" in key.lower()) and ((float(analysis[key])>=0.5) and sentiment.score<=-0.25)) or "adult" in key.lower():
loffensive+=1
loffensive_list.append(text1)
else:
pass
neutral+=1
print (i)
total+=1
else:
print "error"
with open("lastoutput.txt","a+") as w:
w.write("\n")
i+=1
percent_poffensive = str(round((float(poffensive)/total)*100))
percent_loffensive = str(round((float(loffensive)/total)*100))
percent_neut = str(round((float(neutral)/total)*100))
with open("final.txt","a+") as w:
w.write("\n\nContent Distribution: ")
w.write("\nPercent potentially offensive: "+percent_poffensive)
w.write("\nPercent likely offensive: "+percent_loffensive)
w.write("\nPercent neutral: "+percent_neut)
w.write("\n===========================")
w.write(("\n\nPotentially offensive:\n=========================="))
for v in poffensive_list:
w.write("\n"+v)
w.write(("\n\nLikely offensive:\n=========================="))
for v in loffensive_list:
w.write("\n"+v)
print ("------------")
print (total)
print(poffensive)
print (loffensive)
print ("------------")
print ("Profanity list: ")
print ("Potentially offensive list: ")
print (poffensive_list)
print ("Likely offensive list: ")
print (loffensive_list)