Skip to content

Commit

Permalink
After demo
Browse files Browse the repository at this point in the history
  • Loading branch information
fuadaghazada committed Apr 29, 2019
1 parent d3be9e1 commit 15ac52c
Show file tree
Hide file tree
Showing 13 changed files with 206 additions and 47 deletions.
23 changes: 21 additions & 2 deletions NYTimesClueGenerator/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,37 @@
import tkinter as tk
from tkinter import ttk

print("\n\n--Available resources for clue generation:")
print("\t-Wordnet")
print("\t-Merriam Dictionary")
print("\t-Oxford Dictionary")
print("\t-Urban Dictionary")
print("\t-Google Dictionary")
print("\t-Wikipedia")
print('---------------------------------\n\n')

'''
Getting files from 'data' directory
'''
def get_old_puzzles():
files = os.listdir('data')

print('---------------------------------')
print('Old puzzles are loading...\n')

names = {}
for file in files:
name = re.findall('[A-Z][^A-Z]*', file)[1].replace('.json', '')
names[str(name)] = file
if file != '.DS_Store':
name = re.findall('[A-Z][^A-Z]*', file)[1].replace('.json', '')
print("Puzzle: " + name + " has been loaded.")
names[str(name)] = file

# Sorting dates
names = sorted(names.items(), key = lambda x: datetime.datetime.strptime(x[0], '%B%d,%Y'))

print('\nAll old puzzles have been loaded')
print('---------------------------------')

return names


Expand Down Expand Up @@ -85,3 +102,5 @@ def display_old_puzzle(filename):
# Loop
root.resizable(False, False)
root.mainloop()

print("\n\nBye")
12 changes: 7 additions & 5 deletions NYTimesClueGenerator/src/clue_gen/clueChanger.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
from clue_gen.urbanParser import getWordFromUrban
from clue_gen.didyoumean import did_you_mean
from clue_gen.wikipediaParser import search_wikipedia
from clue_gen.googleDictionary import getWordFromGoogleDictionary

# All resources to be searched
RESOURCES = [{'name': 'Wikipedia', 'func': search_wikipedia},
{'name': 'Wordnet', 'func': search_wordnet},
{'name': 'Merriam', 'func': getWordFromMerriam},
{'name': 'Oxford', 'func': getWordFromOxford},
{'name': 'Urban', 'func': getWordFromUrban}]
{'name': 'Urban', 'func': getWordFromUrban},
{'name': 'Google Dict', 'func': getWordFromGoogleDictionary}]

# For finding the stem (root) of the word
ps = PorterStemmer()
Expand All @@ -36,10 +38,10 @@
'''


def changeClue(word, clue, replace_policy):
def changeClue(word, clue, replace_policy, trace = False):

# Checking the word in Google's 'Did You Mean'
did_you_mean_result = did_you_mean(word)
did_you_mean_result = did_you_mean(word, trace)
if did_you_mean_result is not None:
word = did_you_mean_result

Expand All @@ -63,7 +65,7 @@ def changeClue(word, clue, replace_policy):

# Checking the resources
for resource in RESOURCES:
results = resource['func'](word)[replaced]
results = resource['func'](word, trace)[replaced]

if results is not None and len(results) != 0:
# If the original clue returned as a new clue
Expand All @@ -82,7 +84,7 @@ def changeClue(word, clue, replace_policy):
if word.lower() not in result.lower():
continue
else:
new_clue = result.lower().replace(word.lower(), '...').strip()
new_clue = result.lower().replace(word.lower(), '___').strip()
is_found = True
break
else:
Expand Down
34 changes: 25 additions & 9 deletions NYTimesClueGenerator/src/clue_gen/clueGenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
'''


def changePuzzle(clues):
def changePuzzle(clues, trace = False):
across_clues = clues['across']
down_clues = clues['down']

Expand All @@ -18,19 +18,27 @@ def changePuzzle(clues):
word = ac_clue['answer']
num = ac_clue['number']

print("Searching clue for \"" + str(word) + "\"")
print("\n----------------------------------")
print("*** Searching clue for \"" + str(word) + "\" ***\n")
found = False
for i in range(4):
new_clue = changeClue(word, clue, i)
if i == 0: print("\t---Looking for definitions---\n")
elif i == 1: print("\t---Looking for synonyms---\n")
elif i == 2: print("\t---Looking for antonyms---\n")
elif i == 3: print("\t---Looking for examples---\n")

