Anis Taluqdar commited on
Commit
08d712b
·
1 Parent(s): 874e5e7

Add requirements

Browse files
Files changed (2) hide show
  1. app.py +116 -0
  2. requirements.txt +0 -0
app.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import nltk
3
+ import re
4
+ nltk.download('punkt')
5
+ nltk.download('wordnet')
6
+ from nltk.corpus import wordnet
7
+ nltk.download("averaged_perceptron_tagger")
8
+ nltk.download('wordnet')
9
+
10
+
11
+ def find_antonyms(word, tag):
12
+ if tag.startswith('JJ') or (tag.startswith('RB') and word not in ["not", "no"]) or (tag.startswith('VB') and word not in ["be", "have", "do", "will", "shall", "are", "is", "was", "were"]):
13
+ antonyms = []
14
+ for syn in wordnet.synsets(word):
15
+ for lemma in syn.lemmas():
16
+ if lemma.antonyms():
17
+ antonyms.append(lemma.antonyms()[0].name())
18
+
19
+ return antonyms
20
+
21
+ return []
22
+
23
+
24
+ def insert_not(sentence):
25
+ words = nltk.word_tokenize(sentence)
26
+ pos_tags = nltk.pos_tag(words)
27
+
28
+ aux_verbs = ["be", "have", "do", "will", "shall", "are", "is", "was", "were", "am", "does", "has", "had", "can", "could", "may", "might", "must", "should", "would"]
29
+
30
+ new_words = []
31
+ #skip_next = False
32
+
33
+ flag = False
34
+
35
+ for i, (word, tag) in enumerate (pos_tags):
36
+ new_words.append(word)
37
+ if word.lower() in aux_verbs:
38
+ if i + 1 < len(words) and words[i+1].lower() != 'not' and not flag:
39
+ new_words.append("not")
40
+ flag = True
41
+
42
+ return " ".join(new_words)
43
+
44
+
45
+ def modify_sentence(sentence):
46
+ words = nltk.word_tokenize(sentence)
47
+ pos_tagger = nltk.pos_tag(words)
48
+
49
+ new_sentence = insert_not(sentence)
50
+
51
+ if len(new_sentence) != len(sentence):
52
+ return new_sentence
53
+ else:
54
+ new_words = []
55
+ for word, tag in pos_tagger:
56
+ antonyms = find_antonyms(word, tag)
57
+ if antonyms:
58
+ new_word = antonyms[0]
59
+ else:
60
+ new_word = word
61
+
62
+ new_words.append(new_word)
63
+
64
+ new_sentence = " ".join(new_words)
65
+ # print(sentence)
66
+ # print(new_sentence)
67
+
68
+ if new_sentence == sentence:
69
+ new_sentence = insert_not(sentence)
70
+
71
+ return new_sentence
72
+
73
+
74
+
75
+ def remove_no_not(sentence):
76
+ words_to_remove = ['no', 'not']
77
+
78
+ pattern = r'\b(' + '|'.join(words_to_remove) + r')\b'
79
+ cleaned_sentence = re.sub(pattern, '', sentence)
80
+
81
+ cleaned_sentence = re.sub(r'\s+', ' ', cleaned_sentence).strip()
82
+ # print(cleaned_sentence)
83
+ return cleaned_sentence
84
+
85
+
86
+
87
+ def clean_text(text):
88
+ text = re.sub(r'[^\w\s]', '', text)
89
+ text = text.strip()
90
+
91
+ return text
92
+
93
+ def main_func(sentence):
94
+ sentence = clean_text(sentence)
95
+
96
+ removed_no_sentence = remove_no_not(sentence)
97
+ # print(removed_no_sentence)
98
+
99
+ if len(removed_no_sentence) != len(sentence):
100
+ print("Original:", sentence)
101
+ print("Modified:", removed_no_sentence)
102
+ return removed_no_sentence
103
+ else:
104
+ modified_sentence = modify_sentence(sentence)
105
+ print("Original:", sentence)
106
+ print("Modified:", modified_sentence)
107
+
108
+ return modified_sentence
109
+
110
+
111
+
112
+
113
+ demo = gr.Interface(fn=main_func, inputs="text", outputs="text")
114
+
115
+ if __name__ == "__main__":
116
+ demo.launch(show_api=False)
requirements.txt ADDED
Binary file (4.9 kB). View file