Skip to content

Commit

Permalink
Comprehension/fix plagiarism nil error (#7976)
Browse files Browse the repository at this point in the history
* Refactor plagiarism highlighting to handle edge cases

Because of the way that we calculate plagiarism, the code that identifies the matched string for highlighting removes any cases of multiple spaces in a row.  We need to account for that edge case in case a student entry actually has multiple spaces.

* Add tests for Plagiarism edge case
  • Loading branch information
anathomical committed Jul 28, 2021
1 parent 1f60b73 commit cabbd69
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
Expand Up @@ -120,8 +120,17 @@ def passage_word_arrays
end

private def get_highlight(text, cleaned_text)
start_index = cleaned_text.index(matched_slice)
extra_space_indexes = []
cleaned_text.each_char.with_index { |c, i| extra_space_indexes.push(i) if c == ' ' && cleaned_text[i+1] == ' ' }

space_normalized_text = cleaned_text.split.join(' ')

start_index = space_normalized_text.index(matched_slice)
extra_space_indexes.each { |i| start_index += 1 if i <= start_index }

end_index = start_index + matched_slice.size - 1
extra_space_indexes.each { |i| end_index += 1 if i > start_index && i <= end_index }

char_positions = text.enum_for(:scan, /[A-Za-z0-9\s]/).map { |c| Regexp.last_match.begin(0) }
text[char_positions[start_index]..char_positions[end_index]]
end
Expand Down
Expand Up @@ -52,6 +52,54 @@ module Comprehension
expect(feedback[:rule_uid]).to(be_truthy)
expect(feedback[:concept_uid]).to(be_truthy)
end

context 'space normalization handling' do
let(:feedback) { "this is some standard plagiarism feedback" }

it 'should successfully highlight even when the user entry has multiple consecutive spaces but the passage does not' do
entry = "This phrase plagiarises from the passage even though it has a ton of spaces in it."
passage = "From the passage even though it has a ton of spaces."

plagiarism_check = Comprehension::PlagiarismCheck.new(entry, passage, feedback, rule)
feedback = plagiarism_check.feedback_object

expect(feedback[:highlight][0][:text]).to eq("from the passage even though it has a ton of spaces")
expect(feedback[:highlight][1][:text]).to eq("From the passage even though it has a ton of spaces")
end

it 'should successfully highlight even when the passage has multiple consecutive spaces but the user entry does not' do
entry = "This phrase plagiarises from the passage even though it has a ton of spaces in it."
passage = "From the passage even though it has a ton of spaces."

plagiarism_check = Comprehension::PlagiarismCheck.new(entry, passage, feedback, rule)
feedback = plagiarism_check.feedback_object

expect(feedback[:highlight][0][:text]).to eq("from the passage even though it has a ton of spaces")
expect(feedback[:highlight][1][:text]).to eq("From the passage even though it has a ton of spaces")
end

it 'should successfully highlight even when the user entry has isolated punctuation but the passage does not' do
entry = "This phrase plagiarises from - the -- passage even - - though it , has a ton of spaces in it."
passage = "From the passage even though it has a ton of spaces."

plagiarism_check = Comprehension::PlagiarismCheck.new(entry, passage, feedback, rule)
feedback = plagiarism_check.feedback_object

expect(feedback[:highlight][0][:text]).to eq("from - the -- passage even - - though it , has a ton of spaces")
expect(feedback[:highlight][1][:text]).to eq("From the passage even though it has a ton of spaces")
end

it 'should successfully highlight even when the passage has isolated punctuation but the user entry does not' do
entry = "This phrase plagiarises from the passage even though it has a ton of spaces in it."
passage = "From the - passage even - - - though it has -- a ton of spaces."

plagiarism_check = Comprehension::PlagiarismCheck.new(entry, passage, feedback, rule)
feedback = plagiarism_check.feedback_object

expect(feedback[:highlight][0][:text]).to eq("from the passage even though it has a ton of spaces")
expect(feedback[:highlight][1][:text]).to eq("From the - passage even - - - though it has -- a ton of spaces")
end
end
end
end
end

0 comments on commit cabbd69

Please sign in to comment.