-
Notifications
You must be signed in to change notification settings - Fork 0
/
personal_assistant.py
84 lines (71 loc) · 2.64 KB
/
personal_assistant.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
# personal_assistant.py
import speech_recognition as sr
from nltk import pos_tag
from nltk.corpus import wordnet
from nltk.stem import WordNetLemmatizer
from nltk.tokenize import word_tokenize
from textblob import TextBlob
import datetime
class PersonalAssistant:
def __init__(self):
self.recognizer = sr.Recognizer()
def listen(self):
with sr.Microphone() as source:
print("Say something:")
audio = self.recognizer.listen(source)
try:
text = self.recognize_speech(audio)
print("You said:", text)
return text
except sr.UnknownValueError:
print("Sorry, could not understand audio.")
return None
def recognize_speech(self, audio):
return self.recognizer.recognize_google(audio)
def process_text(self, text):
tokens = word_tokenize(text)
tagged_tokens = pos_tag(tokens)
lemmatizer = WordNetLemmatizer()
# Lemmatize words
lemmatized_tokens = [lemmatizer.lemmatize(word, get_wordnet_pos(tag))
for word, tag in tagged_tokens]
return lemmatized_tokens
def get_wordnet_pos(self, treebank_tag):
if treebank_tag.startswith('J'):
return wordnet.ADJ
elif treebank_tag.startswith('V'):
return wordnet.VERB
elif treebank_tag.startswith('N'):
return wordnet.NOUN
elif treebank_tag.startswith('R'):
return wordnet.ADV
else:
return wordnet.NOUN
def analyze_sentiment(self, text):
blob = TextBlob(text)
sentiment = blob.sentiment.polarity
if sentiment > 0:
return "Positive"
elif sentiment < 0:
return "Negative"
else:
return "Neutral"
def execute_command(self, command):
if "time" in command:
current_time = datetime.datetime.now().strftime("%H:%M:%S")
print(f"The current time is {current_time}.")
elif "weather" in command:
# Implement weather API integration here
print("Fetching weather information...")
else:
print("Command not recognized. Please try again.")
if __name__ == "__main__":
assistant = PersonalAssistant()
while True:
user_input = assistant.listen()
if user_input:
processed_input = assistant.process_text(user_input)
sentiment = assistant.analyze_sentiment(user_input)
print(f"Sentiment: {sentiment}")
# Execute command based on processed input
assistant.execute_command(processed_input)