Class: Ebooks::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/moo_ebooks/model.rb

Overview

Main class for Model management. Models are required for text generation.

`mentions` key, which hold the different statuses in them.

will negatively impact text creation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeModel

Returns a new instance of Model.


34
35
36
37
38
39
40
41
42
# File 'lib/moo_ebooks/model.rb', line 34

def initialize
  @tokens = []
  @sentences = []
  @mentions = []
  @keywords = []

  # Reverse lookup tiki by token, for faster generation
  @tikis = {}
end

Instance Attribute Details

#keywordsArray<String>

The top 200 most important keywords, in descending order

Returns:

  • (Array<String>)

33
34
35
# File 'lib/moo_ebooks/model.rb', line 33

def keywords
  @keywords
end

#mentionsArray<Array<Integer>>

Sentences derived from Twitter mentions

Returns:

  • (Array<Array<Integer>>)

29
30
31
# File 'lib/moo_ebooks/model.rb', line 29

def mentions
  @mentions
end

#sentencesArray<Array<Integer>>

Sentences represented by arrays of tikis

Returns:

  • (Array<Array<Integer>>)

25
26
27
# File 'lib/moo_ebooks/model.rb', line 25

def sentences
  @sentences
end

#tokensArray<String>

An array of unique tokens. This is the main source of actual strings in the model. Manipulation of a token is done using its index in this array, which we call a “tiki”

Returns:

  • (Array<String>)

21
22
23
# File 'lib/moo_ebooks/model.rb', line 21

def tokens
  @tokens
end

Class Method Details

.from_hash(data) ⇒ Ebooks::Model

Load a saved model

Parameters:

  • data (Hash)

Returns:


47
48
49
50
51
52
53
54
# File 'lib/moo_ebooks/model.rb', line 47

def self.from_hash(data)
  model = Model.new
  model.tokens = data[:tokens]
  model.sentences = data[:sentences]
  model.mentions = data[:mentions]
  model.keywords = data[:keywords]
  model
end

.from_json(data) ⇒ Object

Load a saved model

Parameters:

  • data (String)

59
60
61
# File 'lib/moo_ebooks/model.rb', line 59

def self.from_json(data)
  from_hash(JSON.parse(data, symbolize_names: true))
end

Instance Method Details

#consume(content) ⇒ Object

Consume a corpus into this model

Parameters:

  • content (Hash)

76
77
78
79
80
# File 'lib/moo_ebooks/model.rb', line 76

def consume(content)
  model = Ebooks::Model.new
  model.consume!(content)
  model
end

#consume!(content) ⇒ Object

Consume a corpus into this model

Parameters:

  • content (Hash)

84
85
86
87
88
89
90
91
92
# File 'lib/moo_ebooks/model.rb', line 84

def consume!(content)
  unless content.key?(:statuses) || content.key?(:mentions)
    raise ArgumentError, 'Malformed hash object. At least :statuses and/or'\
                         ' :mentions must be present as a key'
  end
  consume_statuses(content[:statuses]) unless content[:statuses].nil?
  consume_mentions(content[:mentions]) unless content[:mentions].nil?
  nil
end

#reply(input, limit = 140, sentences = @mentions) ⇒ String

Generates a response by looking for related sentences in the corpus and building a smaller generator from these

Parameters:

  • input (String)
  • limit (Integer) (defaults to: 140)

    characters available for response

  • sentences (Array<Array<Integer>>) (defaults to: @mentions)

Returns:

  • (String)

113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/moo_ebooks/model.rb', line 113

def reply(input, limit = 140, sentences = @mentions)
  # Prefer mentions
  relevant, slightly_relevant = find_relevant(sentences, input)

  if relevant.length >= 3
    generator = SuffixGenerator.build(relevant)
    update(limit, generator)
  elsif slightly_relevant.length >= 5
    generator = SuffixGenerator.build(slightly_relevant)
    update(limit, generator)
  else
    update(limit)
  end
end

#to_hashObject

Turn this model into its Hash representation


69
70
71
72
# File 'lib/moo_ebooks/model.rb', line 69

def to_hash
  { tokens: @tokens, sentences: @sentences, mentions: @mentions,
    keywords: @keywords }
end

#to_jsonObject

Turn this model into its JSON representation.


64
65
66
# File 'lib/moo_ebooks/model.rb', line 64

def to_json
  to_hash.to_json
end

#update(limit = 140, generator = nil, retry_limit = 10) ⇒ String

Generate some text

Parameters:

  • limit (Integer) (defaults to: 140)

    available characters

  • generator (SuffixGenerator, nil) (defaults to: nil)
  • retry_limit (Integer) (defaults to: 10)

    how many times to retry on invalid status

Returns:

  • (String)

99
100
101
102
103
104
105
# File 'lib/moo_ebooks/model.rb', line 99

def update(limit = 140, generator = nil, retry_limit = 10)
  tikis = gather_tikis(limit, generator, retry_limit)

  status = NLP.reconstruct(tikis, @tokens)

  fix status
end