cogibara 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cogibara.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 wstrinz
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Cogibara
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'cogibara'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install cogibara
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,68 @@
1
+ #!/usr/bin/env ruby
2
+ require 'cogibara'
3
+ require 'slop'
4
+
5
+ def text(msg)
6
+ Cogibara::message_handler.handle(msg)
7
+ end
8
+
9
+
10
+ def text_loop
11
+ loop do
12
+ text $stdin.gets
13
+ end
14
+ end
15
+
16
+ def text_or_file
17
+ if File.exist?("./#{@msg}")
18
+ Cogibara::file_handler.handle(File.open("./#{ARGV[0]}",'rb').read, "local")
19
+ else
20
+ text(@msg)
21
+ end
22
+ end
23
+
24
+ def parse
25
+
26
+ opts = Slop.parse do
27
+ on 'v', 'verbose', 'Verbose mode'
28
+ on 'm=', 'message', 'Message'
29
+ on 'n=', 'name', 'Name'
30
+ on 'c=', 'config', 'Configuration file'
31
+ end
32
+
33
+ @verbose = opts.verbose?
34
+ @msg = opts[:message]
35
+ @name = opts[:name] || "cucumber"
36
+ @config_file = opts[:config]
37
+
38
+ if !@msg
39
+ if @config_file
40
+ @verbose = false
41
+ @name = "cucumber"
42
+ configure
43
+ text_loop
44
+ else
45
+ @msg = ARGV.join(' ')
46
+ @name = "cucumber"
47
+ @verbose = false
48
+ end
49
+ end
50
+ end
51
+
52
+ def configure
53
+ if @config_file
54
+ load './' + @config_file.to_s
55
+ end
56
+ Cogibara.setup do |config|
57
+ config.name = @name unless @name.nil?
58
+ config.local = true
59
+ config.use_redis = false
60
+ config.verbose = @verbose unless @verbose.nil?
61
+ end
62
+ end
63
+
64
+ parse
65
+
66
+ configure
67
+
68
+ ARGV[0] ? text_or_file : text_loop
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'cogibara'
4
+
5
+ require 'redis'
6
+ @red = Redis.new
7
+
8
+ Cogibara.setup do |config|
9
+ config.name = "cucumber"
10
+ end
11
+
12
+ if ARGV[0]
13
+ load "./#{ARGV[0]}"
14
+ end
15
+
16
+ Redis.new.subscribe(:toCapy) do |on|
17
+ on.message do |channel, msg|
18
+ # puts msg
19
+ file = @red.hmget("#{msg}",'file')
20
+ text = @red.hmget("#{msg}",'text')
21
+ @current_client = @red.hmget("#{msg}",'client')[0].to_i
22
+ puts "message from #{@current_client}"
23
+ if file[0]
24
+ puts "Handling as file "
25
+ begin
26
+ Cogibara::FileHandler.new.handle(file[0], @current_client)
27
+ rescue Exception
28
+ puts "an error occured! " + ($!).to_s
29
+ end
30
+ elsif text[0]
31
+ puts "Handling as text"
32
+ Cogibara::message_handler.handle(Cogibara::Message.new(text[0],@current_client))
33
+ end
34
+ end
35
+ end
data/cogibara.gemspec ADDED
@@ -0,0 +1,50 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cogibara/version'
5
+ # require 'cogibara/message.rb'
6
+ # require 'cogibara/configuration.rb'
7
+ # require 'cogibara/file_handler.rb'
8
+ # require 'cogibara/text_parser.rb'
9
+ # require 'cogibara/message_handler.rb'
10
+ # require 'cogibara/dispatcher.rb'
11
+ # require 'cogibara/speaker.rb'
12
+ # require 'cogibara/responder.rb'
13
+ # require 'cogibara/transcriber.rb'
14
+ # require 'cogibara/confirmer.rb'
15
+ # require 'cogibara/operator_base.rb'
16
+
17
+ Gem::Specification.new do |spec|
18
+ spec.name = "cogibara"
19
+ spec.version = Cogibara::VERSION
20
+ spec.authors = ["wstrinz"]
21
+ spec.email = ["wstrinz@gmail.com"]
22
+ spec.description = %q{Modular, extensible personal assistant}
23
+ spec.summary = %q{Modular, extensible personal assistant}
24
+ spec.homepage = ""
25
+ spec.license = "MIT"
26
+
27
+ spec.files = `git ls-files`.split($/)
28
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
29
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_development_dependency "bundler", "~> 1.3"
33
+ spec.add_development_dependency "rake"
34
+
35
+ spec.add_dependency 'speech2text'
36
+
37
+ spec.add_dependency 'maluuba_napi2'
38
+ spec.add_dependency 'json', "~> 1.7.7"
39
+ spec.add_dependency "cleverbot2"
40
+ spec.add_dependency 'bing_translator'
41
+ spec.add_dependency 'wikipedia-client'
42
+ spec.add_dependency 'sanitize'
43
+ spec.add_dependency 'uuid'
44
+ spec.add_dependency 'redis'
45
+ spec.add_dependency 'wolfram'
46
+ spec.add_dependency 'google_calendar'
47
+ spec.add_dependency 'forecast_io'
48
+ spec.add_dependency 'geocoder'
49
+ spec.add_dependency 'slop'
50
+ end
@@ -0,0 +1,84 @@
1
+ module Cogibara
2
+ class Configuration
3
+ attr_accessor :active_mode
4
+ attr_accessor :active_file_mode
5
+
6
+ def defaults
7
+ {
8
+ name: "cucumber",
9
+ speak: false,
10
+ text: true,
11
+ hard_parse: true,
12
+ soft_parse: true,
13
+ verbose: true,
14
+ local: false,
15
+ use_redis: true,
16
+ }
17
+ end
18
+
19
+ def name=(name)
20
+ @name = name
21
+ end
22
+
23
+ def name
24
+ @name.nil? ? defaults[:name] : @name
25
+ end
26
+
27
+ def speak=(speak)
28
+ @speak=speak
29
+ end
30
+
31
+ def speak
32
+ @speak.nil? ? defaults[:speak] : @speak
33
+ end
34
+
35
+ def hard_parse=(hard_parse)
36
+ @hard_parse=hard_parse
37
+ end
38
+
39
+ def hard_parse
40
+ @hard_parse.nil? ? defaults[:hard_parse] : @hard_parse
41
+ end
42
+
43
+ def soft_parse=(soft_parse)
44
+ @soft_parse = soft_parse
45
+ end
46
+
47
+ def soft_parse
48
+ @soft_parse.nil? ? defaults[:soft_parse] : @soft_parse
49
+ end
50
+
51
+ def use_redis
52
+ @use_redis.nil? ? defaults[:use_redis] : @use_redis
53
+ end
54
+
55
+ def use_redis=(use_redis)
56
+ @use_redis = use_redis
57
+ end
58
+
59
+ def local
60
+ @local.nil? ? defaults[:local] : @local
61
+ end
62
+
63
+ def local=(local)
64
+ @local = local
65
+ end
66
+
67
+ def text
68
+ @text.nil? ? defaults[:text] : @text
69
+ end
70
+
71
+ def text=(text)
72
+ @text = text
73
+ end
74
+
75
+ def verbose
76
+ @verbose.nil? ? defaults[:verbose] : @verbose
77
+ end
78
+
79
+ def verbose=(verbose)
80
+ @verbose = verbose
81
+ end
82
+
83
+ end
84
+ end
@@ -0,0 +1,30 @@
1
+ module Cogibara
2
+ class Confirmer
3
+
4
+ def actions
5
+ @actions ||= []
6
+ end
7
+
8
+ def add_action(message,success=nil,failure=nil)
9
+ actions << [message,success,failure]
10
+ end
11
+
12
+ def confirm_action
13
+ act = actions.shift
14
+ if act[1]
15
+ act[1].call
16
+ end
17
+ end
18
+
19
+ def deny_action
20
+ act = actions.shift
21
+ if act[2]
22
+ act[2].call
23
+ end
24
+ end
25
+
26
+ def waiting?
27
+ !actions.empty?
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,6 @@
1
+ require 'yaml'
2
+ Cogibara.setup_dispatcher do |dispatcher|
3
+ # yml = YAML.load_file('/home/wstrinz/Documents/Projects/speechrecog/config.yml')
4
+ # dispatcher.config_from_yaml(yml)
5
+ # dispatcher.register_operator(["REMINDER"],{name: "Reminder Setter"})
6
+ end
@@ -0,0 +1,125 @@
1
+ module Cogibara
2
+ class Dispatcher
3
+ def operators
4
+ @operators ||= Hash.new
5
+ end
6
+
7
+ def file_operators
8
+ @file_operators ||= Hash.new
9
+ end
10
+
11
+ def operator_options
12
+ @operator_options ||= Hash.new
13
+ end
14
+
15
+ def operator(keyword)
16
+ operators[keyword] = operators[keyword].new(operator_options[operators[keyword]]) if operators[keyword].is_a? Class
17
+ operators[keyword]
18
+ end
19
+
20
+ def file_operator(keyword)
21
+ file_operators[keyword] = file_operators[keyword].new(operator_options[operators[keyword]]) if file_operators[keyword].is_a? Class
22
+ file_operators[keyword]
23
+ end
24
+
25
+ def register_operator(keywords, options)
26
+ mod_keywords = keywords
27
+ mod_name = options[:name] || keywords[0]
28
+ mod_file = options[:file_name] ? options[:file_name] : module_name.downcase.gsub(' ','_')
29
+ mod_class_name = options[:class_name] ? options[:class_name] : mod_file.split('_').map{ |w| w.capitalize }.join
30
+ mod_file += ".rb"
31
+ loc = File.expand_path(File.dirname(__FILE__))
32
+ if File.exists?("#{loc}/operators/#{mod_file}")
33
+ require "#{loc}/operators/#{mod_file}"
34
+ if Object.const_defined?(mod_class_name)
35
+ mod_class = eval(mod_class_name) ## TODO prooooooobably should do this a different way
36
+ unless (mod_class.method_defined?("process") || mod_class.method_defined?("process_file"))
37
+ puts "process method missing for #{mod_class.to_s} in #{mod_name}"
38
+ else
39
+ # puts "LOADED module #{mod_name} \n\t keywords: #{mod_keywords}" if Cogibara::config.verbose
40
+ operator_options[mod_class] = options[:config] || {}
41
+ # mod_instance = mod_class.new()
42
+ # mod_instance.operator_config = options[:config] if options[:config]
43
+ end
44
+ if mod_class.method_defined?("process")
45
+ # puts "LOADED module #{mod_name} \n\t keywords: #{mod_keywords}"
46
+ mod_keywords.each do |kw|
47
+ operators[kw] = mod_class
48
+ end
49
+ end
50
+ if mod_class.method_defined?("process_file")
51
+ # puts "LOADED module #{mod_name} \n\t keywords: #{mod_keywords}"
52
+ mod_keywords.each do |kw|
53
+ file_operators[kw] = mod_class
54
+ end
55
+ # puts file_operators
56
+ end
57
+ else
58
+ puts "main class: #{mod_class_name} not defined for module #{mod_name}"
59
+ end
60
+ else
61
+ puts "file: #{loc}/operators/#{mod_file} for module #{mod_name} does not exist, skipping load"
62
+ end
63
+ end
64
+
65
+
66
+ def call(keyword,message)
67
+ begin
68
+ if operators[keyword].is_a? Cogibara::OperatorBase
69
+ response = operator(keyword).receive_message(message)
70
+ else
71
+ response = operator(keyword).process(message)
72
+ end
73
+ rescue Exception
74
+ puts "an error occured! " + ($!).to_s
75
+ Cogibara::say(Cogibara::Message.new("** O Noez, an error has occured in module #{keyword}; #{($!).to_s} **",message.clientID))
76
+ nil
77
+ end
78
+ # if response.is_a? Hash
79
+ # if response[:code]
80
+ # if response[:code] == :confirm
81
+ # # Cogibara::Responder.new.send_reply(response[:text],message.clientID)
82
+ # Cogibara::confirmer.add_action(response[:message], response[:success])
83
+ # response[:message]
84
+ # else
85
+ # response
86
+ # end
87
+ # else
88
+ # response
89
+ # end
90
+ # else
91
+ response
92
+ # end
93
+ end
94
+
95
+ def call_file(keyword, file)
96
+ response = operator(keyword).process_file(file)
97
+ end
98
+
99
+ def registered?(keyword)
100
+ operators.has_key?(keyword)
101
+ end
102
+
103
+ def registered_file?(keyword)
104
+ puts "looking for #{keyword} in #{file_operators.keys}"
105
+ file_operators.has_key?(keyword)
106
+ end
107
+
108
+ def config_from_yaml(file)
109
+ yml = file
110
+ yml["modules"].each do |mod|
111
+ mod_name = mod["module_name"]
112
+ mod_keywords = mod["keywords"]
113
+ mod_file = mod["file_name"] ? mod["file_name"] : mod["module_name"].downcase.gsub(' ','_')
114
+ mod_class_name = mod["class_name"] ? mod["class_name"] : mod_file.split('_').map{ |w| w.capitalize }.join
115
+ # mod_file += ".rb"
116
+
117
+ if mod_keywords.is_a? Array
118
+ register_operator(mod_keywords, {name: mod_name, file_name: mod_file, class_name: mod_class_name, config: mod})
119
+ else
120
+ register_operator([mod_keywords], {name: mod_name, file_name: mod_file, class_name: mod_class_name, config: mod})
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,44 @@
1
+ module Cogibara
2
+ class FileHandler
3
+
4
+ def handle(file, clientID)
5
+ Responder.new.send_reply("@transcribe",clientID)
6
+ handle_audio(file, clientID)
7
+ end
8
+
9
+ def handle_audio(file, clientID, options={})
10
+ if Cogibara::config.active_file_mode
11
+ if Cogibara::dispatcher.registered_file?(Cogibara::config.active_file_mode)
12
+ response = Cogibara::dispatcher.call_file(Cogibara::config.active_file_mode, file)
13
+ Responder.new.send_reply(response,clientID)
14
+ else
15
+ puts "active file mode #{Cogibara::config.active_file_mode} enabled but not registered, doing the regular thing"
16
+ newmessage = Message.new(Transcriber.new.transcribe(file), clientID)
17
+ Responder.new.send_reply("you: #{newmessage.text}",clientID)
18
+ Cogibara::message_handler.handle(newmessage)
19
+ end
20
+ else
21
+ newmessage = Message.new(Transcriber.new.transcribe(file), clientID)
22
+ Responder.new.send_reply("you: #{newmessage.text}",clientID)
23
+ Cogibara::message_handler.handle(newmessage)
24
+ end
25
+ end
26
+
27
+ def handle_video(file)
28
+
29
+ end
30
+
31
+ def handle_image(file)
32
+ "images not yet implemented"
33
+ end
34
+
35
+ def handle_other(file)
36
+ "#{file} not supported"
37
+ end
38
+
39
+ def convert_to_audio(file)
40
+
41
+ end
42
+ end
43
+
44
+ end
@@ -0,0 +1,20 @@
1
+ module Cogibara
2
+ class Message
3
+ attr :text
4
+ attr_accessor :structure
5
+ attr :clientID
6
+
7
+ def initialize(text="", clientID=-1,structure=Hash.new)
8
+ @text = text
9
+ @structure = structure
10
+ @clientID = clientID
11
+ end
12
+
13
+ def become_clone!(msg)
14
+ @text = msg.text
15
+ @structure = msg.structure
16
+ @clientID = msg.clientID
17
+
18
+ end
19
+ end
20
+ end