I have a python Code that will recognize speech using the Google STT engine and give me back the results but I get the results in strings with "quotes". I don't want that quotes in my code as I will use it to run many commands and it doesn't work. I haven't tried anything so far as I didn't get anything to try! This is the function in the python code that will recognize speech:

我有一个python代码,可以识别使用谷歌STT引擎的语音并给我回复结果,但我得到的结果是带有“引号”的字符串。我不想在我的代码中使用引号,因为我将使用它来运行许多命令,但它不起作用。到目前为止,我没有尝试任何东西,因为我没有尝试任何东西!这是python代码中将识别语音的函数:

def recog():
    p = subprocess.Popen(['./speech-recog.sh'], stdout=subprocess.PIPE,
                                            stderr=subprocess.PIPE)
    global out,err
    out, err = p.communicate()
    print out

This is speech-recog.sh:

这是speech-recog.sh:

#!/bin/bash

hardware="plughw:1,0"
duration="3"
lang="en"
hw_bool=0
dur_bool=0
lang_bool=0
for var in "$@"
do
    if [ "$var" == "-D" ] ; then
        hw_bool=1
    elif [ "$var" == "-d" ] ; then
        dur_bool=1
    elif [ "$var" == "-l" ] ; then
        lang_bool=1
    elif [ $hw_bool == 1 ] ; then
        hw_bool=0
        hardware="$var"
    elif [ $dur_bool == 1 ] ; then
        dur_bool=0
        duration="$var"
    elif [ $lang_bool == 1 ] ; then
        lang_bool=0
        lang="$var"
    else
        echo "Invalid option, valid options are -D for hardware and -d for duration"
    fi
done

arecord -D $hardware -f S16_LE -t wav -d $duration -r 16000 | flac - -f --best --sample-rate 16000 -o /dev/shm/out.flac 1>/dev/shm/voice.log 2>/dev/shm/voice.log; curl -X POST --data-binary @/dev/shm/out.flac --user-agent 'Mozilla/5.0' --header 'Content-Type: audio/x-flac; rate=16000;' "https://www.google.com/speech-api/v2/recognize?output=json&lang=$lang&key=key&client=Mozilla/5.0" | sed -e 's/[{}]/''/g' | awk -F":" '{print $4}' | awk -F"," '{print $1}' | tr -d '\n'

rm /dev/shm/out.flac

This was taken from Steven Hickson's Voicecommand Program made for Raspberry Pi

这取自Steven Hickson为Raspberry Pi制作的Voicecommand Program

5 个解决方案

#1


44

Just use string methods .replace() if they occur throughout, or .strip() if they only occur at the start and finish:

如果它们在整个过程中出现,只需使用字符串方法.replace(),如果它们仅在开始和结束时出现,则使用.strip():

a = '"sajdkasjdsak" "asdasdasds"' 

a.replace('"', '')
'sajdkasjdsak asdasdasds'

# or, if they only occur at start and finish
a.strip('\"')
'sajdkasjdsak" "asdasdasds'

更多相关文章

  1. 自动完成在VS代码和Python中的自动化对象
  2. 如何删除第一个和最后一个双引号
  3. 在生产中是否应该减少服务器代码?
  4. 支持c和python之间的跨语言(c)标记的代码编辑器
  5. 【小白自学笔记】【机器学习实战】【Python代码逐行理解】CH02
  6. 三个猜数字游戏代码(Python)
  7. re表达式中单引号内的双引号(python)[duplicate]
  8. 在混合的Bash-Python代码片段中,变量的双引号和单引号
  9. 八大经典排序算法基本思想及代码实现(插入排序,希尔排序,选择排序,

随机推荐

  1. 系出名门Android(6) - 控件(View)之DateP
  2. android随笔
  3. android学习之RelativeLayout
  4. Android Power Manager分析(转载整理)
  5. android widget之TextView
  6. Android TextView跑马灯效果
  7. android中控件的大部分属性解释
  8. Android OpenGL ES 开发教程小结
  9. Android EditText属性
  10. Android SDK下载用的代理