Ruby Spell Checker Not Working Properly -
i writing small project in ruby takes of words website , sorts them short long.
to verify gets sorted valid english comparing array of scraped words basic unix/osx words file.
the method spell_check method. problem when used on small arrays works fine, larger ones let non-words through. ideas?
def spell_check (words_array) dictionary = io.read "./words.txt" dictionary = dictionary.split dictionary.map{|x| x.strip } words_array.each |word| if !(dictionary.include? word) words_array.delete word end end return words_array end
i simplified code, maybe work?
def spell_check(words) lines = io.readlines('./words.txt').map { |line| line.strip } words.reject { |word| !lines.include? word } end i noticed trying modify words_array while simultaneously iterating on each:
words_array.each |word| if !(dictionary.include? word) words_array.delete word # delete word words_array while iterating! end end i'm not sure if case in ruby, in other programming languages java , c#, trying modify collection, while you're iterating on @ same time, invalidates iteration, , either produces unexpected behavior, or throws error. maybe problem original code?
also, return statement unnecessary in original code, because last statement evaluated in ruby block returned (unless there's explicit return precedes last statement). it's idiomatic ruby leave out in such cases.
Comments
Post a Comment