Commits

Cha...@Animus.hsd1.in.comcast.net.  committed 824ae7d

Added a shared script for the file manipulation code used in both unspell and spellcheck. Finished working on the unspell code and cleaned it up.

  • Participants
  • Parent commits 74400d0

Comments (0)

Files changed (13)

File RubySolution/lib/letter_node.rb

     
     return word
   end
-end
+end

File RubySolution/lib/letter_tree.rb

 require "#{File.dirname(__FILE__)}/string_extension"
 require "set"
 
-#Dir[File.dirname(__FILE__) + '/lib/*.rb'].each {|file| require file }
-
 class LetterTree
   @@NO_SUGGESTION_TEXT = "NO SUGGESTION"
   

File RubySolution/lib/shared.rb

+class Shared
+  @@POTENTIAL_PATHS = ["words", "/usr/share/dict/words"]
+  
+  def self.populate_words(words, path)
+    if path != nil
+      return Shared.populate_words_with_specific_path(words, path)
+    else
+      populated = false
+      @@POTENTIAL_PATHS.each do |potential_path|
+        populated = Shared.populate_words_with_specific_path(words, potential_path)
+        if populated == true
+          break
+        end
+      end
+      return populated
+    end
+  end
+
+  def self.populate_words_with_specific_path(words, path)
+    begin
+      file = File.open(path)
+      file.each_line do |line|
+        if line.strip!.length > 0
+          if words.respond_to?(:add)
+            words.add(line)
+          elsif words.respond_to?(:push)
+            words.push(line)
+          end
+        end
+      end
+    #rescue
+    #  return false
+    ensure
+      file.close unless file == nil
+    end
+    return true
+  end
+end

File RubySolution/lib/string_extension.rb

     end if char_to_check != nil
     return false
   end
-end
+end

File RubySolution/lib/traversal_data.rb

-
 class TraversalData
   attr_accessor :depth, :current_node, :nodes
-end
+end

File RubySolution/lib/unspell.rb

+require "#{File.dirname(__FILE__)}/string_extension"
+
+class Unspell
+  @@MAX_TIMES_TO_REPEAT_CHAR = 2
+  
+  def self.change_vowels(word_ref, test_mode=false)
+    word = word_ref.clone
+    vowel_to_use = -1
+    changing_vowel = false
+    for i in 0..word.length - 1
+      if word[i].is_vowel?
+        if test_mode
+          vowel_to_use += 1
+          vowel_to_use %= String.Vowels.length
+          changing_vowel = true
+        else
+          changing_vowel = rand(2) == 0
+          vowel_to_use = rand(String.Vowels.length)
+        end
+        
+        # change the current vowel to the new vowel and match the previous case
+        if changing_vowel
+          word[i] = String.Vowels[vowel_to_use].match_case(word[i])
+        end
+      end
+    end
+    return word
+  end
+  
+  def self.repeat_characters(word_ref, test_mode=false)
+    word = word_ref.clone
+    times_to_repeat = word.length
+    
+    (word.length - 1).downto(0) do |i|
+      if test_mode
+        times_to_repeat -= 1
+      else
+        times_to_repeat = rand(@@MAX_TIMES_TO_REPEAT_CHAR)
+      end
+      
+      char_to_repeat = word[i]
+      
+      for j in 1..times_to_repeat
+        word.insert(i, char_to_repeat)
+      end
+    end
+    return word
+  end
+  
+  def self.change_capitalization(word_ref, test_mode=false)
+    word = word_ref.clone
+    changing_case = false
+    for i in 0..word.length - 1
+      if test_mode
+        changing_case = true
+      else
+        changing_case = rand(2) == 0
+      end
+      
+      if changing_case
+        word[i] = word[i].swapcase
+      end
+    end
+    return word
+  end
+  
+  def self.unspell_word(word_ref, test_mode=false)
+    word = word_ref.clone
+    word = Unspell.change_vowels(word, test_mode)
+    word = Unspell.repeat_characters(word, test_mode)
+    word = Unspell.change_capitalization(word, test_mode)
+    return word
+  end
+end

File RubySolution/spellcheck.rb

 #!/usr/bin/env ruby
+require "./lib/letter_tree"
+require "./lib/shared"
 
-class MegaGreeter
-  attr_accessor :names
+def print_usage()
+  puts("Welcome to Charlie's Amazing Spellchecker!!")
+  puts("")
+  puts("Usage:")
+  puts("")
+  puts("spellcheck <optional file name>")
+  puts("If you want to call it without a filename, put the dictionary")
+  puts("file in this directory (#{File.absolute_path(File.dirname(__FILE__))}) and name it words")
+  puts("or make sure a file named words exists in \"/usr/share/dict/\"")
+end
 
-  # Create the object
-  def initialize(names = "World")
-    @names = names
-  end
-
-  # Say hi to everybody
-  def say_hi
-    if @names.nil?
-      puts "..."
-    elsif @names.respond_to?("each")
-
-      # @names is a list of some kind, iterate!
-      @names.each do |name|
-        puts "Hello #{name}!"
-      end
+if __FILE__ == $0 
+  words = LetterTree.new
+  
+  path = nil
+  if ARGV.length != 0
+    arg = ARGV[0].strip
+    if arg == "help" || arg == "--help" || arg == "/?" || arg == "?"
+      print_usage()
+      exit
     else
