Commits

Anonymous committed 74400d0

Added string extension to deal with vowels and changing capital letters and also finished porting the code of the main driver. Now I need to make the interface for the command line and then port the unspeller over.

  • Participants
  • Parent commits 0851b66

Comments (0)

Files changed (5)

File RubySolution/lib/letter_tree.rb

 require "#{File.dirname(__FILE__)}/traversal_data"
 require "#{File.dirname(__FILE__)}/letter_node"
+require "#{File.dirname(__FILE__)}/string_extension"
 require "set"
 
 #Dir[File.dirname(__FILE__) + '/lib/*.rb'].each {|file| require file }
   def spellcheck(word)
     root = self.get_root()
     new_word = self.recursive_spellcheck(word, root)
+    @bad_words.clear()
     return new_word
   end
   
       
       # If a vowel was chanaged to cause the repeating character,
       # don't remove the repeated character.
-      if (current_depth + 1 < word.length && !LetterTree.is_vowel?(word[current_depth+1])) \
+      if (current_depth + 1 < word.length && word[current_depth+1].is_vowel?) \
          || !changing_vowel
         returned_word = self.process_repeated_letter(word, traversal)
         
         end
       end
       
-      if !bad_words.has_key?(word)
-        bad_words[word] = Set.new
+      if !@bad_words.has_key?(word)
+        @bad_words[word] = Set.new
       end
-      bad_words[word].add(current_depth)
+      @bad_words[word].add(current_depth)
     end
     return @@NO_SUGGESTION_TEXT
   end
       new_word = word.clone
       new_word[depth_to_check] = new_word[depth_to_check].swapcase
       
-      return self.spellcheck(new_word, traversal, changing_vowel, true)
+      return self.recursive_spellcheck(new_word, traversal, changing_vowel, true)
     end
     return @@NO_SUGGESTION_TEXT
   end
   def check_for_incorrect_vowel(word, traversal, changing_case)
     depth_to_check = traversal.depth + 1
     
-    if 0 <= depth_to_check && deptch_to_check < word.length \
-       && LetterTree.is_vowel?(word[depth_to_check])
+    if 0 <= depth_to_check && depth_to_check < word.length \
+       && word[depth_to_check].is_vowel?
       new_word = word.clone
-      
-      LetterTree.Vowels.each do |vowel|
+      # check each vowel but don't run spellcheck again on the passed in vowel
+      String.Vowels.each do |vowel|
         if vowel != word[depth_to_check].downcase
+          new_word[depth_to_check] = vowel.match_case(new_word[depth_to_check])
           
+          returned_word = self.recursive_spellcheck(new_word, traversal, true, changing_case)
+          
+          if returned_word != @@NO_SUGGESTION_TEXT
+            return returned_word
+          end
         end
       end
     end
+    return @@NO_SUGGESTION_TEXT
+  end
+  
+  def process_repeated_letter(word, traversal)
+    new_word = word.clone
+    while traversal.depth + 2 < new_word.length \
+          && new_word[traversal.depth + 1] == new_word[traversal.depth + 2]
+      new_word.slice!(traversal.depth + 1)
+      
+      if traversal.current_node != nil
+        new_traversal = traversal.clone
+        new_traversal = self.traverse_one_step(new_word, new_traversal)
+        new_traversal = self.traverse_one_step(new_word, new_traversal)
+        
+        if traversal.depth + 2 == new_traversal.depth && new_traversal.current_node.end \
+           && new_traversal.current_node.get_word() == new_word
+          return new_traversal.current_node.get_word()
+        end
+      end
+      returned_word = self.recursive_spellcheck(new_word, traversal)
+          
+      if returned_word != @@NO_SUGGESTION_TEXT
+        return returned_word
+      end
+    end 
+    return @@NO_SUGGESTION_TEXT
   end
 end

File RubySolution/lib/string_extension.rb

 class String
   @@Vowels = ['a', 'e', 'i', 'o', 'u', 'y']
   
+  def self.Vowels
+    return @@Vowels
+  end
+  
   def match_case(character_to_match)
     if character_to_match == character_to_match.upcase
       return self.upcase
-    else
-      self.downcase
+    elsif character_to_match == character_to_match.downcase
+      return self.downcase
     end
   end
+  
+  def is_vowel?(index=0)
+    char_to_check = self[index]
+    @@Vowels.each do |vowel|
+      if char_to_check.downcase == vowel
+        return true
+      end
+    end if char_to_check != nil
+    return false
+  end
 end

File RubySolution/test/letter_tree_spellcheck_tests.rb

     actual = @words.spellcheck("hi")
     assert_equal("hi", actual)
   end
