File size: 2,261 Bytes
1f510ce |
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 |
import pickle
import numpy as np
import spacy,re
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
import string,nltk
prediction=[]
def preprocess_text(text):
nltk.download('punkt')
nltk.download('stopwords')
text = re.sub(r'(\w)\1{2,}', r'\1', text)
tokens = word_tokenize(text)
tokens = [token for token in tokens if token not in string.punctuation]
tokens = [token.lower() for token in tokens]
stop_words = set(stopwords.words('english'))
tokens = [token for token in tokens ]
return " ".join(tokens)
def remove_user_names(text):
return re.sub("@\w+|@\W+","",text.lower())
def remove_web_url(text):
return re.sub("http:(..([a-zA-Z0-9_]+)+([a-zA-Z0-9_]+)..([a-zA-Z0-9_]+)..(([a-zA-Z0-9_]+))).(([a-zA-Z0-9_]+))","website",text)
def remove_hastag(text):
return re.sub("#\w+","",text)
def remove_all_special_char(text):
return re.sub("[^\w\s]","",text)
def remove_more_space(text):
return re.sub(' +',' ',text)
def remove(text):
return remove_more_space(remove_all_special_char(remove_hastag(remove_web_url(remove_user_names(text)))))
nlp=spacy.load('en_core_web_lg')
emotion_emojies={
"happiness":["๐","๐","๐คฃ","๐","๐","๐","๐","๐"],
"love":["โค๏ธ","๐","๐","๐"],
"sadness":["๐คทโโ๏ธ","๐","๐ซ","๐"],
"empty":["๐ชน","๐ซ","๐ช"],
"enthusiasm":["๐","๐","๐ซก"],
"neutral":["๐","๐ถ","๐ซฅ","๐"],
"worry":["๐ตโ๐ซ","๐ค","๐ฅถ","๐คข"],
"surprise":["๐ฏ","๐ฎ","๐คญ","๐","๐คญ","๐ฒ","๐"],
"fun":["๐","๐","โฑ๏ธ","๐"],
"hate":["๐","๐คฌ","๐ก"],
"boredom":["๐ฅฑ"],
"relief":["๐","๐ฎโ๐จ"],
"anger":["๐ค","๐ฅ","๐ด","๐ค","๐คฏ","๐ข","๐ "]
}
def predict(text):
with open('model.pkl','rb') as f:
model=pickle.load(f)
prediction.append(model.predict(np.stack(nlp(preprocess_text(remove(text))).vector).reshape(1,300)))
emoji=[emotion_emojies[emoji] for emoji in emotion_emojies.keys() if prediction[0][0]==emoji]
return emoji
if "__main__"==__name__:
text=input("Input the Text : ")
print(predict(text)) |