示例#1
0
def search_dish_name(text):
    """Searches for text in the dishes database. If not found, translates text and
    looks for similar dishes in database. Returns JSON data for dish or search results."""

    # timing information, can delete later.
    start = dt.datetime.now()

    results = {}
    if type(text) != unicode:
        text = text.decode('utf-8')
    if len(text) > 10:
        # Most dish names are 3-5 characters.
        # If Tesseract returned more than 10 characters, something probably went wrong.
        print "Input text is too long."
        return None
    else:
        # Find a matching dish, if it exists.
        match = Dish.find_match(text)
        if match:
            # If result is found, return JSON representation of dish.
            results = match.get_json()
            start = time_elapsed("Dish lookup", start)
        else:
            # If no dish is found, return translation data and similar dishes, if they exist.
            translation = translate(text)
            start = time_elapsed("Translation", start)
            results['translation'] = translation

            # Find similar dishes and add to results.
            if len(text) > 1:
                similar_dishes = Dish.find_similar(text)
                start = time_elapsed("Similar dish lookup", start)
                similar_json = []
                for similar_dish in similar_dishes:
                    dish_data = similar_dish.get_json_min()
                    similar_json.append(dish_data)

                if similar_json != []:
                    results['similar'] = similar_json

    return results
示例#2
0
def search_dish_name(text):
    """Searches for text in the dishes database. If not found, translates text and
    looks for similar dishes in database. Returns JSON data for dish or search results."""

    # timing information, can delete later.
    start = dt.datetime.now()

    results = {}
    if type(text) != unicode:
        text = text.decode('utf-8')
    if len(text) > 10:
        # Most dish names are 3-5 characters. 
        # If Tesseract returned more than 10 characters, something probably went wrong.
        print "Input text is too long."
        return None
    else:
        # Find a matching dish, if it exists.
        match = Dish.find_match(text)
        if match:
            # If result is found, return JSON representation of dish.
            results = match.get_json()
            start = time_elapsed("Dish lookup", start)
        else:
            # If no dish is found, return translation data and similar dishes, if they exist.
            translation = translate(text)
            start = time_elapsed("Translation", start)
            results['translation'] = translation

            # Find similar dishes and add to results.
            if len(text) > 1:
                similar_dishes = Dish.find_similar(text)
                start = time_elapsed("Similar dish lookup", start)
                similar_json = []            
                for similar_dish in similar_dishes:
                    dish_data = similar_dish.get_json_min()
                    similar_json.append(dish_data)

                if similar_json != []:
                    results['similar'] = similar_json

    return results
示例#3
0
def generate_food_chains():
    words = []
    food_words = Food_Word.get_all_words()
    dishes = Dish.get_all_dishes()
    words.extend(food_words)
    words.extend(dishes)

    # Generate Markov chains. 
    # Since dish names are short, I'm using an n-gram size of 1.
    for i in range(len(words)):
        word = words[i]
        for j in range(len(word) - 1):
            char = word[j]
            next = word[j + 1]
            if CHAINS.get(char):
                CHAINS[char].append(next)
            else:
                CHAINS[char] = [next]
示例#4
0
def generate_food_chains():
    words = []
    food_words = Food_Word.get_all_words()
    dishes = Dish.get_all_dishes()
    words.extend(food_words)
    words.extend(dishes)

    # Generate Markov chains.
    # Since dish names are short, I'm using an n-gram size of 1.
    for i in range(len(words)):
        word = words[i]
        for j in range(len(word) - 1):
            char = word[j]
            next = word[j + 1]
            if CHAINS.get(char):
                CHAINS[char].append(next)
            else:
                CHAINS[char] = [next]
示例#5
0
文件: dish_test.py 项目: niczy/hyo
 def testInsertEntity(self):
   dish = Dish()
   dish.put()
   self.assertEqual(1, len(Dish.query().fetch(2)))
示例#6
0
文件: dish_logic.py 项目: niczy/hyo
def add_by_category_key(category_key, name, image_key = None):
  category = check_get_cateogry(category_key)
  dish = Dish(restaurant_key = category.restaurant_key, category_key = category_key, name = name, img_key = image_key)
  dish.put()
  return dish
示例#7
0
文件: dish_logic.py 项目: niczy/hyo
def delete_by_id(dish_id):
  dish = Dish.get_by_id(dish_id)
  delete_by_key(dish.key)
示例#8
0
文件: dish_logic.py 项目: niczy/hyo
def get_all_by_restaurant_uid(restaurant_uid):
  restaurant_key = ndb.Key(Restaurant, restaurant_uid)
  check_get_restaurant(restaurant_key)
  return Dish.query(Dish.restaurant_key == restaurant_key).fetch()
示例#9
0
文件: dish_logic.py 项目: niczy/hyo
def get_all_by_category_key(category_key):
  check_get_cateogry(category_key)
  return Dish.query(Dish.category_key == category_key).fetch()