I need some help to fix my code to the right way - python

i need to translate from one to another language . what did i do wrong ?
language={}
language = {"Bounjour" : 'Hello',
"Comment allez vous?" : 'How are you?',
"Aurevoir" : 'Good Bye'
#User input
print 'Bounjour, Comment Allez vous, Aurevoir'
phrase = raw_input('Please enter a phrase to translate: ')
#result
print "Your sentence in English: ",
for phrase in language:
translates = language[words]
print translates

I see three errors:
The user's input is saved in a variable named phrase, but then the for loop uses that same variable as its iterator, so the user input is discarded.
words is not defined anywhere.
translates is not defined anywhere.
But beyond those errors, you don't even need a loop; just print language[phrase].

Related

Extracting words/phrase followed by a phrase

I have one text file with a list of phrases. Below is how the file looks:
Filename: KP.txt
And from the below input (paragraph), I want to extract the next 2 words after the KP.txt phrase (the phrases could be anything as shown in my above KP.txt file). All I need is to extract the next 2 words.
Input:
This is Lee. Thanks for contacting me. I wanted to know the exchange policy at Noriaqer hardware services.
In the above example, I found the phrase " I wanted to know", matches with the KP.txt file content. So if I wanted to extract the next 2 words after this, my output will be like "exchange policy".
How could I extract this in python?
Assuming you already know how to read the input file into a list, it can be done with some help from regex.
>>> wordlist = ['I would like to understand', 'I wanted to know', 'I wish to know', 'I am interested to know']
>>> input_text = 'This is Lee. Thanks for contacting me. I wanted to know exchange policy at Noriaqer hardware services.'
>>> def word_extraction (input_text, wordlist):
... for word in wordlist:
... if word in input_text:
... output = re.search (r'(?<=%s)(.\w*){2}' % word, input_text)
... print (output.group ().lstrip ())
>>> word_extraction(input_text, wordlist)
exchange policy
>>> input_text = 'This is Lee. Thanks for contacting me. I wish to know where is Noriaqer hardware.'
>>> word_extraction(input_text, wordlist)
where is
>>> input_text = 'This is Lee. Thanks for contacting me. I\'d like to know where is Noriaqer hardware.'
>>> word_extraction(input_text, wordlist)
>>>
First we need to check whether the phrase we want is in the sentence. It's not the most efficient way if you have large list but it works for now.
Next if it is in our "dictionary" of phrase, we use regex to extract the keyword that we wanted.
Finally strip the leading white space in front of our target word.
Regex Hint:
(?<=%s) is look behind assertion. Meaning check the word behind the sentence starting with "I wanted to know"
(.\w*){2} means any character after our phrase followed by one or more words stopping at 2 words after the key phrase.
I Think natural language processing could be a better solution, but this code would help :)
def search_in_text(kp,text):
for line in kp:
#if a search phrase found in kp lines
if line in text:
#the starting index of the two words
i1=text.find(line)+len(line)
#the end index of the following two words (first index+50 at maximum)
i2=(i1+50) if len(text)>(i1+50) else len(text)
#split the following text to words (next_words) and remove empty spaces
next_words=[word for word in text[i1:i2].split(' ') if word!='']
#return only the next two words from (next_words)
return next_words[0:2]
return [] # return empty list if no phrase matching
#read your kp file as list of lines
kp=open("kp.txt").read().split("\n")
#input 1
text = 'This is Lee. Thanks for contacting me. I wanted to know exchange policy at Noriaqer hardware services.'
print('input ->>',text)
output = search_in_text(kp,text)
print('output ->>',output)
input ->> This is Lee. Thanks for contacting me. I wanted to know exchange policy at Noriaqer hardware services.
output ->> ['exchange', 'policy']
#input 2
text = 'Boss was very angry and said: I wish to know why you are late?'
print('input ->>',text)
output = search_in_text(kp,text)
print('output ->>',output)
input ->> Boss was very angry and said: I wish to know why you are late?
output ->> ['why', 'you']
you can use this:
with open("KP.txt") as fobj:
phrases = list(map(lambda sentence : sentence.lower().strip(), fobj.readlines()))
paragraph = input("Enter The Whole Paragraph in one line:\t").lower()
for phrase in phrases:
if phrase in paragraph:
temp = paragraph.split(phrase)[1:]
for clause in temp:
print(" ".join(clause.split()[:2]))

Searching text to see if a list of words are there, and if there is, return the text around the word?

