Python Dictionary from External file - python

I am trying to make a dictionary in Python, but I do not know how to do two things.
When I search for keywords in a dictionary, instead of just looking for direct matches, I would like it to find every word that has the keywords in it. E.G. Search: Cat - Results: Cat, Allocate.
I would like the dictionary to load an external file, so new terms that I add to the dictionary can be saved when I load it up afterwards.

You can use following methods:
For 1.
print ("Welcome back to the dictionary");
dict = {"CAT": "A small four legged animal that likes to eat mice",
"DOG": "A small four legged animal that likes to chase cats",
"ALLOCATE": "to give something to someone as ​their ​share of a ​total ​amount, to use in a ​particular way",
}
def Dictionary():
x = input("\n\nEnter a word: \n>>>");
x = x.upper();
found = False
for y in dict:
if x in y:
found = True
print (x,":",dict[x])
Dictionary()
break
if not found:
y = input ("Unable to find word. Enter a new definition of your word: \n>>>");
dict.update({x:y})
Dictionary()
Dictionary()
For 2: You can load data directly from a json file
import json
dict = {}
with open("test.json", "r") as config_file:
dict = json.load(config_file)
where test.json being your file for e.g.
test.json
{"CAT": "A small four legged animal that likes to eat mice",
"DOG": "A small four legged animal that likes to chase cats",
"ALLOCATE": "to give something to someone as ​their ​share of a ​total ​amount, to use in a ​particular way",
}

This should let you match cat in allocate.
for key in dict.keys():
if x in key:
do some stuff

Related

How do you see built-in variables to check if your answer is correct? Python

I'm almost there, I think, but I'm stuck. In this problem, there are built-in variables, Thesaurus and Corpus, that I don't have to define. I'm having a couple issues:
I'm not getting the right keywords and/or occurrences from my code
I can't figure out how to correctly print the Thesaurus (in order to check). The Corpus prints nicely with just a simple print statement, however the same print statement will return items in the Thesaurus as their locations instead of the words. I don't know the variable attributes, as I didn't define the class, so I feel like I'm shooting in the dark.
When I try to give store.append two arguments, it won't accept both, even though I see other people have done it this way. I can only get the words and number of occurrences to print when I have two store.append statements. (I'm trying to get the output to be a tuple and not a list)
Right now, I'm testing with this made-up Thesaurus and Corpus:
class Entry : #testing your own Thesaurus/ Corpus
def __init__(self, word, synonyms) :
self.word = word
self.synonyms = synonyms
e1 = Entry("savory", ["umami", "meat", "main course", "dinner"])
e2 = Entry("sweet", ["dessert", "candy", "tart", "sugar"])
Thesaurus = [e1, e2]
doc1 = ["My", "main course", "is", "a", "meat", "dish", "with", "lots", "of", "umami"]
doc2 = ["yum", "I", "love", "sweet", "dessert"]
doc3 = ["this", "is", "yet", "another", "savory", "dish"]
Corpus = [doc1, doc2, doc3]
Any help is appreciated. Thanks.
def search(keyword) :
all_words = [keyword]
for entry in Thesaurus:
if entry.word == keyword:
for word in entry.synonyms:
all_words.append(word)
store = []
for search_word in all_words:
count = 0
for document in Corpus:
for word in document:
if search_word == word:
count = count + 1
store.append(search_word)
store.append(count)
return store
input = "happy"
output = search(input) # invoke the method using a test input
print(output) # prints the output of the function
# do not remove this line!
I think the best solution for you is using the dictionary as the representation of the Entity if you don't have to use class, which look like e1 = {'savory':["umami", "meat", "main course", "dinner"]} .
You have two ways to print class:
2.1 write a print method in the class and call it to print the class.
2.2 try to visit __ dict __ like: print(e1.__ dict __) (no blankspace)
append two args by using a.apend([b1, b2]) is a possible way.

Searching for 2 word key in dictionary

