Skip to content
Browse files

First, unsophisticated, usage of ReVerb.

  • Loading branch information...
1 parent 5bbb33c commit d0eda6a90c889f3ced192b521737c0a2c52af4cd @HotFusionMan committed Apr 4, 2012
View
10 app/models/q_and_a_machine/assertion.rb
@@ -1,15 +1,9 @@
module QAndAMachine
class Assertion < ActiveRecord::Base
- def initialize( _id = object_id )
- @id = _id
- end
-
- # after_save :enqueue_if_new
-
-
+ after_create :enqueue
def enqueue
- ::Resque.enqueue( AssertionProcessor, @id )
+ ::Resque.enqueue( AssertionProcessor, id )
end
end
end
View
20 app/models/q_and_a_machine/assertion_processor.rb
@@ -1,5 +1,25 @@
module QAndAMachine
class AssertionProcessor
@queue = :assertions
+
+ def self.perform( assertion_id )
+ assertion = Assertion.find( assertion_id )
+
+ # feed assertion's data to ReVerb, put ReVerb's output back into the assertion record
+ external_processor = IO.popen( "java -Xmx512m -jar lib/reverb.jar", 'w+' )
+ external_processor.print assertion.assertion
+ external_processor.close_write
+ external_processor_output = external_processor.gets.chomp!.split( "\t" )
+
+ assertion.arg1 = external_processor_output[2]
+ assertion.relationship = external_processor_output[3]
+ assertion.arg2 = external_processor_output[4]
+ assertion.confidence = external_processor_output[11]
+ assertion.arg1_normalized = external_processor_output[15]
+ assertion.relationship_normalized = external_processor_output[16]
+ assertion.arg2_normalized = external_processor_output[17]
+
+ assertion.save!
+ end
end
end
View
14 db/migrate/20120403200444_add_re_verb_columns_to_assertions.rb
@@ -0,0 +1,14 @@
+class AddReVerbColumnsToAssertions < ActiveRecord::Migration
+ def change
+ add_column :q_and_a_machine_assertions, :arg1, :string
+ add_column :q_and_a_machine_assertions, :relationship, :string
+ add_column :q_and_a_machine_assertions, :arg2, :string
+
+ add_column :q_and_a_machine_assertions, :arg1_normalized, :string
+ add_column :q_and_a_machine_assertions, :relationship_normalized, :string
+ add_column :q_and_a_machine_assertions, :arg2_normalized, :string
+
+ add_column :q_and_a_machine_assertions, :confidence, :float
+ add_column :q_and_a_machine_assertions, :arg2_answer_type, :integer
+ end
+end
View
1 lib/reverb.jar
View
22 spec/models/assertion_processor_spec.rb
@@ -0,0 +1,22 @@
+require 'spec_helper'
+
+
+describe QAndAMachine::AssertionProcessor do
+ it ".perform should feed assertion's data to ReVerb, put ReVerb's output back into the assertion record" do
+ assertion = QAndAMachine::Assertion.new( :assertion => 'Jesse is a friend .' )
+ assertion.save!
+ assertion_id = assertion.id
+
+ QAndAMachine::AssertionProcessor.perform( assertion_id )
+
+ processed_assertion = QAndAMachine::Assertion.find( assertion_id )
+ processed_assertion.arg1.should == 'Jesse'
+ processed_assertion.relationship.should == 'is'
+ processed_assertion.arg2.should == 'a friend'
+ processed_assertion.arg1_normalized.should == 'jesse'
+ processed_assertion.relationship_normalized.should == 'be'
+ processed_assertion.arg2_normalized.should == 'a friend'
+ processed_assertion.confidence.should > 0.0
+ processed_assertion.confidence.should < 1.0
+ end
+end
View
22 spec/models/assertion_spec.rb
@@ -9,9 +9,11 @@
it '#enqueue should queue a job' do
- assertion_id = 42
+ user_id = 42
+ assertion = QAndAMachine::Assertion.new( :user_id => user_id )
+ assertion.save!
+ assertion_id = assertion.id
- assertion = QAndAMachine::Assertion.new( :id => assertion_id )
assertion.enqueue
# redis_keys = Redis.current.keys( '*' )
@@ -20,10 +22,20 @@
# }
queue_name = 'resque:queue:' + QAndAMachine::AssertionProcessor.instance_variable_get( :@queue ).to_s
- Redis.current.llen( queue_name ).should == 1
- job = JSON.parse( Redis.current.lpop( queue_name ) ) # Use lpop so we return Redis to its initial state.
+ Redis.current.llen( queue_name ).should > 0
+
+ job = JSON.parse( Redis.current.lindex( queue_name, 0 ) )
job['args'].size.should == 1
- job['args'].first['id'].should == assertion_id
+ job['args'].first.should == assertion_id
+ end
+
+
+ it '#save[!] should enqueue a job' do
+ assertion = QAndAMachine::Assertion.new
+
+ assertion.should_receive( :enqueue )
+
+ assertion.save!
end
end

0 comments on commit d0eda6a

Please sign in to comment.
Something went wrong with that request. Please try again.