Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor refactor #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions lib/turkish_stemmer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,16 @@ def stem(original_word)
# Preprocess
return original_word if !proceed_to_stem?(original_word)

word = original_word.dup

# Process
stems = []
stems << nominal_verbs_suffix_machine(word)
stems << original_word
stems.flatten!.uniq!
stems << stems.map { |word| noun_suffix_machine(word) }
stems << original_word
stems.flatten!.uniq!
stems << stems.map { |word| derivational_suffix_machine(word) }

# Postprocess
# set of stem candidates
stems = [original_word, *nominal_verbs_suffix_machine(original_word.dup)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I agree that this code can be simplified, I'm not sure I agree with this approach:

  • I like the step by step approach of the original. I think it enhances readability.
  • I agree that calls to flatten! can be replaced, though I prefer the + array operator to splatting.
  • Calling uniq! before mapping the array to the noun_suffix_machine method makes sense.
  • I think calls to &method is a bit of a code smell (and I think hinder performance a bit as well, see here).

As a final thought I don't like the need for using dup on the original before calling methods that are not explicitly mutating. Ideally I think these methods should call dup themselves on their arguments. Otherwise I think they should have a bang ! operator in their names.

noun_suffix_stems = stems.map(&method(:noun_suffix_machine)).flatten
stems.push(*noun_suffix_stems)
derivational_suffix_stems = stems.map(&method(:derivational_suffix_machine))
stems.push(*derivational_suffix_stems)
stems.uniq!

# Postprocess: filter and choose among the stem candidates
stem_post_process(stems, original_word)
end

Expand Down