Alo*_*had 43 python string speech-recognition google-voice
我有一个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
Run Code Online (Sandbox Code Playgroud)
这是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
Run Code Online (Sandbox Code Playgroud)
这取自Steven Hickson为Raspberry Pi制作的Voicecommand Program
smc*_*mci 94
.replace()
如果它们在整个过程中出现,或者.strip()
仅在开始和/或结束时出现,请使用字符串方法:
a = '"sajdkasjdsak" "asdasdasds"'
a = a.replace('"', '')
'sajdkasjdsak asdasdasds'
# or, if they only occur at start and end...
a = a.strip('\"')
'sajdkasjdsak" "asdasdasds'
# or, if they only occur at start...
a = a.lstrip('\"')
# or, if they only occur at end...
a = a.rstrip('\"')
Run Code Online (Sandbox Code Playgroud)
有几种方法可以实现这一点。
您可以使用内置字符串函数.replace()
来替换给定字符串中所有出现的引号:
>>> s = '"abcd" efgh'
>>> s.replace('"', '')
'abcd efgh'
>>>
Run Code Online (Sandbox Code Playgroud)您可以使用字符串函数.join()
和生成器表达式从给定字符串中删除所有引号:
>>> s = '"abcd" efgh'
>>> ''.join(c for c in s if c not in '"')
'abcd efgh'
>>>
Run Code Online (Sandbox Code Playgroud)您可以使用正则表达式从给定字符串中删除所有引号。这有一个额外的好处,让您可以控制何时何地删除引用:
>>> s = '"abcd" efgh'
>>> import re
>>> re.sub('"', '', s)
'abcd efgh'
>>>
Run Code Online (Sandbox Code Playgroud)您可以为此使用eval()
>>> url = "'http address'"
>>> eval(url)
'http address'
Run Code Online (Sandbox Code Playgroud)
尽管eval()带来了风险,但我认为在这种情况下它是安全的。
最简单的方法是:
s = '"sajdkasjdsaasdasdasds"'
import json
s = json.loads(s)
Run Code Online (Sandbox Code Playgroud)
这将删除字符串中的第一个和最后一个引号
import ast
example = '"asdfasdfasdf"'
result = ast.literal_eval(example)
print(result)
Run Code Online (Sandbox Code Playgroud)
输出:
asdfasdfasdf
Run Code Online (Sandbox Code Playgroud)
您可以用空字符串替换“引号”字符,如下所示:
>>> a = '"sajdkasjdsak" "asdasdasds"'
>>> a
'"sajdkasjdsak" "asdasdasds"'
>>> a = a.replace('"', '')
>>> a
'sajdkasjdsak asdasdasds'
Run Code Online (Sandbox Code Playgroud)
对于您的情况,您可以对变量执行相同的操作out
。
if string.startswith('"'):
string = string[1:]
if string.endswith('"'):
string = string[:-1]
Run Code Online (Sandbox Code Playgroud)