Spaces:
Sleeping
Sleeping
File size: 4,020 Bytes
08d712b 709ad12 08d712b 792f49a 08d712b 792f49a c83c82c 792f49a 1f6c6b2 08d712b 792f49a 08d712b 792f49a 08d712b 1d8563f 792f49a 1d8563f 08d712b b8739c7 792f49a |
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 146 147 148 149 150 |
import gradio as gr
import nltk
import re
nltk.download('punkt')
nltk.download('wordnet')
from nltk.corpus import wordnet
nltk.download("averaged_perceptron_tagger")
nltk.download('wordnet')
def find_antonyms(word, tag):
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"]):
antonyms = []
for syn in wordnet.synsets(word):
for lemma in syn.lemmas():
if lemma.antonyms():
antonyms.append(lemma.antonyms()[0].name())
return antonyms
return []
def insert_not(sentence):
words = nltk.word_tokenize(sentence)
pos_tags = nltk.pos_tag(words)
aux_verbs = ["be", "have", "do", "will", "shall", "are", "is", "was", "were", "am", "does", "has", "had", "can", "could", "may", "might", "must", "should", "would"]
new_words = []
#skip_next = False
flag = False
for i, (word, tag) in enumerate (pos_tags):
new_words.append(word)
if word.lower() in aux_verbs:
if i + 1 < len(words) and words[i+1].lower() != 'not' and not flag:
new_words.append("not")
flag = True
return " ".join(new_words)
def modify_sentence(sentence):
words = nltk.word_tokenize(sentence)
pos_tagger = nltk.pos_tag(words)
new_sentence = insert_not(sentence)
if len(new_sentence) != len(sentence):
return new_sentence
else:
new_words = []
for word, tag in pos_tagger:
antonyms = find_antonyms(word, tag)
if antonyms:
new_word = antonyms[0]
else:
new_word = word
new_words.append(new_word)
new_sentence = " ".join(new_words)
# print(sentence)
# print(new_sentence)
if new_sentence == sentence:
new_sentence = insert_not(sentence)
return new_sentence
def remove_no_not(sentence):
words_to_remove = ['no', 'not', "n't"]
pattern = r'\b(' + '|'.join(words_to_remove) + r')\b'
cleaned_sentence = re.sub(pattern, '', sentence)
cleaned_sentence = re.sub(r'\s+', ' ', cleaned_sentence).strip()
# print(cleaned_sentence)
return cleaned_sentence
def transform_contractions(sentence):
contractions_mapping = {
"don't": "do",
"doesn't": "does",
"won't": "will",
"isn't": "is",
"aren't": "are",
"wasn't": "was",
"weren't": "were",
"haven't": "have",
"hasn't": "has",
"hadn't": "had",
"can't": "can",
"couldn't": "could",
"shouldn't": "should",
"mightn't": "might",
"mustn't": "must"
# Add any other contractions you need to handle
}
for contraction, replacement in contractions_mapping.items():
pattern = r'\b' + contraction + r'\b'
sentence = re.sub(pattern, replacement, sentence)
return sentence
def clean_text(text):
text = re.sub(r'[^\w\s]', '', text)
text = text.strip()
return text
def main_func(sentence):
# sentence = clean_text(sentence)
sentence = sentence.lower()
removed_dont = transform_contractions(sentence)
if len(removed_dont) != len(sentence):
return removed_dont
else:
removed_no_sentence = remove_no_not(sentence)
# print(removed_no_sentence)
if len(removed_no_sentence) != len(sentence):
return removed_no_sentence
else:
modified_sentence = modify_sentence(sentence)
return modified_sentence
demo = gr.Interface(title="POS/NEG Sentence APP", fn=main_func, inputs="text", outputs="text", css="footer {visibility: hidden}", examples=[
["I hate football"],
["Don't I like that food?"],
["I can't Swim"],
["Onion price is increasing"]])
if __name__ == "__main__":
demo.launch(show_api=False)
|