new_clue = changeClue(word, clue, i, trace)
if new_clue['new_clue'] is not None:
print("New clue for \"" + str(word) + "\" is found from " + str(new_clue['source']) + "\n")
print("\n*** New clue for \"" + str(word) + "\" is found from " + str(new_clue['source']) + " ***")
changed_across_clues.append({'clue': new_clue['new_clue'], 'answer': word, 'number': num})
print("----------------------------------\n")
found = True
break

# No new clue cannot be found
if found is False:
print("No new clue cannot be found for \"" + str(word) + "\"\n")
print("\n*** No new clue can be found for \"" + str(word) + "\" ***")
print("----------------------------------\n")
changed_across_clues.append(ac_clue)

print("\n----Generating down clues----\n")
Expand All @@ -40,19 +48,27 @@ def changePuzzle(clues):
word = down_clue['answer']
num = down_clue['number']

print("Searching clue for \"" + str(word) + "\"")
print("\n----------------------------------")
print("*** Searching clue for \"" + str(word) + "\" ***\n")
found = False
for i in range(4):
new_clue = changeClue(word, clue, i)
if i == 0: print("\t---Looking for definitions---\n")
elif i == 1: print("\t---Looking for synonyms---\n")
elif i == 2: print("\t---Looking for antonyms---\n")
elif i == 3: print("\t---Looking for examples---\n")

new_clue = changeClue(word, clue, i, trace)
if new_clue['new_clue'] is not None:
print("New clue for \"" + str(word) + "\" is found from " + str(new_clue['source']) + "\n")
print("\n*** New clue for \"" + str(word) + "\" is found from " + str(new_clue['source']) + " ***")
print("----------------------------------\n")
changed_down_clues.append({'clue': new_clue['new_clue'], 'answer': word, 'number': num})
found = True
break

# No new clue cannot be found
if found is False:
print("No new clue cannot be found for \"" + str(word) + "\"\n")
print("\n*** No new clue can be found for \"" + str(word) + "\" ***")
print("----------------------------------\n")
changed_down_clues.append(down_clue)

# Returning
Expand Down
18 changes: 16 additions & 2 deletions NYTimesClueGenerator/src/clue_gen/didyoumean.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@
'''


def did_you_mean(query):
def did_you_mean(query, trace = False):
try:
# Trace
if trace is True:
print("\t\t\"Did You Mean (from Google) check\" for the query: \"" + query + "\"")

# query = str(query).strip()
html = get_page('http:https://www.google.com/search?q=' + query)
soup = BeautifulSoup(html, 'html.parser')
Expand All @@ -21,8 +25,18 @@ def did_you_mean(query):
result = answer.find('i') if answer is not None else None
result = result.text if result is not None else None

# Trace
if trace is True and result is None:
print("\t\tNo correction from \"Did You Mean check\" for the query: \"" + query + "\"\n")
else:
print("\t\t\"" + query + "\" is replaced with \"" + result +"\"\n")

return result
except Exception as e:
raise
pass

# Trace
if trace is True:
print("\t\tPassed \"Did You Mean check\" for the query: \"" + query + "\"\n")

return None
60 changes: 60 additions & 0 deletions NYTimesClueGenerator/src/clue_gen/googleDictionary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import urllib.request
import json
import unicodedata

URL = 'https://googledictionaryapi.eu-gb.mybluemix.net/'

'''
Searching the word in Google Dictionary API (unofficial)
:param word - the given word
'''


def getWordFromGoogleDictionary(word, trace = False):
# Trace
if trace is True:
print("\t\tChecking Google dictionary for the word: \"" + word + "\"")

# Replacing non-eng chars
word = unicodedata.normalize('NFKD', word).encode('ascii', 'ignore').decode('utf8')

url = URL + '?define=' + word + '&lang=en'

meanings = []

try:
request = urllib.request.Request(url)
response = urllib.request.urlopen(request)
content = response.read().decode('utf-8')
contentJson = json.loads(content)

for res in contentJson:
meaning = res['meaning']

for key in meaning.keys():
for definition in meaning[key]:
meanings.append(definition['definition'])

except Exception as e:
pass

# Trace
if trace is True and len(meanings) == 0:
print("\t\tNo results from Google dictionary for the word: \"" + word + "\"\n")

# Returning word
word = {
"word": word,
"syn": [],
"ant": [],
"def": meanings,
"examples": []
}

return word


# TEST
# print(getWordFromGoogleDictionary('İSN\'T'))

10 changes: 9 additions & 1 deletion NYTimesClueGenerator/src/clue_gen/merriamParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
'''


