-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearch.py
179 lines (122 loc) · 5.11 KB
/
Search.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import datetime
import json
import PyDictionary
import pyperclip
import random
import re
import webbrowser
import wikipedia
import wolframalpha
from googlesearch import search
import dearpygui.dearpygui as dpg
functionalityData = json.load(open("data/functionalityData.json"))
preprocessingData = json.load(open("data/preprocessingData.json"))
dictionary = PyDictionary.PyDictionary()
wolfram = wolframalpha.Client(functionalityData["wolfram_key"])
query_id = dpg.generate_uuid()
output_id = dpg.generate_uuid()
def searchZen(query):
# Query Pre-processing
query = query.lower()
for contraction, expansion in preprocessingData["contractions"].items():
query = query.replace(contraction, expansion)
# Commands that require punctuation go here.
if "r/" in query:
query = re.sub(".*r\/", "", query)
query = query.replace("_", "")
webbrowser.open_new("https://www.reddit.com/r/" + query)
return "Reddit page opened."
query = re.sub(r'[^\w\s]', '', query)
# Antonyms, Definitions, Synonyms, and Translations
if query.split()[0] == "antonym":
query = query.replace("antonym ", "", 1)
query = query.replace(" ", "")
antonyms = dictionary.antonym(query)
output = ""
for antonym in antonyms:
output += antonym + "\n"
return output
if query.split()[0] == "define":
query = query.replace("define ", "")
query = query.replace(" ", "")
returnData = dictionary.meaning(query)
output = ""
for type, definition in returnData.items():
for singleDefinition in definition:
output += type + " | " + singleDefinition + "\n"
return output
if query.split()[0] == "synonym":
query = query.replace("synonym ", "", 1)
query = query.replace(" ", "")
synonyms = dictionary.synonym(query)
output = ""
for synonym in synonyms:
output += synonym + "\n"
return output
# Online Connections
if "wikipedia" in query:
query = query.replace("wikipedia", "")
return wikipedia.summary(query)
if "wolfram" in query:
query = query.replace("wolfram alpha", "")
query = query.replace("wolfram", "")
response = wolfram.query(query)
return next(response.results).text
if "google" in query:
query = query.replace("google", "")
for j in search(query, tld="co.in", num=10, stop=10, pause=2):
return j
if "browser" in query:
query = query.replace("browser", "")
webbrowser.open_new("https://google.com")
return "Browser opened."
# Temporal
if query == "what is the date" or query == "what's the date" or query == "date":
date = datetime.datetime.now().strftime("%m/%d/%Y")
return f"The date is {date} (MM/DD/YYYY)"
if query == "what is the time" or query == "what's the time" or query == "what time is it" or query == "time":
time = datetime.datetime.now().strftime("%H:%M:%S")
return f"The time is {time}"
# Miscellanous
if "roll a " in query and "sided dice" in query:
query = query.replace("roll a ", "")
query = query.replace("sided dice", "")
return random.randint(1, int(query))
# Easter Eggs
if query == "what is your name":
return "Zen"
if query == "who made you":
return "Brian Nguyen"
if query == "what gender are you":
return "Animals and Spanish words have genders. I do not."
if query == "answer to life" or query == "answer to the universe" or query == "answer to everything":
return "42 - The Hitchhiker's Guide to the Galaxy"
if query == "i am sad" or query == "i am so sad":
if random.randint(0, 1) == 0:
return "I prescribe hot chocolate and Netflix."
else:
return "Instant cure for sadness: look up pictures of cats while eating ice cream."
def searchHandle():
dpg.set_value(output_id, "Loading...")
dpg.set_value(output_id, searchZen(dpg.get_value(query_id)))
def copyOutput():
pyperclip.copy(dpg.get_value(output_id))
def clear():
dpg.set_value(output_id, "Please Enter Query")
def openb():
webbrowser.open_new(dpg.get_value(output_id))
with dpg.window(label="Search Window", pos=[0, 0], no_close=True, no_collapse=True, no_move=True, no_resize=True, width=284, height=268):
dpg.add_input_text(label="", width=267, on_enter=True, callback=searchHandle, id=query_id)
dpg.add_button(label="Run", callback=searchHandle)
dpg.add_button(label="Copy Result", callback=copyOutput)
dpg.add_button(label="Clear Result", callback=clear)
dpg.add_button(label="Open Result", callback=openb)
with dpg.window(label="Results Window", pos=[0, 268], no_close=True, no_collapse=True, no_move=True, no_resize=True, width=284, height=268):
dpg.add_text("Output", wrap=250 , id=output_id)
dpg.set_value(output_id, "Please Enter Query")
dpg.setup_viewport()
dpg.set_viewport_resizable(False)
dpg.set_viewport_width(300)
dpg.set_viewport_height(575)
dpg.set_viewport_title("Zen")
dpg.start_dearpygui()