forked from abderraouf2che/Hate-Speech-Detector-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnegationClass.py
144 lines (115 loc) · 4.21 KB
/
negationClass.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
import nltk
from nltk import word_tokenize, pos_tag
from nltk.corpus import wordnet
## get Antonyms using Wordnet #
def get_antonyms(word):
antonyms=[]
for syn in wordnet.synsets(word):
for l in syn.lemmas():
if l.antonyms():
antonyms.append(l.antonyms()[0].name())
return sorted(set(antonyms))
##### Simple Negation with posatag ###
def invert_simple(sent):
text = word_tokenize(sent)
tags=pos_tag(text)
new_sent=[]
for word in tags:
if word[0] not in ['not','n\'t']:
new_sent.append(word[0])
new_sent=" ".join(new_sent)
return new_sent
'''
example:
sent="i don't like cooking, it isn't good"
print(invert_simple(sent))
'''
from nltk.stem.wordnet import WordNetLemmatizer
from thesaurus import Word
from nltk import word_tokenize, pos_tag
sentence='He told us a very exciting adventure story.'
def sent_negation(sentence):
opinion_list=['think','thinks','agree','disagree','agrees','agreed','disagrees','thought','thought']
lem=WordNetLemmatizer()
tags3=pos_tag(word_tokenize(sentence))
tags3.append((" "," "))
new_st=[]
for i,word in enumerate(tags3):
try:
if word[1]=='VBP':
if word[0] in opinion_list:
new_st.append(word[0])
elif word[0] in ['am','are','have',"'m","'re","'ve","ve","m",'re','im']:
if tags3[i+1][1]=='JJ':
new_st.append(word[0])
pass
elif tags3[i+1][0] in ['not','n\'t']:
new_st.append(word[0])
else:
new_st.append(word[0])
new_st.append('not')
elif word[0] in ['do','did']:
if tags3[i+1][0] in ['not','n\'t']:
new_st.append(word[0])
else:
new_st.append(word[0])
new_st.append('not')
else:
w=word[0]
a=get_antonyms(w)
if len(a)>0:
new_st.append(a[0])
else:
new_st.append('do not')
new_st.append(word[0])
elif word[1]=='JJ':
a=get_antonyms(word[0])
if word[0]=='i':
new_st.append(word[0])
elif len(a)>0:
new_st.append(a[0])
pass
else:
if tags3[i-1][0]=='the':
new_st.append('non')
new_st.append(word[0])
else:
new_st.append('not')
new_st.append(word[0])
elif word[1]=='VBZ':
if word[0] in ["'s",'is','has','does']:
if tags3[i+1][1]=='JJ':
new_st.append(word[0])
pass
elif tags3[i+1][0] in ['not','n\'t']:
new_st.append(word[0])
else:
new_st.append(word[0])
new_st.append('not')
else:
new_st.append('does not')
new_st.append(lem.lemmatize(word[0],"v"))
elif word[1]=='VBD':
if word[0] in ['was','were']:
if tags3[i+1][1]=='JJ':
new_st.append(word[0])
pass
else:
new_st.append(word[0])
new_st.append('not')
else:
new_st.append('did not')
new_st.append(lem.lemmatize(word[0],"v"))
elif word[1]=='MD':
if tags3[i+1][0] in ['not','n\'t']:
new_st.append(word[0])
else:
new_st.append(word[0])
new_st.append('not')
elif word[0] in ['not','n\'t']:
pass
else:
new_st.append(tags3[i][0])
except:
pass
return " ".join(new_st)