Skip to content

Commit

Permalink
Interim checkin - work on user parser
Browse files Browse the repository at this point in the history
  • Loading branch information
CoralineAda committed Dec 14, 2014
1 parent 0a643af commit deae3d6
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ log/*
arkham.hect/*
.capistrano/metrics
alice.log
user_setup.rb
Empty file added .irbrc
Empty file.
3 changes: 2 additions & 1 deletion alice/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ def expire!
end

def fetch_content_from_sources
content = Parser::Wikipedia.fetch(topic).to_s
content = Parser::User.fetch(topic)
content += Parser::Wikipedia.fetch(topic).to_s
content += Parser::Google.fetch(topic)
end

Expand Down
36 changes: 36 additions & 0 deletions alice/parser/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module Parser
class User
def self.fetch(topic)
user = ::User.from(topic)
content = user_methods(user).map do |method|
string_from_user_info(user, method)
end.flatten.reject(&:empty?).join('. ').gsub('.. ', '. ')
end

def self.user_methods(user)
user.methods.select { |m| /^info_/.match(m) && user.method(m).arity.zero? }
end

def self.string_from_user_info(user, method_name)
info = user.public_send(method_name)
is_boolean = /\?/.match(method_name)
return(info) unless is_boolean
info ? positive_response_for(user, method_name) : negative_response_for(user, method_name)
rescue
""
end

def self.scrub_method_name(method_name)
method_name.to_s.gsub(/^info_|\?/, '').gsub('_', ' ')
end

def self.positive_response_for(user, method)
"#{user.current_nick} #{scrub_method_name(method)}".gsub(/can([^ ])/, 'can \1')
end

def self.negative_response_for(method)
"#{user.current_nick} is not #{scrub_method_name(method)}".gsub(/can[^ ]/, 'can not \1')
end

end
end
9 changes: 9 additions & 0 deletions alice/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,17 @@ def update_nick(new_nick)
return false if has_nick?(new_nick)
update_attribute(:alt_nicks, [self.alt_nicks, new_nick.downcase].flatten.uniq)
end

def info_factoids
factoids.map(&:text)
end

alias_method :description, :describe
alias_method :formatted_name, :proper_name

# Informational methods
PROPERTIES.each do |property|
alias_method property.to_s.sub(/^/, 'info_').to_sym, property
end

end
4 changes: 0 additions & 4 deletions spec/models/context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@
@context = Context.create(topic: "NickCave")
end

it "detects a user" do
expect(@context.user).to eq(@user)
end

context "derives a corpus from user data" do
it "including factoids" do
expect(@context.corpus.include?("He is Australian")).to be_truthy
Expand Down

0 comments on commit deae3d6

Please sign in to comment.