-      puts "Hello #{@names}!"
+      path = arg
     end
   end
-
-  # Say bye to everybody
-  def say_bye
-    if @names.nil?
-      puts "..."
-    elsif @names.respond_to?("join")
-      # Join the list elements with commas
-      puts "Goodbye #{@names.join(", ")}.  Come back soon!"
-    else
-      puts "Goodbye #{@names}.  Come back soon!"
+  
+  if Shared.populate_words(words, path)
+    line = ""
+    while line != nil
+      print "> "
+      STDOUT.flush
+      line = $stdin.gets()
+      if line != nil
+        line.strip!
+        puts(words.spellcheck(line))
+      end
     end
-  end
-
+  else
+    print_usage()
+  end  
 end
-
-
-if __FILE__ == $0
-  mg = MegaGreeter.new
-  mg.say_hi
-  mg.say_bye
-
-  # Change name to be "Zeke"
-  mg.names = "Zeke"
-  mg.say_hi
-  mg.say_bye
-
-  # Change the name to an array of names
-  mg.names = ["Albert", "Brenda", "Charles",
-    "Dave", "Englebert"]
-  mg.say_hi
-  mg.say_bye
-
-  # Change to nil
-  mg.names = nil
-  mg.say_hi
-  mg.say_bye
-end

File RubySolution/test/letter_tree_population_tests.rb

   attr_accessor :tree, :badWord
 end
 
-
- 
 class LetterTreePopulationTests < Test::Unit::TestCase
   
   def setup
     assert_equal(false, @words.tree['h'].nodes['i'].nodes['g'].end)
     assert_equal(true, @words.tree['h'].nodes['i'].nodes['g'].nodes['h'].end)
   end
-end
+end

File RubySolution/test/letter_tree_spellcheck_tests.rb

   attr_accessor :tree, :badWord
 end
 
-
- 
 class LetterTreeSpellcheckTests < Test::Unit::TestCase
   def setup
     @words = LetterTree.new;
     
     assert_equal("Beaumont", actual)
   end
-end
+end

File RubySolution/test/spellcheck_tests.rb

 require "./letter_tree_population_tests"
 require "./letter_tree_spellcheck_tests"
 require "./string_extension_tests"
+require "./unspeller_tests"

File RubySolution/test/string_extension_tests.rb

     
     assert_equal(false, actual)
   end
-end
+end

File RubySolution/test/unspeller_tests.rb

+require "../lib/unspell"
+require "test/unit"
+
+#class LetterTree
+#  attr_accessor :tree, :badWord
+#end
+ 
+class UnspellerTests < Test::Unit::TestCase
+  def test_change_no_vowel_word
+    actual = Unspell.change_vowels("blrg")
+    assert_equal("blrg", actual)
+  end
+  
+  def test_change_all_vowels
+    actual = Unspell.change_vowels("eiouya", true)
+    
+    assert_equal("aeiouy", actual)
+  end
+  
+  def test_change_capital_vowel
+    actual = Unspell.change_vowels("E", true)
+    
+    assert_equal("A", actual)
+  end
+  
+  def test_change_vowels_in_word
+    actual = Unspell.change_vowels("sheep", true)
+    
+    assert_equal("shaep", actual)
+  end
+  
+  def test_get_repeating_chars
+    actual = Unspell.repeat_characters("job", true)
+    
+    assert_equal("joobbb", actual)
+  end
+  
+  def test_change_capitalization_on_word
+    actual = Unspell.change_capitalization("yUp", true)
+    
+    assert_equal("YuP", actual)
+  end
+  
+  def test_unspell_word
+    actual = Unspell.unspell_word("yUp", true)
+    
+    assert_equal("AeePPP", actual)
+  end
+end

File RubySolution/unspeller.rb

+#!/usr/bin/env ruby
+require "./lib/unspell"
+require "./lib/shared"
+
+def are_args_correct?()
+  if ARGV.length != 1 && ARGV.length != 2
+    return false
+  end
+  
+  begin
+    Integer(ARGV[0].strip)
+  rescue
+    return false
+  end
+  
+  return true
+end
+
+def print_usage()
+  puts("Welcome to Charlie's Amazing Unspeller!!");
+  puts("");
+  puts("Usage:");
+  puts("");
+  puts("unspell <# of times to run> <optional file name>");
+  puts("If you want to call it without a filename, put the dictionary");
+  puts("file in this directory (#{File.absolute_path(File.dirname(__FILE__))}) and name it words")
+  puts("or make sure a file named words exists in \"/usr/share/dict/\"")
+end
+
+if __FILE__ == $0 
+  
+  if !are_args_correct?
+    print_usage()
+    exit
+  end
+  
+  words = []
+  times_to_run = Integer(ARGV[0])
+  path = nil
+  if ARGV.length == 2
+    path = ARGV[1]
+  end
+  
+  if Shared.populate_words(words, path) && words.length > 0
+    while times_to_run > 0
+      word_to_unspell = Unspell.unspell_word(words[rand(words.length)])
+      puts(word_to_unspell)
+      times_to_run -= 1
+    end
+  else
+    print_usage()
+  end  
+end