How to use the results of PocketSphinx voice-recognition code? - python

I am trying to create a program that will constantly listen for speech, and if I say a certain string, it will call a certain function, example, if (string) then (function). I initially tried using Google Speech to Text API but could not get it to continuously listen (I even found a thread where someone said that Google Speech to Text API does not support continuous listening), so I reverted to PocketSphinx.
I have barely any code at all. I just have the initial beginner code you try out when you first install and run PocketSphinx (because I am a beginner). However, I don't want to do much. I created my own dictionary of a whopping 1 words, because I only want it to do one thing, and it reads it fine.
The problem I am having is actually using the string value that it constantly listens for to run future code. I don't know if I would have to add code to make it stop listening for a few moments after it hears a word in order for it to do something with the word is just heard or.... Also, I've heard that when you are comparing strings, you would use .equals() rather than ==, correct me/elaborate if I'm wrong. I've tried it both ways. When I use ==, nothing happens, it just continuously listens and prints the one word I say. When I use .equals(), I get an AttributeError.
First Method:
import os
from pocketsphinx import LiveSpeech, get_model_path
speech = LiveSpeech(
sampling_rate = 16000,
hmm=os.path.join(get_model_path(), 'en-us'),
lm='A:...(some random directory to a .lm file)',
dic='A:...(some random directory to a .dic file)')
for code in speech:
print(code)
if code == "string":
print("It works!")
Second Method:
import os
from pocketsphinx import LiveSpeech, get_model_path
speech = LiveSpeech(
sampling_rate = 16000,
hmm=os.path.join(get_model_path(), 'en-us'),
lm='A:...(some random directory to a .lm file)',
dic='A:...(some random directory to a .dic file)')
for code in speech:
print(code)
if code.equals(myString): #myString is set to the word I say to it.
print("It works!")
Second Method Error:
'LiveSpeech' object has no attribute 'equals'

You need to use live speech as if it were an iterator (like a list)
for example:
for phrase in speech:
text = phrase.hypothesis()

Related

Interact to remote console by sending text with a loop with Python/pwntools

I'm playing with an remote console that asks me to return every word it gives.
For example :
>>> car # Remote console gives a word
car # I answer
Ok next word ! # Remote console after checking
>>> house # Remote console gives a second word and is waiting for me
I could manually answer each word the console says. But I'd like to make a script to automate it.
I'm thinking about a loop that would do this job, that is to say :
get the word from the remote console
send that word back to the remote console
I tried it with the pwntools Python library by using the recvline() and sendline() commands. I wrote :
import pwn
import re
c = pwn.remote ("URL", port)
question = str(c.recvline())
c.sendline(question)
c.interactive()
By doing this, the console returns :
Ok next word !
>>> house
That's a start but obviously as there's no loop, the script can't send "house" back. To adapt it, I used while and for loops on the recvline command but without results after many tries. And I must admit I'm getting stuck.
Is there a trick to do it and put each try inside the >>> input of the service ?
Any help would be very appreciated.
Thanks.

Why is my code not appending these results?

I am trying to learn how to use api's by writing some code which returns words that you can make from some letters you give it for a game called wordscape.
The code runs without errors but I am getting very confused as to why it only appends some of the correct results, not all of them.
Here is an example of the problem, using a snippet of the code:
import requests
test=["we","ew"]
for word in test:
parameters={"define":word}
r=requests.get("https://googledictionaryapi.eu-gb.mybluemix.net",params=parameters)
liste=[]
if r.status_code==200:
liste.append(word)
Lets say I decide to input the letters we, there are 2 possible arrangements of those letters, ew and we. Both of these are words in the dictionary api I am using, however if you run the above code, it only appends ew.
However, if you remove ew from test, so that it only has we, it will append we.
What is going on here?
Note: Here is the GitHub for the api: https://github.com/meetDeveloper/googleDictionaryAPI
Hope this helps!
import requests
test=["we","ew"]
liste=[] #Redeclaring the list inside the loop will eliminate previously stored values and as such the scope of the list should be outside the loop.
for word in test:
parameters={"define":word}
r=requests.get("https://googledictionaryapi.eu-gb.mybluemix.net",params=parameters)
if r.status_code==200:
liste.append(word)

Basics of connecting python to the web and validating user input

