Skip to content

Commit

Permalink
Ensure reanalyse deletes previous analyisis
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesdeb committed Apr 26, 2021
1 parent 1ff44cb commit 270ba7c
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/controllers/text_samples_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def update # rubocop:disable Metrics/MethodLength

# PATCH/PUT /text_samples/1/generate
def reanalyse
@text_sample.analyse
@text_sample.reanalyse
flash.now[:notice] = 'Text sample reanalysed successfully'

# TODO: change the form_with in the show so that it submits via AJAX. This
Expand Down
8 changes: 7 additions & 1 deletion app/models/sentence_chunk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ def self.save_chunks_by_insert_all( # rubocop:disable Metrics/MethodLength
SentenceChunk.insert_all! import_array
end

def self.reanalyse(text_sample)
# clear out anything from previous analysis
SentenceChunk.where('text_sample_id = ?', text_sample.id).delete_all
SentenceChunk.analyse(text_sample)
end

# Entry point for generating text using the sentence chunk strategy
#
# @param [Hash] params parameters to generate with
Expand Down Expand Up @@ -171,7 +177,7 @@ def self.choose_starting_chunk(text_sample_id, chunk_size)
candidates[(rand * candidates.size).to_i]
end

# Choose the next word chunk after this one
# Choose the next chunk after this one
def choose_next_chunk
token_ids_where = []

Expand Down
11 changes: 11 additions & 0 deletions app/models/text_sample.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ def analyse
end
end

def reanalyse
STRATEGIES.each do |strategy|
case strategy
when :word_chunk
WordChunk.reanalyse self
when :sentence_chunk
SentenceChunk.reanalyse self
end
end
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
Expand Down
6 changes: 6 additions & 0 deletions app/models/word_chunk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ def self.save_word_chunks_by_create(chunks_hash, text_sample, chunk_size)
end
end

def self.reanalyse(text_sample)
# clear out anything from previous analysis
WordChunk.where('text_sample_id = ?', text_sample.id).delete_all
WordChunk.analyse(text_sample)
end

# 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]
Expand Down
37 changes: 34 additions & 3 deletions spec/models/sentence_chunk_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,37 @@
end
end

describe '::reanalyse' do
let(:text_sample) { TextSample.create!(description: 'Stuff', text: 'man on the moon') }
let(:relation) { double('relation') }

before(:each) do
text_sample.analyse

expect(WordChunk.count).to be > 0
expect(SentenceChunk.count).to be > 0

allow(SentenceChunk).to receive(:where).and_return(relation)
allow(relation).to receive(:delete_all)
allow(SentenceChunk).to receive(:analyse)
end

it 'deletes all Sentence Chunks' do
SentenceChunk.reanalyse(text_sample)

expect(SentenceChunk)
.to have_received(:where)
.with('text_sample_id = ?', text_sample.id)
expect(relation).to have_received(:delete_all)
end

it 'calls analyse' do
SentenceChunk.reanalyse(text_sample)

expect(SentenceChunk).to have_received(:analyse).with(text_sample)
end
end

describe '::count_chunks' do
let(:text_sample) { TextSample.create!(description: 'Stuff', text: 'at ') }
let(:token_ids) { 'array of token ids' } # for example [1,2]
Expand Down Expand Up @@ -555,7 +586,7 @@
sentence_chunk_size: 2 }))
end

it 'chooses word chunk from candidates' do
it 'chooses sentence chunk from candidates' do
expect(SentenceChunk)
.to(
have_received(:choose_chunk_from_candidates).with(candidates)
Expand All @@ -572,14 +603,14 @@
.to receive(:build_counts_array).and_return(counts_array)
end

it 'calculates probabilities of each word chunk' do
it 'calculates probabilities of each sentence chunk' do
SentenceChunk.choose_chunk_from_candidates(candidates)

expect(SentenceChunk)
.to(have_received(:build_counts_array).with(candidates))
end

it 'selects a word chunk' do
it 'selects a sentence chunk' do
new_chunk = SentenceChunk.choose_chunk_from_candidates(candidates)

expect(new_chunk).to be_instance_of(SentenceChunk)
Expand Down
20 changes: 20 additions & 0 deletions spec/models/text_sample_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,26 @@
end
end

describe '#reanalyse' do
let(:text_sample) { create(:text_sample) }

it 'reanalyses SentenceChunk' do
allow(SentenceChunk).to receive(:reanalyse)

text_sample.reanalyse

expect(SentenceChunk).to have_received(:reanalyse).with(text_sample)
end

it 'reanalyses Word Chunks' do
allow(WordChunk).to receive(:reanalyse)

text_sample.reanalyse

expect(WordChunk).to have_received(:reanalyse).with(text_sample)
end
end

describe '#generate' do # rubocop:disable Metrics/BlockLength
let(:text_sample) do
TextSample.create!(description: 'Stuff', text: 'another man')
Expand Down
31 changes: 31 additions & 0 deletions spec/models/word_chunk_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,37 @@
end
end

describe '::reanalyse' do
let(:text_sample) { TextSample.create!(description: 'Stuff', text: 'man on') }
let(:relation) { double('relation') }

before(:each) do
text_sample.analyse

expect(WordChunk.count).to be > 0
expect(SentenceChunk.count).to be > 0

allow(WordChunk).to receive(:where).and_return(relation)
allow(relation).to receive(:delete_all)
allow(WordChunk).to receive(:analyse)
end

it 'deletes all Word Chunks' do
WordChunk.reanalyse(text_sample)

expect(WordChunk)
.to have_received(:where)
.with('text_sample_id = ?', text_sample.id)
expect(relation).to have_received(:delete_all)
end

it 'calls analyse' do
WordChunk.reanalyse(text_sample)

expect(WordChunk).to have_received(:analyse).with(text_sample)
end
end

describe '::count_chunks_of_size' do
let(:text_sample) { TextSample.create!(description: 'Stuff', text: 'at') }
let(:chunks_hash) { { 'at' => 1 } }
Expand Down
4 changes: 2 additions & 2 deletions spec/requests/text_samples_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@
allow(text_sample)
.to receive(:generate).and_return(generation_result)
get generate_text_sample_url(text_sample), params: {
chunk_size: chunk_size, output_size: output_size
chunk_size: chunk_size, output_size: output_size, strategy: :word_chunk
}
end

Expand All @@ -206,7 +206,7 @@
end

it 'sets a flash message' do
expect(flash[:notice]).to eq(generation_result[:message])
expect(flash[:warning]).to eq(generation_result[:message])
end
end

Expand Down

0 comments on commit 270ba7c

Please sign in to comment.