Class: QuestionService

Inherits:
Object
  • Object
show all
Defined in:
lib/serendipitous/question_service.rb

Overview

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

namespace

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

Class Method Summary collapse

Class Method Details

.answerable_fields_for(content) ⇒ Object



15
16
17
18
# 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) ⇒ Object



20
21
22
23
24
25
26
27
# 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) ⇒ Object



29
30
31
32
33
34
35
36
37
# 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) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# 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) ⇒ Object

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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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