'I am trying a different approach in python
The goal of my code is to ask for the the data input and the target in a dictionary format. The input is the word and the target is the definition and I want to return the output which is the target or the definition. I am trying a different approach, can you help me?
def search(input, target):
data_words = []
for x in input:
data_words += [x[0].lower()]
if target.lower() not in data_words:
return "Word does not exist"
else:
index = data_words.index(target.lower())
return (input[index][-1])
Solution 1:[1]
From what I understood, you're trying to get definitons of words. Such as:
Definition of apple: the round fruit of a tree of the rose family, which typically has thin green or red skin and crisp flesh.
You can create a dict and add your target's (in this case apple) and definitions into that dict.
First you should install nltk package.
Simply, run pip install nltk on your terminal.
Here's the code:
# Use nltk to tokenize words
import nltk
nltk.download('punkt')
from nltk.tokenize import word_tokenize
# Create your definitions dictionary
dictionary = {'apple': 'the round fruit of a tree of the rose family, which typically has thin green or red skin and crisp flesh.',
'banana': 'a long curved fruit which grows in clusters and has soft pulpy flesh and yellow skin when ripe.'}
def search(sentence):
sentence = sentence.lower()
tokenize = word_tokenize(sentence)
for i in tokenize:
if i in dictionary:
print(f"The definition of {i} is:", dictionary[i])
getDefinition = input("The word that you want to see it's definiton: ")
search(getDefinition)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mustafa U?ur Bask?n |
