Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,107 +1,108 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
from
|
4 |
-
import
|
5 |
-
import
|
|
|
|
|
6 |
import spacy
|
7 |
-
|
8 |
-
import subprocess
|
9 |
|
10 |
-
|
11 |
-
nltk.download('punkt')
|
12 |
-
nltk.download('stopwords')
|
13 |
-
nltk.download('wordnet') # Download WordNet
|
14 |
|
15 |
-
#
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
nlp = spacy.load("en_core_web_sm")
|
21 |
|
22 |
-
#
|
23 |
-
|
|
|
24 |
|
25 |
-
#
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
28 |
|
29 |
-
#
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
-
#
|
34 |
-
def
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
40 |
|
41 |
-
#
|
42 |
-
def
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
if token.is_title:
|
50 |
-
replacement = replacement.capitalize()
|
51 |
-
processed_text.append(replacement)
|
52 |
-
else:
|
53 |
-
processed_text.append(token.text)
|
54 |
-
return " ".join(processed_text)
|
55 |
|
56 |
-
#
|
57 |
-
def
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
return ai_probability
|
64 |
|
65 |
-
#
|
66 |
-
def
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
no_repeat_ngram_size=3,
|
79 |
-
)
|
80 |
-
paraphrased_text = paraphrase_tokenizer.decode(paraphrased_ids[0], skip_special_tokens=True)
|
81 |
-
paraphrased_paragraphs.append(paraphrased_text)
|
82 |
-
return "\n\n".join(paraphrased_paragraphs)
|
83 |
|
84 |
-
#
|
85 |
-
def
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
|
97 |
-
#
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
-
|
107 |
-
interface.launch(debug=True)
|
|
|
1 |
+
from nltk.tokenize import word_tokenize
|
2 |
+
from nltk.tag import pos_tag
|
3 |
+
from nltk.corpus import wordnet as wn
|
4 |
+
from sematch.semantic.similarity import WordNetSimilarity
|
5 |
+
from vocabulary.vocabulary import Vocabulary as vb
|
6 |
+
import json
|
7 |
+
from random import randint
|
8 |
import spacy
|
9 |
+
import os.path
|
|
|
10 |
|
11 |
+
nlp = spacy.load('en_core_web_sm')
|
|
|
|
|
|
|
12 |
|
13 |
+
# Function to tag sentence with part of speach
|
14 |
+
def tag(sentence):
|
15 |
+
words = word_tokenize(sentence)
|
16 |
+
words = pos_tag(words)
|
17 |
+
return words
|
|
|
18 |
|
19 |
+
# Determine the POS to paraphrase
|
20 |
+
def paraphraseable(tag):
|
21 |
+
return tag.startswith('NN') or tag =='VB' or tag.startswith('JJ')
|
22 |
|
23 |
+
# POS tagging
|
24 |
+
def pos(tag):
|
25 |
+
if tag.startswith('NN'):
|
26 |
+
return wn.NOUN
|
27 |
+
elif tag.startswith('V'):
|
28 |
+
return wn.VERB
|
29 |
|
30 |
+
# Function to crate synonyms using wordnet nltk
|
31 |
+
def synonyms(word, tag):
|
32 |
+
listOfLemmas = [baseWord.lemmas() for baseWord in wn.synsets(word, pos(tag))]
|
33 |
+
if len(listOfLemmas) > 0:
|
34 |
+
listOfLemmas = listOfLemmas[0]
|
35 |
+
lemmas = [lemma.name().encode('ascii', 'ignore') for lemma in listOfLemmas]
|
36 |
+
return set(lemmas)
|
37 |
+
else:
|
38 |
+
return set([])
|
39 |
|
40 |
+
# Create dictonary synonums
|
41 |
+
def dictonarySynonums(word):
|
42 |
+
synJSON = vb.synonym(word)
|
43 |
+
if synJSON != False:
|
44 |
+
synonyms_lists = [dictSyno["text"].encode('ascii', 'ignore') for dictSyno in json.loads(vb.synonym(word))]
|
45 |
+
return set(synonyms_lists)
|
46 |
+
else:
|
47 |
+
return set([])
|
48 |
|
49 |
+
# controll set to calculate the semantic similarity of synonums from the base words using SPACY
|
50 |
+
def controlledSetSpacy(word,similarWords):
|
51 |
+
utf_en_word = nlp(word.decode('utf-8', 'ignore'))
|
52 |
+
for similarWord in similarWords.copy():
|
53 |
+
utf_en_similarWord = nlp(similarWord.decode('utf-8','ignore'))
|
54 |
+
if utf_en_word.similarity(utf_en_similarWord) <.76: # Variable to control accuracy of controlset
|
55 |
+
similarWords.discard(similarWord)
|
56 |
+
return similarWords
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
+
# controll set to calculate the semantic similarity of synonums from the base words using WordNetSimilarity
|
59 |
+
def controlledSetWordNetSimilarity(word,similarWords):
|
60 |
+
wns = WordNetSimilarity()
|
61 |
+
for similarWord in similarWords.copy():
|
62 |
+
if wns.word_similarity(word, similarWord, 'li') < 0.9996: # Variable to control accuracy of controlset
|
63 |
+
similarWords.discard(similarWord)
|
64 |
+
return similarWords
|
|
|
65 |
|
66 |
+
# to to get synonums from wordnet nltk as well as from python dictonary synonums
|
67 |
+
def synonymIfExists(sentence):
|
68 |
+
for (word, t) in tag(sentence):
|
69 |
+
if paraphraseable(t) and word not in ["i","I"]:
|
70 |
+
syns = synonyms(word, t)
|
71 |
+
syns.update(dictonarySynonums(word))
|
72 |
+
if syns:
|
73 |
+
syns = controlledSetWordNetSimilarity(word,syns) # Or use the commented controlled set
|
74 |
+
#syns = controlledSetSpacy(word,syns)
|
75 |
+
if len(syns) > 1:
|
76 |
+
yield [word, list(syns)]
|
77 |
+
continue
|
78 |
+
yield [word,[]]
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
+
# Function to get the semantic similar synonums and the total count of synonums in the entire sentence
|
81 |
+
def paraphrase(sentence):
|
82 |
+
bagOfWords = []
|
83 |
+
counter = 1
|
84 |
+
for tempArray in synonymIfExists(sentence):
|
85 |
+
eachBoW=[]
|
86 |
+
eachBoW.append(tempArray[0])
|
87 |
+
eachBoW.extend(tempArray[1])
|
88 |
+
eachBoW=list(set(eachBoW))
|
89 |
+
counter *= len(eachBoW)
|
90 |
+
bagOfWords.append(eachBoW)
|
91 |
+
return bagOfWords,counter
|
92 |
|
93 |
+
# Function to re-create sentence with synonums where the synonums are taken in randon order
|
94 |
+
def paraPhraseThisSentence(sentence):
|
95 |
+
ppList = []
|
96 |
+
vList,count = paraphrase(sentence)
|
97 |
+
allWordsCount = len(vList)
|
98 |
+
for y in range(count):
|
99 |
+
str = []
|
100 |
+
returnStr = " "
|
101 |
+
for w in range(allWordsCount):
|
102 |
+
str.append(vList[w][randint(0,len(vList[w])-1)].replace("_"," "))
|
103 |
+
ppList.append(returnStr.join(str))
|
104 |
+
ppList = list(set(ppList))
|
105 |
+
print (ppList)
|
106 |
+
return ppList
|
107 |
|
108 |
+
paraPhraseThisSentence("Financial Institutes have always helped the society to become better version of itself.")
|
|