class QuestionService

TODO: scope this class (and all other classes) into Serendipity

namespace

Takes a look at a Content and asks a question about it

Public Class Methods

answerable_fields_for(content) click to toggle source
# File lib/serendipitous/question_service.rb, line 15
def self.answerable_fields_for(content)
  # TODO: aggregate QuestionServiceLayer responses here
  ContentService.unanswered_fields(content)
end
build_question(content, field) click to toggle source
# File lib/serendipitous/question_service.rb, line 20
def self.build_question content, field
  type = field_type(field)
  template = questions_for(type).sample
    .gsub('<<field>>', field.to_s.gsub('_', ' '))
    # TODO: SanitizationService for things like ^

  TemplateService.perform_data_replacements(template, content)
end
field_type(value) click to toggle source
# File lib/serendipitous/question_service.rb, line 29
def self.field_type value
  # TODO: piggyback on Watson NLC
  case value
  when :best_friend, :mother
    'Character'
  else
    'Data'
  end
end
question(content) click to toggle source
# File lib/serendipitous/question_service.rb, line 4
def self.question(content)
  # TODO: Make "What is" a token based on content type + field
  #       e.g. location-reference --> "Where is _____?""
  field_to_answer = answerable_fields_for(content).keys.sample

  {
    field:    field_to_answer,
    question: build_question(content, field_to_answer)
  }
end
questions_for(type) click to toggle source

TODO: stick this in internationalized yaml TODO: make this smarter

# File lib/serendipitous/question_service.rb, line 41
def self.questions_for type
  case type
  when 'Character'
    [
      # e.g. field=best_friend -> "Who is Alice's best friend?"
      "Who is [[name]]'s <<field>>?"
    ]
  when 'Location'
    [
      # e.g. field=hometown -> "Where is Alice's home town?"
      "Where is [[name]]'s <<field>>?"
    ]
  when 'Item'
    [
      # e.g. field=favorite_item -> "What is Alice's favorite item?"
      "What is [[name]]'s <<field>>?"
    ]
  when 'Data'
    [
      # e.g. field=height -> "What is Alice's height?"
      # TODO: special cases here:
      #       height -> "How tall is..."
      #       weight -> "How much does...weigh?"
      #       age    -> "How old is..."
      "What is [[name]]'s <<field>>?"
    ]
  else
    [
      'Dunno lol'
    ]
  end
end