+  
+  def test_spellcheck_properly_spelled_one_letter_word
+    @words.add("a")
+    @words.add("hi")
+    @words.add("noise")
+    @words.add("morenoise")
+    @words.add("even more noise")
+    @words.add("hihihihihi")
+    
+    actual = @words.spellcheck("a")
+    assert_equal("a", actual)
+  end
+  
+  def test_spellcheck_mismatched_case
+    @words.add("inside")
+    
+    actual = @words.spellcheck("inSIDE")
+    
+    assert_equal("inside", actual)
+  end
+  
+  def test_spellcheck_incorrect_vowel
+    @words.add("wake")
+    
+    actual = @words.spellcheck("weke")
+    
+    assert_equal("wake", actual)
+  end
+  
+  def test_spellcheck_repeated_letters
+    @words.add("job")
+    
+    actual = @words.spellcheck("jjoobbb")
+    
+    assert_equal("job", actual)
+  end
+  
+  def test_spellcheck_examples_on_website
+    @words.add("sheep")
+    @words.add("people")
+    @words.add("inside")
+    @words.add("job")
+    @words.add("wake")
+    @words.add("conspiracy")
+    
+    assert_equal("sheep", @words.spellcheck("sheeeeep"))
+    assert_equal("people", @words.spellcheck("peepple"))
+    assert_equal("NO SUGGESTION", @words.spellcheck("sheeple"))
+    assert_equal("inside", @words.spellcheck("inSIDE"))
+    assert_equal("job", @words.spellcheck("jjoobbb"))
+    assert_equal("conspiracy", @words.spellcheck("CUNsperrICY"))
+  end
+  
+  def test_spellcheck_words_that_need_backtracking_to_find
+    @words.add("wake")
+    @words.add("west")
+    
+    actual = @words.spellcheck("weke")
+    
+    assert_equal("wake", actual)
+  end
+  
+  def test_spellcheck_words_that_have_multiple_casings
+    # Actual example from my word list.
+    # That is what made me realize that this is not handled properly.
+    @words.add("Wake")
+    @words.add("wake")
+    
+    actual = @words.spellcheck("Wake")
+    assert_equal("Wake", actual)
+    
+    actual = @words.spellcheck("wake")
+    assert_equal("wake", actual)
+  end
+  
+  def test_spellcheck_words_that_have_repeating_characters_with_multiple_casings
+    @words.add("Azov's")
+    
+    actual = @words.spellcheck("OozZAavv''S")
+    
+    assert_equal("Azov's", actual)
+  end
+  
+  def test_spellecheck_words_that_have_repeating_vowels_next_to_other_vowels
+    @words.add("Beaumont")
+    
+    actual = @words.spellcheck("bOoaaOOMuUnTt")
+    
+    assert_equal("Beaumont", actual)
+  end
 end

File RubySolution/test/spellcheck_tests.rb

 require "test/unit"
 require "./letter_tree_population_tests"
-require "./letter_tree_spellcheck_tests"
+require "./letter_tree_spellcheck_tests"
+require "./string_extension_tests"

File RubySolution/test/string_extension_tests.rb

+require "../lib/string_extension"
+require "test/unit"
+ 
+class LetterTreeSpellcheckTests < Test::Unit::TestCase
+  def test_vowels_are_in_an_array
+    expected = ['a', 'e', 'i', 'o', 'u', 'y']
+    
+    assert_equal(expected, String.Vowels)
+  end
+  
+  def test_match_case_matches_to_upper
+    actual = "aaa".match_case("A")
+    
+    assert_equal("AAA", actual)
+  end
+  
+  def test_match_case_matches_to_upper_does_nothing_if_giving_uppecase_string
+    actual = "AAA".match_case("A")
+    
+    assert_equal("AAA", actual)
+  end
+  
+    def test_match_case_matches_to_lower
+    actual = "AAA".match_case("a")
+    
+    assert_equal("aaa", actual)
+  end
+  
+  def test_match_case_matches_to_upper_does_nothing_if_giving_uppecase_string
+    actual = "aaa".match_case("a")
+    
+    assert_equal("aaa", actual)
+  end
+  
+  def test_match_case_matches_to_upper_with_string_of_mixed_case
+    actual = "aAa".match_case("B")
+    
+    assert_equal("AAA", actual)
+  end
+  
+  def test_match_case_matches_to_lower_with_string_of_mixed_case
+    actual = "aAa".match_case("b")
+    
+    assert_equal("aaa", actual)
+  end
+  
+  def test_match_case_returns_nil_if_string_passed_in_is_mixed_case
+    actual = "aAa".match_case("aB")
+    
+    assert_equal(nil, actual)
+  end
+  
+  def test_character_is_vowel
+    actual = "a".is_vowel?
+    
+    assert_equal(true, actual)
+  end
+  
+  def test_character_is_not_vowel
+    actual = "b".is_vowel?
+    
+    assert_equal(false, actual)
+  end
+  
+  def test_second_character_is_vowel
+    actual = "ba".is_vowel?(1)
+    
+    assert_equal(true, actual)
+  end
+  
+  def test_second_character_is_not_vowel
+    actual = "ab".is_vowel?(1)
+    
+    assert_equal(false, actual)
+  end
+  
+  def test_return_false_if_index_is_out_of_range
+    actual = "a".is_vowel?(1)
+    
+    assert_equal(false, actual)
+  end
+end