0

I have a text file with chicken, recipe for chicken as line 1 and beef, beef recipe as line 2.

I can search for the beef or chicken recipe if the user only types beef or chicken. But I want to search for beef recipes if the user types in "I would like to search for beef recipes" or chicken....

I am trying to use .split but not quite there. Below is my code.

while True:
    food = input ("What food? ")
    file = open("meals.txt", "r")
        line = file.readline()
        data = line.split(",")
        if data[0] == food:
            print(data[1])
    file.close()
bob1978
  • 11
  • 4
  • if you know that the first word is the keyword you could do something like `if keyword in food:` to check if the keyword is anywhere in the inputted phrase. – Tadhg McDonald-Jensen May 19 '16 at 12:13
  • Is the issue how to search the text, or how to get keywords out of the question? – Scott Hunter May 19 '16 at 12:15
  • Use this piece of code http://stackoverflow.com/questions/6531482/how-to-check-if-a-string-contains-an-element-from-a-list-in-python/6531704#6531704 to search if the input phrase has any word that is present in list1 = ['chicken','beef'] – dot.Py May 19 '16 at 12:16
  • I would suggest - file = open("meals.txt", "r") this line to make outside loop and examine break condition in loop – Nikhil Rupanawar May 19 '16 at 12:22
  • I'm sorry, but this question in an incomplete specification. You must define what is a word (or this is the same what is a separator), or as an alternative if you want to accept the words as substring. Examples: *I want eggs,beef,veal or goose* (sep is comma) or at the opposite *The beefeater wants chicken recipes* – Serge Ballesta May 19 '16 at 12:27

5 Answers5

0

You will want to make the "food" item into a list, and iterate over it.

food_type = food.split()
for type in food_type:
    if type == 'beef':
        #do something
    elif type == 'chicken':
        #do something else
0

Ideally you should be using elasticsearch for this. However if you want to do string parsing.

while True:
food = input ("What food? ")
food_tokens = food.split()
file = open("meals.txt", "r")
    line = file.readline()
    data = line.split(",")
    for food_token in food_tokens:
        if data[0] == food_token:
            print(data[1])
file.close()

This checks for words "beef" and "chicken" and if input contains them, then it prints the data you want.

ajays20078
  • 368
  • 1
  • 3
  • 10
0

First you'll need some sort of list of valid search words, in this case keywords = ['beef', 'chicken']. Otherwise, if someone were to search for 'a chicken recipe' you might also get a hit on 'recipe' in 'a tasty beef recipe'. Then you find all valid keywords in the user input, and then match the valid ones to the meals:

keywords = ['beef', 'chicken']
userinput = input("what food? ")

# generate a list of meals
with open("meals.txt", "r") as file:
  meals = file.readlines()

for word in userinput.split():
  if word in keywords:
    for meal in meals:
      if word in meal:
        # yay found one, store it or something
Swier
  • 4,047
  • 3
  • 28
  • 52
0

You can use a regex:

import re

foodType = re.search('(beef|chicken)', food)
if foodType is None:
    print 'beef or chicken, make your choice.'
else:
    print foodType.group(1) 
    """whatever you meant to do with beef or chicken instead.
       foodType.group(1) is 'chicken' or 'beef'
    """

In there if foodType is None, it means your user didn't type chicken or beef, at all. Otherwise foodType.group(1) is set to chicken, or beef, whichever one was entered. I just put a print, but from there you can do whatever you want with it.

quemeraisc
  • 504
  • 2
  • 8
0
file = open("txt", "r")
all_lines = file.readlines()

while True:
    food = raw_input ("What food? ")
    for line in all_lines:
        data = line.split(',')
        if data[0] == food:
            print "Found %s" % food
            print "data[1] is %s" % data[1]
            break
    else:
        print "Not found %s" % food
Nikhil Rupanawar
  • 4,061
  • 10
  • 35
  • 51