im new in python and world of programming. get to the point. when i run this code and put input let say chicken, it will reply as two leg animal. but i cant get reply for two words things that has space in between like space monkey(althought it appear in my dictionary) so how do i solve it???
my dictionary: example.py
dictionary2 = {
"chicken":"chicken two leg animal",
"fish":"fish is animal that live under water",
"cow":"cow is big vegetarian animal",
"space monkey":"monkey live in space",
my code: test.py
from example import *
print "how can i help you?"
print
user_input = raw_input()
print
print "You asked: " + user_input + "."
response = "I will get back to you. "
input_ls = user_input.split(" ")
processor = {
"dictionary2":False,
"dictionary_lookup":[]
}
for w in input_ls:
if w in dictionary2:
processor["dictionary2"] = True
processor["dictionary_lookup"].append(w)
if processor["dictionary2"] is True:
dictionary_lookup = processor["dictionary_lookup"][0]
translation = dictionary2[dictionary_lookup]
response = "what you were looking for is: " + translation
print
print "Response: " + response
You need to explain your purpose to get better help.
In your case you seem to be only interested in looking up words and then this code should be sufficient. Notice the .format() syntax which cleans up your code drastically.
updated code : now a list is created with the combinations found in the input. This however might need modification to fit needs.
dictionary2 = {
"chicken":"chicken two leg animal",
"fish":"fish is animal that live under water",
"cow":"cow is big vegetarian animal",
"space monkey":"monkey live in space"}
print("how can i help you?")
user_input = raw_input()
print("You asked: {}.".format(user_input))
split = user_input.split(" ")
combos = [' '.join(split[x:y]) for x in range(len(split)) for y in range(len(split)+1) if ' '.join(split[x:y]) != ""]
# Create an empty dictionary to insert found item
response = {}
for item in combos:
if dictionary2.get(item):
response[item] = "what you were looking for is: {}.".format(dictionary2[item])
# If dictionary is empty do this
if not response:
print("Response: I will get back to you!")
# If not, loop over keys(k) and values(v) and print them with an index(ind)
for ind, (k,v) in enumerate(response.iteritems()):
print("Response {}: {} ({})".format(ind+1, v, k))
I've redone my answer even though an answer is chosen as this was an interesting problem and I was close to a fair solution in given time.
This answers can take human like questions instead of just words.
Although, for true machine learning nltk is a better option. For start we can use something like below.
It used builtin library difflib to match question against dictionary keys and decided which has higher probability.
Warning: Exception handling is not implemented. It will just pick up max probable match.
We then use re to remove words in key from answer and put everything back together. This provides a more natural answer than just displaying key values.
import re
from difflib import SequenceMatcher
def similarity(a, b):
return SequenceMatcher(None, a, b).ratio()
dictionary2 = {
"chicken":"chicken two leg animal",
"fish":"fish is animal that live under water",
"cow":"cow is big vegetarian animal",
"space monkey":"monkey live in space",}
user_input = raw_input("User Question:")
#Check which key has greater probability match
similarity_list = []
for i in dictionary2.keys():
similarity_list.append((i,similarity(user_input,i)))
key_match = max(similarity_list, key=lambda x:x[1])
uin = ('|'.join(key_match[0].split()))
p = re.compile(r"\b(" + uin + ")\\W", re.I)
ans = p.sub('', dictionary2[key_match[0]])
print "answer: {} {}".format(key_match[0], ans)
Result
Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
User Question:what is a chicken?
answer: chicken two leg animal
>>> ================================ RESTART ================================
>>>
User Question:Where does space monkey live?
answer: space monkey live in space
>>> ================================ RESTART ================================
>>>
User Question:Where does fish live?
answer: fish is animal that live under water
>>> ================================ RESTART ================================
>>>
User Question:what is a cow?
answer: cow is big vegetarian animal
>>>
The problem with your code is when you use for w in input_ls and what you passed was "space monkey", it looks for space, then it looks for monkey. If you want the desired results with this specific script, it'd look like this
print "how can i help you?"
print
user_input = raw_input()
print
print "You asked: " + user_input + "."
response = "I will get back to you. "
input_ls = user_input
processor = {
"dictionary2":False,
"dictionary_lookup":[]
}
if input_ls in dictionary2:
processor["dictionary2"] = True
processor["dictionary_lookup"].append(input_ls)
if processor["dictionary2"] is True:
dictionary_lookup = processor["dictionary_lookup"][0]
translation = dictionary2[dictionary_lookup]
response = "what you were looking for is: " + translation
print
print "Response: " + response
note I also changed input_ls = user_input.split(" ") to input_ls = user_input because that turns your string into an array of individual words, which wouldn't return what you're looking for if you're trying to look up specific phrases instead of individual words, and made this important change here
if input_ls in dictionary2:
processor["dictionary2"] = True
processor["dictionary_lookup"].append(input_ls)
--edit--
I had to clock out for work, but now that I'm home I can address this better. When trying to accomplish this goal using a dictionary, here's how I would've done it.
dictionary2 = {
"red":"the color red",
"blue":"fish is animal that live under water",
"red and blue":"these colors make blue",
"blue and yellow":"these colors make green"
}
user_input = raw_input('what would you like?\t')
user_input = user_input.split(' ')
print
for word in user_input:
for key,val in dictionary2.iteritems():
if word in key:
print '%s: %s' % (key,val)
When trying to iterate through a dictionary, you need to use either:
dictionary2.iteritems() for both key and val
dictionary2.iterkeys() for your keys
dictionary2.itervalues() for your values

Read in lines of code from a file and randomly execute one of the lines

I might have explained it weirdly in the title, but here is the issue I have. I am making a small sentence-generating program and to choose the sentence, it chooses a random sentence structure.
I want to have a file with the different structure codes on separate lines, like this:
random.choice(blankThat)+" "+sentenceSubject+" "+random.choice(description)+"."
random.choice(questionBegin)+" "+sentenceSubject+" "+random.choice(pastDescription)+"?"
random.choice(pastBegin)+" "+sentenceSubject+" "+random.choice(pastDescription)+"."
random.choice(subjectBegin)+" "+sentenceSubject+"."
random.choice(subjectQuestion)+" "+sentenceSubject+"?"
sentenceSubject+" "+random.choice(description)+"."
sentenceSubject+" "+random.choice(pastDescription)+"."
random.choice(subjectBeginExclaim)+" "+sentenceSubject+"!"
random.choice(songList)+" "+random.choice(["is","was"])+" "+random.choice(adverbs)+" "+random.choice(adjectives)+"."
sentenceSubject+" "+"is"+" "+random.choice(describers)+"."
How would I then randomly choose to execute one of the above lines of code? I tried using this simple code to randomly choose one...
templateFile = open("structures.txt","a+")
templates = templateFile.readlines()
templates = [y.strip() for y in templates]
finalSentence = random.choice(templates)
But when I print(finalSentence), it just spits out one of the lines instead of executing it:
random.choice(pastBegin)+" "+sentenceSubject+" "+random.choice(pastDescription)+"."
How can I just randomly choose and execute one of the lines? I'd prefer it if I can read in the structures from a file, as I will regularly be adding new sentence structures.
Here's a sketch of what you can do. It looks like each line of your file uses three types of expressions:
string literals like " is ",
references to constant strings, like sentenceSubject, and
random choices from string collections, like random.choice(blankThat).
Create a mini-language that can recognize these expressions. E.g.:
?blankThat " " !sentenceSubject " " ?description "."
Create a dictionary of all constant strings, e.g.:
strings = {"sentenceSubject" : "Hello, world", ...}
Create a dictionary of all string collections, e.g.:
collections = {"blankThat" : ["foo", "bar", ...],
"description" : ["dog", "cat", ...], ...}
Write a mini-parser that takes a string written in your mini-language, breaks it into expressions, determines the type of each expression by the first character of the token, and converts it to the proper string:
?X -> random lookup, find X in collections, call random.choice(collections[X])
!X -> constant string, find strings[X]
"X" -> string literal, just use X
Finally, combine all translated pieces. Hope it helps.
This is not a direct answer to your question, but I think it could prove helpful. Consider different ways of constructing and generating this information. Here is a simple and imperfect example, but I think it could be a good place to start:
import random
subject = "Jeremy"
descriptions = ["cool", "tall", "strong"]
hobbies = ["running", "coding"]
def sentence_maker3000():
sentence_vals = {"subject": subject, "descriptions": random.choice(descriptions), "hobbies": random.choice(hobbies)}
valid_sentences = ["{subject} is {descriptions}", "{subject} likes {hobbies}"]
sentence = random.choice(valid_sentences).format(**sentence_vals)
return sentence
print(sentence_maker3000()) # Might print "Jeremy is cool" or "Jeremy likes coding"
You can construct all your valid sentences using Python's formatting brackets. Very easy to read and much shorter to write.
You can write these valid sentences in a separate text file like so:
{subject} is {descriptions}
{subject} likes {hobbies}
and then replace the valid_sentences assignment with:
with open('input.txt', 'r') as f:
valid_sentences = f.read().splitlines()
Use
print(eval(finalSentence))
instead of print(finalSentence). What eval(str) does is it takes a string and runs it like it would have been normal code.

Python Create a List file and write query

so sorry for my question if it seems so easy but I am newbie user of python and I can not find a way to solve it.
I have a "dish.py" file which includes some sub-lists
Fruits={"Ap":Apple
"Br":Black Mulberry
"Ch":Black Cherry
}
Meals={"BN":Bean
"MT":Meat
"VG":Vegetable
}
Legumes={"LN":Green Lentil
"P": Pea
"PN":Runner Peanut
}
I want to impelement the dish.py file in a code that at the end, I want to create a query inside of the file
with open("/home/user/Py_tut/Cond/dish.py", 'r') as dish:
content = dish.read()
print dish.closed
dm=dict([dish])
nl=[x for x in dm if x[0]=='P']
for x in dm:
x=str(raw_input("Enter word:"))
if x in dm:
print dm[x]
elif x[0]==("P"):
nl.append(x)
print .join( nl)
It may be look so messy but
dm=dict([dish]) I want to create a dictionary for query
nl=[x for x in dm if x[0]=='P'] I want to write words begin with "P" letter
Here is my questions:
1. Q: I suppose there is a problem with my dish.py file. How can I reorganize it?
2. Q: How can I apply a query to the file and extract the words begin with "P"
Thank you so much in advance
dict() can't load strings:
>>> dict("{'a': 1, 'b': 2}")
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
dict("{'a': 1, 'b': 2}")
ValueError: dictionary update sequence element 0 has length 1; 2 is required
As a sequence it would be ("{", "'", "a", "'", ":",...
Instead I would use the json module, change the dish.py format (changing extension to .json and using JSON syntax) and change the code.
dish.json
{
"Fruits": {
"Ap": "Apple",
"Br": "Black Mulberry",
"Ch": "Black Cherry"
},
"Meals": {
"BN": "Bean",
"MT": "Meat",
"VG": "Vegetable"
},
"Legumes": {
"GL": "Green Lentin",
"P": "Pea",
"PN": "Running Peanut"
}
}
__init__.py
import json
with open("/home/user/Py_tut/Cond/dish.py", 'r') as dish:
content = dish.read()
print(dish.closed)
dm = json.loads(content) # loads JSON
nl=[x for x in dm if x[0]=='P']
for x in dm:
x = str(raw_input("Enter word:"))
if x in dm:
print dm[x]
elif x[0] == ("P"):
nl.append(x)
print "".join(nl)
Q: How can I apply a query to the file and extract the words begin with "P" Thank you so much in advance
Assuming that you want to get every string separated by either space or newline and return them into a list, i'd do this:
import re #Importing RegExp module
def wordsBeginP():
with open("words.txt") as wordsfile: # Querying a file
words = wordsfile.open
parsed = re.sub(r"\n", " ", words) # Replace \n to " "
return [for i in parsed.split(" ") if i[0] == "P"] # Return list of words
So I think you have more than these two issues/questions.
First, if you want to include 'hardcoded' lists, dicts and such, you probably want to include dish with dish.py being in your working directory.
That is, if your data structures in the python file are actually in the correct form:
Fruits={"Ap":'Apple',
"Br":'Black Mulberry',
"Ch":'Black Cherry'
}
Meals={"BN":'Bean',
"MT":'Meat',
"VG":'Vegetable'
}
Legumes={"LN":'Green Lentil',
"P":'Pea',
"PN":'Runner Peanut'
}
Finally, you can search in all the datastructures that were named and included in the file, under the created namespace of the include (which is dish).
for f in [dish.Fruits,dish.Meals,dish.Legumes]:
for k,v in f.items():
if k.startswith('P'):
print k,v
Also interesting for you might be pickling (though there are some caveats).

How can I test for different strings in a string and act differently upon some of them? Python

What I want to do is look for different strings in a string and act differently upon some of them. Ths is what I have now:
import re
book = raw_input("What book do you want to read from today? ")
keywords = ["Genesis", "genesis", "Gen", "Gen.", "gen", "gen.", "Matthew", "matthew", "Matt", "Matt.", "matt", "matt." ]
if any(keyword in book for keyword in keywords):
print("You chose the book of: " + book)
I plan to change the "print" at the end to another action later on. So basicly if the user inputs the string "Genisis" then it will take action #1 and if the user inputs "Gen." it will also take action #1 as with all the other forms of the string "Genisis" but if the user inputs the string "Matthew" I want it to take action #2 and it should take action #2 with all the other variations of matthew.
I considered something like this:
book = raw_input("What book do you want to read from today? "
if book == "Genesis":
print "Genesis"
but that would require lots of lines for all the variations I have listed of "genesis"
I hope someone can help!
book = raw_input("What book do you want to read from today? ").lower().strip('.')
# keywords = ["Genesis", "genesis", "Gen", "Gen.", "gen", "gen.", "Matthew", "matthew", "Matt", "Matt.", "matt", "matt." ]
if book == 'genesis':
#action1
pass
elif book == 'gen':
#action2
pass
else:
print('not find the book!')
Using slices would still require you to write an if statement, but it would make the reduce the amount of code needed:
if book in keywords[:6]:
print "Genesis"
You can use a for loop and test for the containment of a book in any of a unique set of keywords. Whatever variation the book input takes, str.lower ensures you can find it in a keyword and take action based on the keyword:
actions = {...} # dictionary of functions
keywords = ['genesis', 'matthew', ...]
book = raw_input("What book do you want to read from today? ")
for kw in keywords:
if book.lower() in kw:
actions[kw]() # take action!
break # stop iteration

Categories