parent
b915697424
commit
d608426b90
After Width: | Height: | Size: 739 KiB |
@ -0,0 +1,6 @@ |
||||
//! annyang
|
||||
//! version : 0.2.0
|
||||
//! author : Tal Ater @TalAter
|
||||
//! license : MIT
|
||||
//! https://www.TalAter.com/annyang/
|
||||
(function(){"use strict";var a=this,b=a.webkitSpeechRecognition||a.mozSpeechRecognition||a.msSpeechRecognition||a.oSpeechRecognition||a.SpeechRecognition;if(!b)return a.annyang=null,null;var c,d,e,f="en-US",g={start:[],error:[],end:[],result:[],resultMatch:[],resultNoMatch:[]},h=!1,i="font-weight: bold; color: #00f;",j=/\s*\((.*?)\)\s*/g,k=/(\(\?:[^)]+\))\?/g,l=/(\(\?)?:\w+/g,m=/\*\w+/g,n=/[\-{}\[\]+?.,\\\^$|#]/g,o=function(a){return a=a.replace(n,"\\$&").replace(j,"(?:$1)?").replace(l,function(a,b){return b?a:"([^\\s]+)"}).replace(m,"(.*?)").replace(k,"\\s*$1?\\s*"),new RegExp("^"+a+"$","i")},p=function(a){for(var b=0,c=a.length;c>b;b++)a[b].apply(this)};a.annyang={init:function(j){d&&d.abort&&d.abort(),d=new b,d.maxAlternatives=5,d.continuous=!0,d.lang=f,d.onstart=function(){p(g.start)},d.onerror=function(){p(g.error)},d.onend=function(){p(g.end),e&&a.annyang.start()},d.onresult=function(b){p(g.result);for(var d,e=b.results[b.resultIndex],f=0;f<e.length;f++){d=e[f].transcript.trim(),h&&a.console.log("Speech recognized: %c"+d,i);for(var j=0,k=c.length;k>j;j++){var l=c[j].command.exec(d);if(l){var m=l.slice(1);return h&&(a.console.log("command matched: %c"+c[j].originalPhrase,i),m.length&&a.console.log("with parameters",m)),c[j].callback.apply(this,m),p(g.resultMatch),!0}}}return p(g.resultNoMatch),!1},c=[],this.addCommands(j)},start:function(a){a=a||{},e="undefined"!=typeof a.autoRestart?!!a.autoRestart:!0,d.start()},abort:function(){e=!1,d.abort()},debug:function(a){h=arguments.length>0?!!a:!0},setLanguage:function(a){f=a,d&&d.abort&&(d.lang=a)},addCommands:function(b){var d,e;for(var f in b)if(b.hasOwnProperty(f)){if(d=a[b[f]]||b[f],"function"!=typeof d)continue;e=o(f),c.push({command:e,callback:d,originalPhrase:f})}h&&a.console.log("Commands successfully loaded: %c"+c.length,i)},addCallback:function(b,c){if(void 0!==g[b]){var d=a[c]||c;"function"==typeof d&&g[b].push(d)}}}}).call(this); |
After Width: | Height: | Size: 50 KiB |
@ -0,0 +1,62 @@ |
||||
supported_operations = ["__add__", "__sub__", "__mul__", "__floordiv__", "__mod__", "__divmod__", "__pow__", "__lshift__", "__rshift__", "__and__", "__xor__", "__or__", "__pow__", "__div__", "__truediv__", "__div__", "__radd__", "__rsub__", "__rmul__", "__rdiv__", "__rtruediv__", "__rfloordiv__", "__rmod__", "__rdivmod__", "__rpow__", "__rlshift__", "__rrshift__", "__rand__", "__rxor__", "__ror__", "__sub__", "__rpow__", "__radd__", "__neg__", "__pos__", "__abs__", "__complex__", "__long__", "__float__", "__oct__", "__hex__", "__index__", "__len__"] |
||||
class Wrapper(object): |
||||
"""Wrap a value so that it will be lazy-evaluated. `wrapper.value` returns the current value (you can also use c(wrapper)).""" |
||||
def __init__(self, target, attribute=None, arguments=None): |
||||
self.target = target |
||||
self.attribute = attribute |
||||
self.arguments = arguments |
||||
if attribute is not None and arguments is not None: |
||||
raise TypeError |
||||
@property |
||||
def value(self): |
||||
if self.attribute is None and self.arguments is None: |
||||
return self.target |
||||
if self.attribute is not None: |
||||
return getattr(c(self.target), self.attribute) |
||||
if self.arguments is not None: |
||||
return c(self.target)(*map(c, self.arguments)) |
||||
@value.setter |
||||
def value(self, new_target): |
||||
self.target = new_target |
||||
def __getattr__(self, name): |
||||
return Wrapper(self, attribute=name) |
||||
def __call__(self, *args): |
||||
return Wrapper(self, arguments=args) |
||||
def __repr__(self): return str(self) |
||||
def __str__(self): |
||||
if self.attribute is None and self.arguments is None: |
||||
return "w(" + str(self.target) + ")" |
||||
if self.attribute is not None: |
||||
return "w(" + str(self.target) + "." + str(self.attribute) + ")" |
||||
if self.arguments is not None: |
||||
return "w(" + str(self.target) + "(" + ",".join(map(str, self.arguments)) + ")" |
||||
|
||||
def pass_through(operation): |
||||
return lambda self, *others: Wrapper(Wrapper(self, attribute=operation), arguments=others) |
||||
for operation in supported_operations: |
||||
setattr(Wrapper, operation, pass_through(operation)) |
||||
|
||||
def c(g): |
||||
"""Get the current value of a wrapper or bare variable.""" |
||||
if isinstance(g, Wrapper): return g.value |
||||
else: return g |
||||
|
||||
def wrap(v): |
||||
"""Wrap a variable, or just return it if it's already wrapped.""" |
||||
if isinstance(v, Wrapper): return v |
||||
else: return Wrapper(v) |
||||
|
||||
if __name__ == "__main__": |
||||
foo, bar, baz = wrap(1), wrap(2), wrap(3) |
||||
print("foo, bar, and baz are the wrapped values 1, 2, and 3.") |
||||
boop = foo + bar*baz |
||||
print("boop is set to foo + bar*baz: %s" % (boop, )) |
||||
print("And we can fetch the value of that computation easily as boop.value: %s" % (boop.value, )) |
||||
print |
||||
print("We can change the value of foo:") |
||||
print(">>> foo.value = 5") |
||||
foo.value = 5 |
||||
print("And now, when we fetch the value of boop, it is adjusted accordingly:") |
||||
print(">>> boop.value => %s" % (boop.value,)) |
||||
print |
||||
print("There is also a shorthand for evaluating a glomp: c(boop) => %s" % (c(boop), )) |
Loading…
Reference in new issue