What I like about PyDictionary when it comes to this project is that if a variable matches the name of a notable character. Einstein for example, it finds a match. What I am trying to accomplish is that when a loop runs through sequence of letters, if the first two letters or more matches the beginning (prefix) of the word, then treat that as a match.
I have tried several approaches and can't seem to get this working. Only when I pass in a whole word into a variable does PyDictionary find a match.
This is what I have tried up to now
#ATTEMPT 1
import re
import textwrap
import numpy as np
import datetime as dt
from PyDictionary import PyDictionary
from dateutil import parser
import nltk
nltk.download('wordnet')
from nltk.corpus import wordnet
nltk.download('omw-1.4')
dictionary = PyDictionary()
einstein = ['e', 'i', 'n', 's', 't', 'e', 'i', 'n']
newereinstein = str(einstein)
neweinstein = ''
letter = ''
for l in einstein:
letter = letter + l
neweinstein = letter
meaning = dictionary.meaning(neweinstein)
meaning = str(meaning)
if re.search('einst', meaning):
print('found einst...!')
print('this is einstein found', neweinstein)
print(meaning + 'this is the meaning of neweinstien on this loop atb the top')
else:
print('hit, was not found on this loop with re.search(' + neweinstein + meaning)
#Hits the Else statement every time
words = dictionary.getSynonyms()
if any(word.startswith('hit') for word in words.keys()):
print("At least one word in the dictionary starts with the letter:", 'hit')
else:
print("No word in the dictionary starts with the letter:" + neweinstein)
#same application, I get "doesn't have attributes keys" error with this
#ATTEMPT 2
import re
import textwrap
import numpy as np
import datetime as dt
from PyDictionary import PyDictionary
from dateutil import parser
import nltk
dictionary = PyDictionary()
newcollector = ""
nextcollector = ""
for t in range (len(diagonal_matrix)):
for i in range(len(diagonal_matrix)):
collector = diagonal_matrix[i]
collector = str(collector)
print("this is i", collector)
newcollector = str(diagonal_matrix[t])
print("this is t", newcollector)
newcollector = newcollector + collector
print("before the if statement", newcollector)
meaning = dictionary.meaning(newcollector)
if meaning is not None and len(newcollector.strip()) >= 2:
print("this is meaning not none kicking in:", newcollector)
nextletter = diagonal_matrix[i + 1]
nextcollector = newcollector + str(nextletter)
else:
meaning = ''
for t in dictionary:
if t.text.startswith(nextcollector):
print("found first letters of a word", nextcollector)
nextletter = diagonal_matrix[i + 1]
nextcollector = newcollector + str(nextletter)
if i == len(diagonal_matrix) - 1:
break
print("not met condtion", newcollector)
if dictionary.meaning(nextcollector):
meaning = dictionary.meaning(nextcollector)
print(meaning)
else:
print(collector)
print("out of loop", nextcollector)
#finds the meaning if any of the two letters make up a full word (hi for example) but like my other attempts doesn't find if the first two letters or more of a word matches a word
#ATTEMPT 3
import re
import textwrap
import numpy as np
import datetime as dt
from PyDictionary import PyDictionary
from dateutil import parser
import nltk
nltk.download('wordnet')
from nltk.corpus import wordnet
nltk.download('omw-1.4')
import string
import enchant
def check_if_word_in_dictionary(word):
dictionary = PyDictionary()
found = False
if dictionary.meaning(word, True) is not None:
found = True
elif len(word) > 1:
# check if any word in the dictionary starts with the provided letters
possible_words = [key for key in dictionary.getMeanings() if key.startswith(word)]
if possible_words:
found = True
if found:
print(f"You're in luck, '{word}' is found in the dictionary!")
else:
print(f"It appears '{word}' is NOT a word found in the dictionary.")
check_if_word_in_dictionary("Einstei")
#Again, only find if the letters make a full word but hits false otherwise
#ATTEMPT 4
import re
import textwrap
import numpy as np
import datetime as dt
from PyDictionary import PyDictionary
from dateutil import parser
import nltk
nltk.download('wordnet')
from nltk.corpus import wordnet
nltk.download('omw-1.4')
dictionary = PyDictionary()
print(dictionary)
oriword = ['e', 'i', 'n', 's', 't', 'e', 'i', 'n']
syncount = 0
lemmacount = 0
nextcombo = ''
for letter in oriword:
print('this is the letter before the loop check ' + str(letter))
nextcombo = nextcombo + str(letter)
print('this is next combo ' + str(nextcombo))
synsets = wordnet.synsets(nextcombo)
antonym = dictionary.antonym(nextcombo)
print('this is the synonyms: ' + str(antonym))
print('this is a meaning:' + str(meaning))
if synsets:
for synset in synsets:
print('this is synsets loop ')
syncount += 1
print('this is syncount: '+ str(syncount))
for lemma in synset.lemmas():
lemmacount += 1
print('this is lemma count: ' + str(lemmacount))
antonym = str(antonym)
newword = str(oriword)
print('nextcombo:' + str(nextcombo))
print('this is the word before the if statement ' + str(newword))
nextcombo = nextcombo + str('')
for d in dictionary:
print('i am' + str(d))
if d.startswith(nextcombo):
print('this is the starts with function kicking in' + str(nextcombo))
print('found word')
print('this is meaning that is not None: ' + str(meaning))
print('this is the word within the if statement', nextcombo)
print('this is the lemma name: ', str(lemma.name))
print('this is the lemma loop ', str(antonym))
print('this is the synset: ', str(synset))
break
else:
continue
break
if dictionary.meaning(nextcollector):
else:
print('not found')
#Again, this hits the else statement every time
Please ignore the indentation. I may have cocked that up pasting in here from four different projects. I am well familiar with the indentation rules of Python so disregard these errors.
Your help is very much appreciated. I am stuck. thank you.