-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrapwrite.py
135 lines (102 loc) · 3.25 KB
/
rapwrite.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
#Imports
import requests
import json
from tkinter import *
from tkinter import ttk
#Working On RapWrite
root = Tk()
root.call('source', 'forest-dark.tcl')
ttk.Style().theme_use('forest-dark')
root.title('RapWrite')
root.geometry('850x500')
#root.maxsize(850, 500)
#Main Frame
mainFrame = ttk.Frame(root)
mainFrame.pack(pady=5)
bottomFrame = ttk.Frame(root)
bottomFrame.pack(side='top', pady=5)
# Listbox
rhyme_Listbox = Listbox(mainFrame)
rhyme_Listbox.pack(side='left')
nRhyme_Listbox = Listbox(mainFrame)
nRhyme_Listbox.pack(side='left')
followUp_Listbox = Listbox(mainFrame)
followUp_Listbox.pack(side='left')
#Entry Box
string = Entry(bottomFrame, width=20, font=('Arial 14'))
string.pack()
#Scroll Bar
padScroll = Scrollbar(mainFrame)
padScroll.pack(side='right', fill='y')
#Text Box
versePad = Text(mainFrame, width=40, height=20, font=('Arial', 12), selectbackground='yellow', selectforeground='black', undo=True, yscrollcommand=padScroll.set)
versePad.pack()
padScroll.config(command=versePad.yview)
##API Request
def apiRequest(words):
#API Request Section
rhymeRequest = requests.get(f'http://api.datamuse.com/words?rel_rhy={words[-1]}').text
rhymeRequest_info = json.loads(rhymeRequest)
nryRequest = requests.get(f'http://api.datamuse.com/words?rel_nry={words[-1]}').text
nryRequest_info = json.loads(nryRequest)
bgaRequest = requests.get(f'http://api.datamuse.com/words?rel_bga={words[-1]}').text
bgaRequest_info = json.loads(bgaRequest)
return rhymeRequest_info, nryRequest_info, bgaRequest_info
#Main Functions
def rhymeList(rhymeRequest_info):
wordRhymes = []
length = len(rhymeRequest_info)
if length == 0:
wordRhymes.append('No Rhymes...')
else:
for i in range(length):
if rhymeRequest_info[i]['score'] >= 1:
result = rhymeRequest_info[i]['word']
wordRhymes.append(result)
return wordRhymes
def nRhyme(nryRequest_info):
nRhymes = []
length = len(nryRequest_info)
if length == 0:
nRhymes.append('No Near Rhymes...')
else:
for i in range(length):
if nryRequest_info[i]['score'] >= 1:
result = nryRequest_info[i]['word']
nRhymes.append(result)
return nRhymes
def followUp(bgaRequest_info):
bgaStarters = []
length = len(bgaRequest_info)
if length == 0:
bgaStarters.append('No Follow Up...')
else:
for i in range(length):
if bgaRequest_info[i]['score'] >= 1:
result = bgaRequest_info[i]['word']
bgaStarters.append(result)
return bgaStarters
def listFill(rhyList, nryList, followUpList):
for item in rhyList:
rhyme_Listbox.insert(END, item)
for item in nryList:
nRhyme_Listbox.insert(END, item)
for item in followUpList:
followUp_Listbox.insert(END, item)
def delete():
rhyme_Listbox.delete(0, END)
nRhyme_Listbox.delete(0, END)
followUp_Listbox.delete(0, END)
def button_command():
text = string.get()
words = text.split()
rhymeRequest_info, nryRequest_info, bgaRequest_info = apiRequest(words)
rhyList = rhymeList(rhymeRequest_info)
nryList = nRhyme(nryRequest_info)
bgaList = followUp(bgaRequest_info)
delete()
listFill(rhyList, nryList, bgaList)
return None
unblock = ttk.Button(bottomFrame, text="Unblock", command=button_command)
unblock.pack()
mainFrame.mainloop()