12 Replies - 1504 Views - Last Post: 01 September 2013 - 06:44 PM Rate Topic: -----

#1 Destroyer5  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 09-August 13

Dragonfly speech hazzard

Posted 10 August 2013 - 09:20 AM

Hi, I am a sophomore in high school and have been trying to recreate a jarvis system for fun but have stumble across a problem that just doesn't add up to the rest of the code from the website [http://pythonhosted.org/dragonfly/grammar.html] which describes what the grammar does.
The code is this
 
[/from dragonfly import *
from dragonfly import grammar
class ExampleRule(CompoundRule):

    spec = "I want to eat <food>"
    extras = [Choice("food", {
                              "(an | a juicy) apple": "good",
                              "a [greasy] hamburger": "bad",
                             }
                    )
             ]

    def _process_recognition(self, node, extras):
        good_or_bad = extras["food"]
        print "That is a %s idea!" % good_or_bad

rule = ExampleRule()
grammar.add_rule(rule)
print(grammar)
]

This is a sample program that they give on the website supporting the grammar yet it gives me the error that "'module' object has no attribute 'add_rule'". I've researched this and have found no results. It seems that dragonfly is good but needs proper instruction. I have to use dragonfly because I've already used pyspeech but i can't use that anymore because i needed a statemachine import for better command recognition. I found fysom which needs a python higher than 2.5. Currently i am using python 2.7
These last few days i've been hitting my head with a brick
Any help will be greatly appreciated!

This post has been edited by modi123_1: 10 August 2013 - 11:13 AM
Reason for edit:: please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Dragonfly speech hazzard

#2 DblAAssassin  Icon User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 246
  • Joined: 11-May 13

Re: Dragonfly speech hazzard

Posted 10 August 2013 - 12:12 PM

It seems like the error is pointing to line 18.
grammar.add_rule(rule)


Try changing it to...
grammar.add_rule(ExampleRule())
grammar.load()


I can't seem to find any official documentation for this Python Package. Here is the best documentation, I found on it!.
Was This Post Helpful? 1
  • +
  • -

#3 Destroyer5  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 09-August 13

Re: Dragonfly speech hazzard

Posted 10 August 2013 - 01:32 PM

I just tried that and still give me the same error and you were correct the error is with line 18. It doesn't like the whole grammar. The website you gave me is the one i have been using and also the one i got this example from yet it doesn't work. This whole package is really fishy yet there have been some people who have done it. I've seen by videos of it on youtube.
Was This Post Helpful? 0
  • +
  • -

#4 BlueMelon  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 187
  • Joined: 27-April 10

Re: Dragonfly speech hazzard

Posted 10 August 2013 - 01:55 PM

Ah it's a windows only library? Shame...

Make sure you have the library properly installed before running the example.

from dragonfly.all import Grammar, CompoundRule

# Voice command rule combining spoken form and recognition processing.
class ExampleRule(CompoundRule):
    spec = "do something computer"                  # Spoken form of command.
    def _process_recognition(self, node, extras):   # Callback when command is spoken.
        print "Voice command spoken."

# Create a grammar which contains and loads the command rule.
grammar = Grammar("example grammar")                # Create a grammar to contain the command rule.
grammar.add_rule(ExampleRule())                     # Add the command rule to the grammar.
grammar.load()  


Was This Post Helpful? 0
  • +
  • -

#5 Destroyer5  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 09-August 13

Re: Dragonfly speech hazzard

Posted 10 August 2013 - 01:57 PM

It is properly installed because i can load the "engine modulal" and get spai5 to talk through that but for voice input i need the grammar to work which won't. everything is fine besides the grammar
Was This Post Helpful? 0
  • +
  • -

#6 BlueMelon  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 187
  • Joined: 27-April 10

Re: Dragonfly speech hazzard

Posted 10 August 2013 - 01:59 PM

Well from what I can see you are not importing it correctly (see example.)
You also did not declare a grammar object, which is why you are getting errors. (see example.)

Edit:
Did you even try running the example from the website?

This post has been edited by BlueMelon: 10 August 2013 - 02:00 PM

Was This Post Helpful? 0
  • +
  • -

#7 Destroyer5  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 09-August 13

Re: Dragonfly speech hazzard

