Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import re
|
3 |
+
import contractions
|
4 |
+
import unicodedata
|
5 |
+
import translators as ts
|
6 |
+
|
7 |
+
import numpy as np
|
8 |
+
import nltk
|
9 |
+
nltk.download('punkt')
|
10 |
+
|
11 |
+
import spacy
|
12 |
+
spacy.load('en_core_web_sm')
|
13 |
+
|
14 |
+
nlp = spacy.load('en_core_web_sm')
|
15 |
+
|
16 |
+
def spacy_lemmatize_text(text):
|
17 |
+
text = nlp(text)
|
18 |
+
text = ' '.join([word.lemma_ if word.lemma_ != '-PRON-' else word.text for word in text])
|
19 |
+
return text
|
20 |
+
|
21 |
+
def remove_accented_chars(text):
|
22 |
+
text = unicodedata.normalize('NFC', text).encode('ascii', 'ignore').decode('utf-8', 'ignore')
|
23 |
+
return text
|
24 |
+
|
25 |
+
def remove_special_characters(text, remove_digits=False):
|
26 |
+
pattern = r'[^a-zA-Z0-9\s]' if not remove_digits else r'[^a-zA-Z\s]'
|
27 |
+
text = re.sub(pattern, '', text)
|
28 |
+
return text
|
29 |
+
|
30 |
+
def remove_stopwords(text, is_lower_case=False, stopwords=None):
|
31 |
+
if not stopwords:
|
32 |
+
stopwords = nltk.corpus.stopwords.words('english')
|
33 |
+
tokens = nltk.word_tokenize(text)
|
34 |
+
tokens = [token.strip() for token in tokens]
|
35 |
+
|
36 |
+
if is_lower_case:
|
37 |
+
filtered_tokens = [token for token in tokens if token not in stopwords]
|
38 |
+
else:
|
39 |
+
filtered_tokens = [token for token in tokens if token.lower() not in stopwords]
|
40 |
+
|
41 |
+
filtered_text = ' '.join(filtered_tokens)
|
42 |
+
return filtered_text
|
43 |
+
|
44 |
+
def greet(sentence):
|
45 |
+
opo_texto_sem_caracteres_especiais = (remove_accented_chars(sentence))
|
46 |
+
# sentenceMCTIList_base = nltk.word_tokenize(opo_texto_sem_caracteres_especiais)
|
47 |
+
sentenceExpanded = contractions.fix(opo_texto_sem_caracteres_especiais)
|
48 |
+
sentenceWithoutPunctuation = remove_special_characters(sentenceExpanded , remove_digits=True)
|
49 |
+
sentenceLowered = sentenceWithoutPunctuation.lower()
|
50 |
+
sentenceLemmatized = spacy_lemmatize_text(sentenceLowered)
|
51 |
+
sentenceLemStopped = remove_stopwords(sentenceLemmatized, is_lower_case=False)
|
52 |
+
|
53 |
+
return nltk.word_tokenize(sentence)
|
54 |
+
|
55 |
+
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
56 |
+
iface.launch()
|