I have a function that currently searches text to see if any of my keywords are mentioned within it. I want to enhance this function to return the keyword found and 25 words after it is identified. My code is below, but will not work due to "word" not being identified:
def searching(text):
key_words = ["address","home", "location", "domicile"]
if any(word in text for word in key_words):
statement = text[text.find(word) + 1:].split()[0:20]
my_return = " ".join(statement)
return my_return
else:
return "No result"
text = I have a pretty good living situation. I am very thankful for my home located in Massachusetts.
I would expect my function to return "home located in Massachusetts" but I am getting an error.
NameError: name 'word' is not defined
Any ideas?
You can split string to the words and check the result list.
You are returning in function so it just returned just after the first iteration, you can provide keywords in the argument.
The result you are expecting can be obtained like this:
def searching(text):
key_words = ["address","home", "location", "domicile"]
for word in key_words:
if word in text.split():
statement = text[text.find(word) + 0:].split()[0:20]
my_return = " ".join(statement)
print(my_return)
else:
print("No result")
text = "I have a pretty good living situation. I am very thankful for my home located in Massachusetts."
print(searching(text))
Output
No result
home located in Massachusetts.
No result
No result
For returning the match when it matches the first time, you can do this and remove else.
def searching(text):
key_words = ["address","home", "location", "domicile"]
for word in key_words:
if word in text.split():
statement = text[text.find(word) + 0:].split()[0:20]
my_return = " ".join(statement)
return my_return
text = "I have a pretty good living situation. I am very thankful for my home located in Massachusetts. You can find me at my address 123 Happy Lane."
print(searching(text))
Output
address 123 Happy Lane.

Where could the error be in this programme, please?

I am grouped into groups for a project, and I am are expected to come up with as many famous scientists who have the same first letter of their name as I as possible. But I need to know whether i will have to come up with the answers on my own, or is there somebody in my group that I can work with?
The input is a string of my group members' names separated by spaces, and then a string of my name.
Expected Output:
A string that says "Compare notes" if I have a name buddy, or "No such luck" if I have to work on this alone.
group_members = input().split()
my_name = input()
for fellow in group_members:
if fellow[0] is my_name[0]:
print("Compare notes")
break
else:
print("No such luck")
break
My programme worked perfectly for all but one of the test cases. Please, where could the bug be here? Thanks for your support.
Try this
group_members = input().split()
my_name = input()
for fellow in group_members:
if fellow[0] == my_name[0]:
print("Compare notes")
break
else:
print("No such luck")
More on else for the for loop is in the docs.

Is there a way to recognize at least one word in the input?

For example
userDay = input("How was you day? ")
The user input "My day was good"
and then the program recognizes that the user said "good", and then chooses the correct response for "good".
Sorry if you don't understand, its hard to explain.
Easy and quick way would be split the response into words using response.split() and then check each word if it equal to good
This way you can avoid searching for ‘ good’ or ‘ good ‘ or ’good ‘ (good word can be starting word, ending word or somewhere in the line)
I'd suggest using python's built-in lexical analysis tool, shlex. It is very useful as it contains functionality that makes it easy to write a (simple) lexical analyzer.
So in your case you can do something like this:
import shlex
user_input = 'My day was good'
li = shlex.split(user_input)
print 'Your day was good' if 'good' in li else 'Your day was bad'
The prints here are for demonstrating purposes only. You have to substitute them with your code that will choose the correct response for "good".
Just check if a substring is in a string:
userDay = input("How was you day? ")
if 'good' in userDay:
print('good')
or you can use:
if userDay.find('good') == 1:
print('good')
Something like this:
response = input("How was you day? ")
if response == 'good':
# do something
Or if you're just looking for 'good' in the response somewhere:
response = input("How was you day? ")
if 'good' in response.lower():
# do something

How to pick key words from a list, after appending a .csv file to that list?

I need to be able to pick keywords from an Excel CSV file, I've appended the file to a list. The program is a Phone Troubleshoot, and I need the input ("The screen won't turn on") to have the same output as if I inputted ("The display is blank").
"Troubleshooting Program to give the user a solution to a trouble they've encountered based on inputted key words."
phoneprob=input("What problem are you having with your phone? ")
prob=open("phone.csv","r")
phone=prob.read()
prob.close()
eachProb=phone.split("\n")
print(eachProb)
problist=[eachProb]
print (problist)
Are you trying to build a keyword dictionary or retriving a sentence problem ?
In both case, you need to associate a problem to keywords.
A basic approch to get keywords is to split the sentence in words (using s.split()) and update keyword list with the most used of them...
difflib can help here.
Since we don't know the given file schema, i assume it's just a list of sentence and you provided keyword/problems elsewhere (situation Dict).
For example:
csv_ret = ["I can't turn on my phone", "The screen won't turn on", "phone The display is blank"]
situations = {
"screen": ["turn on", "blank", "display", "screen"],
"battery": ["turn on", "phone"]
}
def get_situation_from_sentence(sentence):
occurences = {}
for word in sentence.split():
for key, value in situations.items():
if word in value:
if occurences.get(key) is None:
occurences[key] = [word]
elif word not in occurences.get(key):
occurences[key].append(word)
averages = {k: ((len(v) * 100) / len(situations[k])) for k, v in occurences.items()}
return "{}, {}".format(averages, sentence)
for sentence in csv_ret:
print(get_situation_from_sentence(sentence))
results:
{'battery': 50.0}, I can't turn on my phone
{'screen': 25.0}, The screen won't turn on
{'screen': 50.0, 'battery': 50.0}, phone The display is blank
This code evaluate a sentence problems and related keyword match in percent.
Once again this is a very basic solution, and you probably need something more robust (lexer/parser, machine learning ...) but sometime simpler is better :)

Categories

Resources