Posted 10 August 2013 - 02:07 PM

The example they gave me [from dragonfly.all import Grammar, CompoundRule

# Voice command rule combining spoken form and recognition processing.
class ExampleRule(CompoundRule):
spec = "do something computer" # Spoken form of command.
def _process_recognition(self, node, extras): # Callback when command is spoken.
print "Voice command spoken."

# Create a grammar which contains and loads the command rule.
grammar = Grammar("example grammar") # Create a grammar to contain the command rule.
grammar.add_rule(ExampleRule()) # Add the command rule to the grammar.
grammar.load() # Load the grammar.]
This runs but does nothing when i say anything. This whole thing confuses me.
Was This Post Helpful? 0
  • +
  • -

#8 BlueMelon  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 187
  • Joined: 27-April 10

Re: Dragonfly speech hazzard

Posted 10 August 2013 - 02:57 PM

At-least the example runs and you can build off of that. Maybe it's not picking up the microphone, there could be a setting for that in the docs.

If it confuses you I suggest you review some basic python concepts before continuing.
Was This Post Helpful? 0
  • +
  • -

#9 DblAAssassin  Icon User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 246
  • Joined: 11-May 13

Re: Dragonfly speech hazzard

Posted 11 August 2013 - 09:22 AM

View PostDestroyer5, on 10 August 2013 - 02:07 PM, said:

The example they gave me [from dragonfly.all import Grammar, CompoundRule

# Voice command rule combining spoken form and recognition processing.
class ExampleRule(CompoundRule):
spec = "do something computer" # Spoken form of command.
def _process_recognition(self, node, extras): # Callback when command is spoken.
print "Voice command spoken."

# Create a grammar which contains and loads the command rule.
grammar = Grammar("example grammar") # Create a grammar to contain the command rule.
grammar.add_rule(ExampleRule()) # Add the command rule to the grammar.
grammar.load() # Load the grammar.]
This runs but does nothing when i say anything. This whole thing confuses me.


From all the examples I see they say...
from dragonfly.all import Grammar, CompoundRule


Maybe try adding this to the beginning of your code, and the thing I said before to the end; so your code should look like this...
from dragonfly.all import Grammar, CompoundRule
class ExampleRule(CompoundRule):
    spec = "I want to eat <food>"
    extras = [Choice("food", {
                              "(an | a juicy) apple": "good",
                              "a [greasy] hamburger": "bad",
                             }
                    )
             ]
    def _process_recognition(self, node, extras):
        good_or_bad = extras["food"]
        print "That is a %s idea!" % good_or_bad

rule = ExampleRule()
grammar.add_rule(ExampleRule())
grammar.load()
print grammar


Also if your using Python 2.7 you don't need to parentheses around what your printing ie. line 19 in your original code.
Was This Post Helpful? 0
  • +
  • -

#10 DblAAssassin  Icon User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 246
  • Joined: 11-May 13

Re: Dragonfly speech hazzard

Posted 11 August 2013 - 05:20 PM

I think I know why you have the error!!!...
Pseudoprivate Attributes!
Although I can't find any good virtual documentation on this for "module privates", I can only find it for "class privates".
Anyways let me explain what it is...

Quote

Quoted from Python Pocket Reference by: Mark Lutz Published by: O'Reilly
Names in modules with a single underscore(e.g.,_X), and those not listed on the module's __all__ list, are not copied over when a client uses from module import *....

So I was thinking the CompoundRule, being a Pseudoprivate Module Private, would be the only logical explanation to why the code isn't working. This would mean if you attempted to import all the elements in the module by inputting from dragonfly import *, it would not import the Module Privates; there fore you would have to call out CompoundRule specifically. Well first does the code in my last post work?
Was This Post Helpful? 1
  • +
  • -

#11 Destroyer5  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 09-August 13

Re: Dragonfly speech hazzard

Posted 23 August 2013 - 06:44 PM

With your help i was able to get it to work and have created a great working speech module that can open music, search web, open any web browser avalible, tell time, tell the weather, click on startmenu, search the startmenu and much more. Thanks everyone for the help!!!!
Was This Post Helpful? 1
  • +
  • -

#12 DblAAssassin  Icon User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 246
  • Joined: 11-May 13

Re: Dragonfly speech hazzard

Posted 01 September 2013 - 09:13 AM

View PostDestroyer5, on 23 August 2013 - 06:44 PM, said:

With your help i was able to get it to work and have created a great working speech module that can open music, search web, open any web browser avalible, tell time, tell the weather, click on startmenu, search the startmenu and much more. Thanks everyone for the help!!!!

Would you mind sharing to the code?
Was This Post Helpful? 0
  • +
  • -

#13 Destroyer5  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 09-August 13

Re: Dragonfly speech hazzard

Posted 01 September 2013 - 06:44 PM

import os
import speech
import webbrowser
import pythoncom
import dragonfly
import dragonfly.log as log_
from pymouse import PyMouse
from pykeyboard import PyKeyboard
from dragonfly.engines.engine import get_sapi5_engine
songs = ["Famous last words", "The end", "Dead", "This is how i disappear",
        "The Sharpest lives", "Welcome to the black parade","Sleep", "Teenagers",
        "Disenchanted", "Hidden track","House of wolves", "Cancer", "Mama","I don't love you", "Exit"]
handle = webbrowser.get()
engine = get_sapi5_engine()
speech.standing_by = False
Ie = ("Internet Explorer")
Fire = ("Firefox")
Chrome = ("Google Chrome")
def pausable(f):
  
  "Decorator that causes callbacks to pause when Standing By."
  def newf(phrase, listener):
    if speech.standing_by:
      return 
    else:
      return f(phrase, listener)
  return newf

stored_words = []


def search_ing(phrase, listener):
      x = PyKeyBoard
      x.Type(phrase)
def Mouse(phrase, listener):
    if phrase == "Click on skype":
        m = PyMouse()
        m.click(15,10)
        m.move(40,150)
        time.sleep(2)
        m.click(40,150)
        m.screen_size()
        m.position()
    elif phrase == "Search menu":
        m = PyMouse()
        m.move(30,500)
        time.sleep(2)
        m.click(30,500)
        m.screen_size()
        m.position()
        LListen = speech.listenforanything(search_ing)

def Anything(phrase, listener):
    print phrase
    stored_words.append(phrase)
        
def Firefox_Google(phrase, listener):
    if phrase == "Click on Google Chrome":
        m = PyMouse()
        m.move(200,0)
        time.sleep(1)
        m.click(200,0,1)
        m.screen_size()
        m.position()
    elif phrase == "Click on Firefox":
        m = PyMouse()
        m.move(400, 10)
        time.sleep(1)
        m.click(400, 10)
        m.screen_size()
        m.position()
    elif phrase == "Click on Windows media player":
        m = PyMouse()
        m.move(330, 10)
        time.sleep(1)
        m.click(330, 10)
        m.screen_size()
        m.position()
    elif phrase == "Click on my folder":
        m = PyMouse()
        m.move(150,10)
        time.sleep(1)
        m.click(150,10)
        m.screen_size()
        m.position()
    elif phrase == "Click on start menu":
        m = PyMouse()
        m.move(15,10)
        time.sleep(1)
        m.click(15,10)
        m.screen_size()
        m.position()
        Lp2 = speech.listenfor(["Click on skype"], Mouse)


def Music_cat(phrase, listener):
  print(phrase)
  if phrase == "My Chemical Romance":
    engine.speak("Which song do you require by them")
  elif phrase == "Green Day":
    engine.speak("Which song do you want by them")
  elif phrase == "Exit":
    engine.speak("going back")
    return
  else:
    return

def Internet_sites(phrase, listener):
  print(phrase)
  if phrase == "Youtube":
    engine.speak("Ok getting on youtube")
    handle.open_new_tab("https:www.youtube.com")
  elif phrase == "Facebook":
    engine.speak("Facebook it is")
    handle.open_new_tab("https:www.facebook.com")
  elif phrase == "Google":
    engine.speak("Opening google")
    handle.open_new_tab("https:www.google.com")
  elif phrase == "yahoo":
    engine.speak("opening yahoo.com")
    handle.open_new_tab("https:www.yahoo.com")
  elif phrase == "Exit":
    return
  else:
    return
  

c = ""

def phrases_music(phrase, listener):
    global c
    print(phrase)
    if phrase == "Famous last words":
        engine.speak("Opening Famous last words")
        c = ("C:\\Users\\Andy\\Music\\My Chemical Romance\\The Black Parade\\13 Famous Last Words.wma")
    elif phrase == "Hidden track":
        engine.speak("opening The hidden track")
        c = ("C:\\Users\\Andy\\Music\\My Chemical Romance\\The Black Parade\\14 (Hidden Track).wma")
    elif phrase == "Disenchanted":
        engine.speak("opening the Disencharted")
        c = ("C:\\Users\\Andy\\Music\\My Chemical Romance\\The Black Parade\\12 Disenchanted.wma")
    elif phrase == "Teenagers":
        engine.speak("Opening the music file teenagers")
        c = ("C:\\Users\\Andy\\Music\\My Chemical Romance\\The Black Parade\\11 Teenagers.wma")
    elif phrase == "Sleep":
        engine.speak("Sleep is one of my favorites too")
        c = ("C:\\Users\\Andy\\Music\\My Chemical Romance\\The Black Parade\\10 Sleep.wma")
    elif phrase == "Mama":
        engine.speak("I love this one too")
        c = ("C:\\Users\\Andy\\Music\\My Chemical Romance\\The Black Parade\\09 Mama (Guest Vocal).wma")
    elif phrase == "Cancer":
        engine.speak("Cancer is always bad")
        c = ("C:\\Users\\Andy\\Music\\My Chemical Romance\\The Black Parade\\08 Cancer.wma")
    elif phrase == "House of wolves":
        engine.speak("Haha this is awesome")
        c = ("C:\\Users\\Andy\\Music\\My Chemical Romance\\The Black Parade\\07 House Of Wolves.wma")
    elif phrase == "I don't love you":
        engine.speak("I don't love you either")
        c = ("C:\\Users\\Andy\\Music\\My Chemical Romance\\The Black Parade\\06 I Don't Love You.wma")
    elif phrase == "Welcome to the black parade":
        engine.speak("Thank you and enjoy")
        c =("C:\\Users\\Andy\\Music\\My Chemical Romance\\The Black Parade\\05 Welcome To The Black Parade.wma")
    elif phrase == "The sharpest lives":
        engine.speak("Yes they are sharp")
        c = ("C:\\Users\\Andy\\Music\\My Chemical Romance\\The Black Parade\\04 The Sharpest Lives.wma")
    elif phrase == "this is how i disappear":
        engine.speak("Ok i'll disappear")
        c = ("C:\\Users\\Andy\\Music\\My Chemical Romance\\The Black Parade\\03 This Is How I Disappear.wma")
    elif phrase == "Dead":
        engine.speak("ok i'll play this")
        c = ("C:\\Users\\Andy\\Music\\My Chemical Romance\\The Black Parade\\02 Dead!.wma")
    elif phrase == "The end":
        engine.speak("Yes this is the end")
        c = ("C:\\Users\\Andy\\Music\\My Chemical Romance\\The Black Parade\\01 The End.wma")
    elif phrase == "Print the song list":
        engine.speak("printing the song list")
        print(songs)
        
 
    elif phrase == "Exit":
        os.system("taskkill.exe /F /IM wmplayer.exe /T")
        return
    else:
        return
      
    os.startfile(c)

a = ""

b = ""
d = ""
@pausable
def Internet_phrases(phrase, listener):
    global d
    global a
    global b
    print phrase
    if phrase == "Internet Explorer":
        engine.speak("Bringing up the slowest of the slow")
        a = "C:\\Program Files\\Internet Explorer\\iexplore.exe"
        b = "iexplore.exe"
    elif phrase == "Firefox":
        engine.speak("Bringing up firefox sir")
        a = "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"
        b = "firefox.exe"
    elif phrase == "Google Chrome":
        engine.speak("Bringing up google chrome")
        a = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"
        b = "chrome.exe"
    elif phrase == "Close internet":
        engine.speak("Closing this program")
        print("taskkill.exe /F /IM \"" + b + "\" /T")
        os.system("taskkill.exe /F /IM \"" + b + "\" /T")
        return
    elif phrase == "Google Search":
        engine.speak("What do you want to search")
        LGoogle = speech.listenforanything(Google_Search)
        handle.open_new_tab('http://www.google.com/search?btnG=1&q=%s' % LGoogle)
        return
        
    elif phrase == "exit":
        return
    else:
        return
    os.startfile(a)
@pausable
def stand_by(phrase, listener):
    engine.speak("Goodbye")
    speech.standing_by = True

def wake_up(phrase, listener):
  engine.speak("Waking up")
  speech.standing_by = False
def typ(phrase, listener):
    if phrase == "Type":
        LListen = speech.listenforanything(search_ing)
def anl(phrase, listener):
    print(phrase)
    y = int(phrase)
    m = PyMouse()
    m.position()
    pos = m.position()
    m.move(pos[0] + y, pos[1])
def anp(phrase, listener):
    print(phrase)
    y = int(phrase)
    m = PyMouse()
    m.position()
    pos = m.position()
    m.move(pos[0], pos[1] + y)
def anu(phrase, listener):
    print(phrase)
    y = int(phrase)
    m = PyMouse()
    m.position()
    pos = m.position()
    m.move(pos[0], pos[1] - y)
def ane(phrase, listener):
    print(phrase)
    y = int(phrase)
    m = PyMouse()
    m.position()
    pos = m.position()
    m.move(pos[0] - y, pos[1])
def ant(phrase, listener):
    if phrase == "Mouse left":
        print("How far do you want to go")
        L2 = speech.listenforanything(ane)
    elif phrase == "Mouse Up":       
        print("How far do you want to go")
        L3 = speech.listenforanything(anu)
    elif phrase == "Mouse Right":
        print("How far do you want to go")
        L2 = speech.listenforanything(anl)
    elif phrase == "Mouse down":
        print("How far do you want to go")
        L2 = speech.listenforanything(anp)
    elif phrase == "Right Click":
        m = PyMouse()
        m.position()
        pos = m.position()
        m.click(pos[0], pos[1], 3)
    elif phrase == "Click":
        m = PyMouse()
        m.position()
        pos = m.position()
        m.click(pos[0], pos[1])
    elif phrase == "Double Click":
        m = PyMouse()
        m.position()
        pos = m.position()
        m.click(pos[0], pos[1])
        m.click(pos[0], pos[1])


@pausable
def Phrases(phrase, listener):
    print phrase
    if phrase == "Open music":
        engine.speak("What music do you want")
        L6 = speech.listenfor(["My Chemical Romance", "Exit", "Green Day"], Music_cat)
        L2 = speech.listenfor(["Print the song list", "Famous last words", "The end", "Dead", "this is how i disappear",
                               "The Sharpest lives", "Welcome to the black parade","Sleep", "Teenagers",
                               "Disenchanted", "Hidden track","House of wolves", "Cancer", "Mama","I don't love you", "Exit"], phrases_music)
    elif phrase == "Bring the internet up":
        engine.speak("What internet would you like")
        engine.speak("We have, internet explorer,Firefox and Google Chrome")
        engine.speak("Or you can search google")
        print((Ie),(Fire),(Chrome))
        L3 = speech.listenfor(["Internet Explorer", "Firefox", "Google Chrome", "Exit", "Close internet", "Google Search"], Internet_phrases)
    
    elif phrase == "What sites are there":
        engine.speak("We have Youtube. Facebook.Google. Yahoo.com or you can google a site")
        print("Youtube, Facebook, Google, Yahoo, or google something?")
        L7 = speech.listenfor(["Facebook", "Youtube", "Google", "Yahoo", "Exit"], Internet_sites)


L1 = speech.listenfor(["Open music", "Bring the internet up", "What sites are there"], Phrases)
L4 = speech.listenfor(["Stand by"], stand_by)
L5 = speech.listenfor(["Wake up"], wake_up)
L100 = speech.listenfor(["Click on Google Chrome",
                       "Click on Firefox"
                       "Click on Windows media player",
                       "Click on my folder",
                       "Click on start menu"], Firefox_Google)

Ls = speech.listenfor(["Mouse left", "Mouse Up", "Mouse Right", "Mouse down", "Right Click", "Click", "Double Click", "Type"], ant)


        


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1