I'm relatively new, and I'm just at a loss as to where to start. I don't expect detailed step-by-step responses (though, of course, those are more than welcome), but any nudges in the right direction would be greatly appreciated.
I want to use the Gutenberg python library to select a text based on a user's input.
Right now I have the code:
from gutenberg.acquire import load_etext
from gutenberg.cleanup import strip_headers
text = strip_headers(load_etext(11)).strip()
where the number represents the text (in this case 11 = Alice in Wonderland).
Then I have a bunch of code about what to do with the text, but I don't think that's relevant here. (If it is let me know and I can add it).
Basically, instead of just selecting a text, I want to let the user do that. I want to ask the user for their choice of author, and if Project Gutenberg (PG) has pieces by that author, have them then select from the list of book titles (if PG doesn't have anything by that author, return some response along the lines of "sorry, don't have anything by $author_name, pick someone else." And then once the user has decided on a book, have the number corresponding to that book be entered into the code.
I just have no idea where to start in this process. I know how to handle user input, but I don't know how to take that input and search for something online using it.
Ideally, I'd be able to handle things like spelling mistakes too, but that may be down the line.
I really appreciate any help anyone has the time to give. Thanks!
The gutenberg module includes facilities for searching for a text by metadata, such as author. The example from the docs is:
from gutenberg.query import get_etexts
from gutenberg.query import get_metadata
print(get_metadata('title', 2701)) # prints frozenset([u'Moby Dick; Or, The Whale'])
print(get_metadata('author', 2701)) # prints frozenset([u'Melville, Hermann'])
print(get_etexts('title', 'Moby Dick; Or, The Whale')) # prints frozenset([2701, ...])
print(get_etexts('author', 'Melville, Hermann')) # prints frozenset([2701, ...])
It sounds as if you already know how to read a value from the user into a variable, and replacing the literal author in the above would be as simple as doing something like:
author_name = my_get_input_from_user_function()
texts = get_etexts('author', author_name)
Note the following note from the same section:
Before you use one of the gutenberg.query functions you must populate the local metadata cache. This one-off process will take quite a while to complete (18 hours on my machine) but once it is done, any subsequent calls to get_etexts or get_metadata will be very fast. If you fail to populate the cache, the calls will raise an exception.
With that in mind, I haven't tried the code I've presented in this answer because I'm still waiting for my local cache to populate.

Telnet using Python

hi I am new to python and I am trying to telenet to my host connected via Host-only adapter :
My command are
import telnetlib
import time
def call_func():
time1 = 2
connect = telnetlib.Telnet('192.168.1.100',23,3)
connect.write('show version'.encode('ascii'))
time.sleep(time1)
print (connect.read_very_eager().decode('ascii'))
connect.close()
call_func()
However I am not able to read the full output of the show version command. Can someone explain why I am not able to do so?
Output got:
'R1>show version'
You can read about all read_ methods in telnetlib documentation and compare their outputs. It states there, that only read_until() will give you text "until a given byte string". All of the other ones will return only "all data until EOF", "everything that can be without blocking in I/O", "readily available data." etc.
That said, you should use read_until() to be sure you get a full string returned. This in the only method that waits for telnet to return the whole text.
Additional explanation can be found here in a similar question.

Python Terminal Menu? Terminal Coloring? Terminal progress display?

I have a project that uses Python (2.* flavors) extensively and I am wondering if there is a terminal menu library or something to that effect? I am looking to breathe some flavor and life into my script by simplifying some of the options using arrow key highlightable options, some color, etc etc. I vaguely recall there being a way to make a bash shell terminal menu but I'm not at all sure how I would pass user input from bash to the python script, perhaps have a bash terminal menu push the script call with sysarggs? I'd like something on the python side if possible. Any suggestions?
Also just a random question, kind of fits in here since we're on the topic of terminal aesthetics, what's the best way to handle a counter? My script looks for image files, then when it finds one it clears the terminal with a subprocess call to clear and then prints the total images found again IE 10 images, find one, clear, print "11 images found", sometimes my script works REAL fast and I feel this detriments performance. Thoughts?
Thanks so much everyone, I love stack overflow ;)
Edit - Thanks for all the quick responses! I have alot of options to mull over. I gave everyone an upvote because all of your responses are helpful. I will check out all the libraries when I get home and try to pick one of you for an answer depending on what hashes out the best, wish I could pick you all though because all of your answers are relevant! Much appreciated folks. I'll report back in once I get home from work and get a chance to get some coding in ;)
Edit 2 - A clarification on the counter/progress display, looking for a way to keep this from detrimenting performance when my script finds thousands of images in a very short ammount of time, this is real chopped up python...
for each item in list:
if item ends with .jpg
cnt=cnt+1
do stuff with image file
subprocess.call('clear')
print str(cnt)+" total images processed."
Thanks again!
Check out Clint (*C*ommand *L*ine *IN*terface *T*ools)!
Official page: https://github.com/kennethreitz/clint
Great overview: http://www.nicosphere.net/clint-command-line-library-for-python/
Example colors:
from clint.textui import colored
print 'I love ' + colored.yellow('pyt') + colored.blue('hon')
and indents too:
from clint.textui import colored, indent, puts
with indent(3, quote=colored.red(' >')):
puts ('some random text')
puts ('another text')
with indent(3, quote=colored.green(' |')):
puts('some more nested identation')
puts('cool isn\'t?')
P.S. The same author wrote a similarly nice HTTP request library called "requests": https://github.com/kennethreitz/requests
If you want a lot of control and you're on *nix, you could use the stdlib curses module.
If you just want a bit of color (/don't want to modify your script a ton to fit curses), you can use ANSI escape codes. For example:
print '\033[1;32mgreen\033[1;m'
will print the word 'green' colored... green.
Here's a loading bar I came up with using carriage returns (based on the answers in this forum):
from time import sleep
import sys
num = 100
print 'Loading: [%s] %d%%' % (' '*(num/2), 0),
try:
colorCode = 43
for x in xrange(num+1):
if x == num: colorCode = 42
print '\rLoading: [\033[1;%dm%s\033[1;m%s] %d%%' % (colorCode, "|"*(x/2), " "*(num/2-x/2), x),
sys.stdout.flush()
sleep(0.02) # do actual stuff here instead
except KeyboardInterrupt:
print '\rLoading: [\033[1;41m%s\033[1;m%s] %d%% ' % ("|"*(x/2), " "*(num/2-x/2), x)
Example Output:
Loading: [||||||||||||||||||||||||||||||||||||||||| ] 82%
(Although it doesn't show up on SO, it is colored- yellow for loading, red for abort, and green for done.)
There's a library called Urwid that offers menus and some more. I never used it for serious purposes, but the it work pretty fine with my preliminary experiences on it. It works on Un*x systems only though. (The project page says it works under Cygwin, but I never tried.)

Categories