Skip to content

Commit

Permalink
move generate from Text Sample to WordChunk
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesdeb committed May 27, 2020
1 parent e6ba20a commit 9ed0adc
Show file tree
Hide file tree
Showing 7 changed files with 318 additions and 303 deletions.
6 changes: 5 additions & 1 deletion app/controllers/text_samples_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ def create # rubocop:disable Metrics/MethodLength
if @text_sample.save
@text_sample.analyse

format.html { redirect_to @text_sample, notice: 'Text sample was successfully created.' }
format.html do
redirect_to @text_sample,
notice: 'Text sample was successfully created and analysed.'
end
format.json { render :show, status: :created, location: @text_sample }
else
format.html { render :new }
Expand All @@ -60,6 +63,7 @@ def create # rubocop:disable Metrics/MethodLength
def update # rubocop:disable Metrics/MethodLength
respond_to do |format|
if @text_sample.update(text_sample_params)
# TODO: reanalyse the text
format.html { redirect_to @text_sample, notice: 'Text sample was successfully updated.' }
format.json { render :show, status: :ok, location: @text_sample }
else
Expand Down
61 changes: 16 additions & 45 deletions app/models/text_sample.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,62 +6,33 @@ class TextSample < ApplicationRecord
validates :description, presence: true
validates :text, presence: true

STRATEGIES = %i[word_chunk word].freeze
STRATEGIES = %i[word_chunk sentence_chunk].freeze

def analyse
STRATEGIES.each do |strategy|
case strategy
when :word_chunk
WordChunk.analyse self
# when :word

# when :sentence_chunk
# SentenceChunk.analyse self
end
end
end

def generate(params = {})
unless chunks_built?
return { message: 'Word chunks have not been built for this text sample' }
end

chunk_size, output_size = get_generate_params(params)

output = []

if chunk_size == 'all'
WordChunk::CHUNK_SIZE_RANGE.each do |current_chunk_size|
output.push(generate_text(current_chunk_size, output_size))
end
# generate text for the current text sample using the given strategy
def generate(params = { strategy: :word_chunk })
# add the current text sample to params
params.merge!({ text_sample_id: id })

# case params['strategy'].parameterize.underscore.to_sym
# case params.except!(:strategy)
case params.extract!(:strategy)
when :word_chunk
{ strategy: :word_chunk }.merge(WordChunk.generate(params))
when :sentence_chunk
{ strategy: :sentence_chunk }.merge(SentenceChunk.generate(params))
else
output.push(generate_text(chunk_size, output_size))
{ strategy: :word_chunk }.merge(WordChunk.generate(params))
end
{ output: output }
end

def get_generate_params(params = {})
chunk_size =
params[:chunk_size].to_i.zero? ? Setting.chunk_size : params[:chunk_size].to_i

output_size =
params[:output_size].to_i.zero? ? Setting.output_size : params[:output_size].to_i

[chunk_size, output_size]
end

def generate_text(chunk_size, output_size)
word_chunk = WordChunk.choose_starting_word_chunk(self, chunk_size)

output = word_chunk.text
while output.size < output_size
word_chunk = word_chunk.choose_next_word_chunk
next_character = word_chunk.text[-1]
output += next_character
end

{ text: output, chunk_size: chunk_size }
end

def chunks_built?
!WordChunk.find_by(text_sample_id: id).nil?
end
end
58 changes: 55 additions & 3 deletions app/models/word_chunk.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class WordChunk < ApplicationRecord
class WordChunk < ApplicationRecord # rubocop:disable Metrics/ClassLength
belongs_to :text_sample

validates :text, presence: true
Expand Down Expand Up @@ -80,9 +80,61 @@ def self.save_word_chunks_by_create(chunks_hash, text_sample, chunk_size)
end
end

def self.choose_starting_word_chunk(text_sample, chunk_size)
# Entry point for generating text using the word chunk strategy
def self.generate(params = {}) # rubocop:disable Metrics/MethodLength
unless chunks_built_for? params[:text_sample_id]
return { message: 'Word chunks have not been built for this text sample' }
end

chunk_size, output_size, text_sample_id = extract_generate_params(params)

output = []

if chunk_size == 'all'
CHUNK_SIZE_RANGE.each do |current_chunk_size|
output.push(generate_text(current_chunk_size, output_size, text_sample_id))
end
else
output.push(generate_text(chunk_size, output_size, text_sample_id))
end

{ output: output }
end

def self.extract_generate_params(params = {})
chunk_size = if params[:chunk_size]
.to_i.zero?
Setting.chunk_size
else params[:chunk_size].to_i
end

output_size = if params[:output_size]
.to_i.zero?
Setting.output_size else params[:output_size].to_i end

[chunk_size, output_size, params[:text_sample_id]]
end

def self.chunks_built_for?(text_sample_id)
!WordChunk.find_by(text_sample_id: text_sample_id).nil?
end

def self.generate_text(chunk_size, output_size, text_sample_id)
word_chunk = choose_starting_word_chunk(text_sample_id, chunk_size)

output = word_chunk.text
while output.size < output_size
word_chunk = word_chunk.choose_next_word_chunk
next_character = word_chunk.text[-1]
output += next_character
end

{ text: output, chunk_size: chunk_size }
end

def self.choose_starting_word_chunk(text_sample_id, chunk_size)
candidates = WordChunk
.where({ text_sample_id: text_sample.id, size: chunk_size })
.where({ text_sample_id: text_sample_id, size: chunk_size })
.limit(nil)
candidates[(rand * candidates.size).to_i]
end
Expand Down
8 changes: 1 addition & 7 deletions app/views/text_samples/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<h2>Generate</h2>
<%= form_with(model: @text_sample, url: url_for([:generate, @text_sample]), method: "get", local: true) do %>
<div class="form-group row">
<%= label_tag(:generate_strategy, "Strategy", class: "col-sm-3 col-form-label") %>
<%= label_tag(:strategy, "Strategy", class: "col-sm-3 col-form-label") %>
<div class="col-sm-9">
<%= select_tag(:generate_strategy, options_for_select(TextSample::STRATEGIES.map {| strategy| [strategy.to_s.humanize, strategy]}, request.query_parameters[:generate_strategy] || Setting.generate_strategy),class: "form-control") %>
</div>
Expand All @@ -29,12 +29,6 @@
<%= select_tag(:chunk_size, options_for_select([['All Chunk Sizes','all'],2,3,4,5,6,7,8], request.query_parameters[:chunk_size] || Setting.chunk_size),class: "form-control") %>
</div>
</div>
<div class="form-group row">
<%= label_tag(:prior_word_count, "Prior Word Count", class: "col-sm-3 col-form-label") %>
<div class="col-sm-9">
<%= select_tag(:prior_word_count, options_for_select([['All Prior Word Counts','all'],2,3,4,5,6,7,8], request.query_parameters[:prior_word_count] || Setting.prior_word_count),class: "form-control") %>
</div>
</div>
<%= submit_tag("Generate", class: "btn btn-primary") %>
<% end %>
<% if @generated_texts %>
Expand Down
Loading

0 comments on commit 9ed0adc

Please sign in to comment.