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))