diff --git a/app/controllers/text_samples_controller.rb b/app/controllers/text_samples_controller.rb index caa5e6f..0244d53 100644 --- a/app/controllers/text_samples_controller.rb +++ b/app/controllers/text_samples_controller.rb @@ -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 diff --git a/app/models/sentence_chunk.rb b/app/models/sentence_chunk.rb index 00742cd..8a39d07 100644 --- a/app/models/sentence_chunk.rb +++ b/app/models/sentence_chunk.rb @@ -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 @@ -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 = [] diff --git a/app/models/text_sample.rb b/app/models/text_sample.rb index 24c5c44..e012cb6 100644 --- a/app/models/text_sample.rb +++ b/app/models/text_sample.rb @@ -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 diff --git a/app/models/word_chunk.rb b/app/models/word_chunk.rb index 4930e70..c0618b3 100644 --- a/app/models/word_chunk.rb +++ b/app/models/word_chunk.rb @@ -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] diff --git a/spec/models/sentence_chunk_spec.rb b/spec/models/sentence_chunk_spec.rb index dd2717f..df9b72d 100644 --- a/spec/models/sentence_chunk_spec.rb +++ b/spec/models/sentence_chunk_spec.rb @@ -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] @@ -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) @@ -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) diff --git a/spec/models/text_sample_spec.rb b/spec/models/text_sample_spec.rb index bf85c15..9e26637 100644 --- a/spec/models/text_sample_spec.rb +++ b/spec/models/text_sample_spec.rb @@ -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') diff --git a/spec/models/word_chunk_spec.rb b/spec/models/word_chunk_spec.rb index 6d9b803..a35c8c5 100644 --- a/spec/models/word_chunk_spec.rb +++ b/spec/models/word_chunk_spec.rb @@ -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 } } diff --git a/spec/requests/text_samples_spec.rb b/spec/requests/text_samples_spec.rb index f848680..2d95c8d 100644 --- a/spec/requests/text_samples_spec.rb +++ b/spec/requests/text_samples_spec.rb @@ -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 @@ -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