def getWordFromMerriam(word):
def getWordFromMerriam(word, trace = False):
# Trace
if trace is True:
print("\t\tChecking Merriam dictionary for the word: \"" + word + "\"")

url = "https://dictionaryapi.com/api/v3/references/thesaurus/json/"+word+"?key="+merriam_app_key
synsMixed = []
antsMixed = []
Expand Down Expand Up @@ -48,6 +52,10 @@ def getWordFromMerriam(word):
exMixed = []
defsMixed = []

# Trace
if trace is True and len(synsMixed) == 0 and len(antsMixed) == 0 and len(defsMixed) == 0 and len(exMixed) == 0:
print("\t\tNo results from Merriam dictionary for the word: \"" + word + "\"\n")

# Returning word
word = {
"word": word,
Expand Down
10 changes: 9 additions & 1 deletion NYTimesClueGenerator/src/clue_gen/oxford.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
URL = 'https://od-api.oxforddictionaries.com:443/api/v2/entries/' + LANGUAGE + '/'


def getWordFromOxford(word):
def getWordFromOxford(word, trace = False):
# Trace
if trace is True:
print("\t\tChecking Oxford dictionary for the word: \"" + word + "\"")

url = URL + word.lower()

synsMixed = []
Expand Down Expand Up @@ -41,6 +45,10 @@ def getWordFromOxford(word):
exMixed = []
defsMixed = []

# Trace
if trace is True and len(defsMixed) == 0 and len(exMixed) == 0:
print("\t\tNo results from Oxford dictionary for the word: \"" + word + "\"\n")

# Returning word
word = {
"word": word,
Expand Down
10 changes: 9 additions & 1 deletion NYTimesClueGenerator/src/clue_gen/urbanParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
'''


def getWordFromUrban(word):
def getWordFromUrban(word, trace = False):
# Trace
if trace is True:
print("\t\tChecking Urban dictionary for the word: \"" + word + "\"")

url = URL + word

defsMixed = []
Expand All @@ -36,6 +40,10 @@ def getWordFromUrban(word):
except Exception as e:
pass

# Trace
if trace is True and len(defsMixed) == 0 and len(exMixed) == 0:
print("\t\tNo results from Urban dictionary for the word: \"" + word + "\"\n")

word = {
"word": word,
"syn": [],
Expand Down
10 changes: 9 additions & 1 deletion NYTimesClueGenerator/src/clue_gen/wikipediaParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
'''


def search_wikipedia(word):
def search_wikipedia(word, trace = False):
# To ignore module warnings
warnings.filterwarnings('ignore')

# Trace
if trace is True:
print("\t\tChecking Wikipedia for the word: \"" + word + "\"")

# Example sentences
sentences = []

Expand All @@ -29,6 +33,10 @@ def search_wikipedia(word):
except Exception as e:
pass

# Trace
if trace is True and len(sentences) == 0:
print("\t\tNo results from Wikipedia for the word: \"" + word + "\"\n")

return {
"word": word,
"syn": [],
Expand Down
10 changes: 9 additions & 1 deletion NYTimesClueGenerator/src/clue_gen/wordnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
'''


def search_wordnet(word):
def search_wordnet(word, trace = False):
# Trace
if trace is True:
print("\t\tChecking Wordnet for the word: \"" + word + "\"")

# Synonym set of the word)
synset = wordnet.synsets(str(word))

Expand All @@ -33,6 +37,10 @@ def search_wordnet(word):
if len(synonyms) != 0 and word.lower() in synonyms:
synonyms.remove(word.lower())

# Trace
if trace is True and len(synonyms) == 0 and len(antonyms) == 0 and len(definitions) == 0 and len(examples) == 0:
print("\t\tNo results from Wordnet for the word: \"" + word + "\"\n")

return {
"word": word,
"syn": synonyms,
Expand Down
Loading

0 comments on commit 15ac52c

Please sign in to comment.