-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsentiment-vsm.py
115 lines (97 loc) · 4.94 KB
/
sentiment-vsm.py
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
#!/bin/python
import speech_recognition as sr
import sys
import os
import numpy as np
import nltk
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')
from nltk.stem import PorterStemmer
from nltk.tokenize import word_tokenize
from nltk import pos_tag
from textblob import TextBlob
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
import subprocess
stopwords=['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're", "you've", "you'll", "you'd", 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', "she's", 'her', 'hers', 'herself', 'it', "it's", 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', "that'll", 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', "don't", 'should', "should've", 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', "aren't", 'couldn', "couldn't", 'didn', "didn't", 'doesn', "doesn't", 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'isn', "isn't", 'ma', 'mightn', "mightn't", 'mustn', "mustn't", 'needn', "needn't", 'shan', "shan't", 'shouldn', "shouldn't", 'wasn', "wasn't", 'weren', "weren't", 'won', "won't", 'wouldn', "wouldn't"]
punkts='''"#$%&\'()*+,-./:;<=>@[\\]^_`{|}~'''
def CorFilt(i):
ps = PorterStemmer()
buff = word_tokenize(i.lower().replace("\n", "").replace(" ", " ").replace("n't", " not"))
buff2 = ""
for j in pos_tag(buff):
if j[-1] == 'RB' and j[0] != "not":
pass
else:
buff2 += j[0] + " "
buff2 = buff2.replace("not ", "NOT")
buff = word_tokenize(buff2.strip())
ans = ""
for j in buff:
if (j not in punkts) and (j not in stopwords):
if j == "!":
ans += " XXEXCLMARK"
elif j == "?":
ans += " XXQUESMARK"
else:
if j != "'s" and j != "``":
ans += " " + ps.stem(j)
return ans.strip()
import pickle
f=open("EmoVec","rb")
EmoVec=pickle.load(f)
f.close()
f=open("vectorizer","rb")
vectorizer=pickle.load(f)
f.close()
def EmowavE(sent, vectorizer=vectorizer, EmoVec=EmoVec, trans=True):
transDict = {'gu': 'Gujarati',
'hi': 'Hindi'}
# Translate from any language to english
if trans:
analysis = TextBlob(sent)
if analysis.detect_language() != 'en':
try:
print(f"\nInput text was in {transDict[analysis.detect_language()]}")
except:
print(f"\nInput text was not in English")
print("\nTranslating...")
output = subprocess.check_output(['trans', '-b', sent])
sent = output.decode('utf-8').strip()
print(f"\nTranslation in English: {sent}")
EmoBuff = vectorizer.transform([CorFilt(sent)])
EmoDict = {0: 'anger',
1: 'disgust',
2: 'fear',
3: 'joy',
4: 'sadness'}
return EmoDict[np.argmax([float(cosine_similarity(EmoBuff.reshape(-1, 1).T, EmoVec[i].reshape(-1, 1).T)) for i in
range(EmoVec.shape[0])])]
header = sys.stdin.buffer.read(78)
while(1):
#data = sys.stdin.buffer.read(882000) #5 sec
data = sys.stdin.buffer.read(5292000) #30 sec
f = open("/tmp/inter.wav", "wb")
f.write(header)
f.write(data)
f.close()
os.system("ffmpeg -y -i /tmp/inter.wav -f wav /tmp/inter_f.wav 2> /dev/null")
File="/tmp/inter_f.wav"
AUDIO_FILE = File
r = sr.Recognizer()
with sr.AudioFile(AUDIO_FILE) as source:
audio = r.record(source)
try:
print("#######################################################################################")
print("Recognizing Text...")
Data=r.recognize_google(audio)
file1 = open("sentData.txt","a")#append mode
#file1.write(Data+" | Emotion: "+EmowavE(Data)+"\n")
file1.write(EmowavE(Data)+"\n")
file1.close()
print("#######################################################################################")
print("The audio file contains: " + Data)
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))