Skip to content
Advertisement

Add string and integer from one line string generate by speech recognizer to 2 separate list

I have some string from speech recognizer and the value is random but in the same form (x,y…n)

MyOrder = 'orange 2 grape 3 apple 4 mango 5 banana 1'

How to add string and integer into separate list in the correct order like MyOrder? or to filter x,y (string,int or vice versa) along those strings in one line for e.g:

fruit_cart = ['orange', 'grape', 'apple', 'mango', 'banana']
fruit_quantity = [2,3,4,5,1]

I tried to add the list string using conditional if statement, but the order were not the same as MyOrder string.

import numpy as np
import re
import speech_recognition as sr

fruit_cart = []
fruit_quantity = []
fruit_price = []
gross_price = []

recognizer = sr.Recognizer()
with sr.Microphone() as source2:
    recognizer.adjust_for_ambient_noise(source2, duration=0.9)
    audio2 = recognizer.listen(source2)
    MyOrder = recognizer.recognize_google(audio2)
    MyOrder = MyOrder.lower()
    print("You said: " +MyOrder)
    if "apple" in(MyOrder) :
        fruit_cart.append("apple")
        fruit_price.append(5)
    if "banana" in(MyOrder) :
        fruit_cart.append("banana")
        fruit_price.append(7)
    if "grape" in (MyOrder):
        fruit_cart.append("grape")
        fruit_price.append(9)
    if "mango" in (MyOrder):
        fruit_cart.append("mango")
        fruit_price.append(6)
    if "orange" in (MyOrder):
        fruit_cart.append("orange")
        fruit_price.append(8)
    q = [int(x) for x in re.findall('(d+)', MyOrder)]
    fruit_quantity.extend(q)
    total_fruit = len(fruit_cart)
    print(fruit_cart)
    print(fruit_quantity)
    print(fruit_price)
    print(total_fruit)
    gross_price = np.multiply(fruit_quantity,fruit_price)

total_price = 0
for b in range(total_fruit):
    print("%i t %st    %s tt%itt   %i" % (b + 1, fruit_cart[b], fruit_price[b], fruit_quantity[b], gross_price[b]))
    total_price = total_price + gross_price[b]
tax = total_price * 10/100
final_price = total_price + tax

I’m new to python by the way

Advertisement

Answer

The problem in your code is that it will work in the same order than the ifs statements. So apples will always be first if they appear inMyOrder, then banana, grape, etc. You want to fill your lists it in order of appearence in MyOrder.

I would convert the string into a list and iterate the list instead of using if statements. For the prices, I would define a dictionary with the price of each fruit. Here’s how I would do it:

prices = {"apple": 5, "banana": 7, "grape": 9, "mango": 6, "orange": 8}

with sr.Microphone() as source2:
    recognizer.adjust_for_ambient_noise(source2, duration=0.9)
    audio2 = recognizer.listen(source2)
    MyOrder = recognizer.recognize_google(audio2)
    MyOrder = MyOrder.lower()
    print("You said: ", MyOrder)

word_list = MyOrder.split(" ")
fruit_cart = word_list[::2]
fruit_quantity = [int(n) for n in word_list[1::2]]
fruit_price = [prices[fruit] for fruit in fruit_cart]
total_fruit = len(fruit_cart)
gross_price = np.multiply(fruit_quantity,fruit_price)

total_price = 0
for b in range(total_fruit):
    print("%i t %st   Rp %s tt%itt  Rp %i" % (b + 1, fruit_cart[b], fruit_price[b], fruit_quantity[b], gross_price[b]))
    total_price = total_price + gross_price[b]
tax = total_price * 10/100
final_price = total_price + tax

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement