Permalink
Browse files

Got rid of redundant args in method call.

  • Loading branch information...
1 parent 65e91d5 commit 65fbfdc340cd8d9497469c49c7f8daae8f530c11 @jasiek committed Oct 15, 2011
View
@@ -64,8 +64,8 @@ def detect_single_lang(filename, alpha)
def initialize_factory(profile_directory)
profiles = load_profiles(profile_directory)
- profiles.each_with_index do |profile, index|
- @detector_factory.add_profile(profile, index, profiles.length)
+ profiles.each do |profile|
+ @detector_factory.add_profile(profile)
end
end
View
@@ -169,7 +169,8 @@ def update_lang_prob(prob, word, alpha)
# verbose
weight = alpha / BASE_FREQ
prob.length.times do |i|
- prob[i] *= weight + lang_prob_map[i]
+ # tiny workaround for nil values in word freq array
+ prob[i] *= weight + (lang_prob_map[i] || 0.0)
end
true
end
@@ -13,15 +13,15 @@ def initialize
# @param [LangProfile] language profile to be added.
# @param [Fixnum] index at which the language profile is to be added.
# @param [Fixnum] counts how many language profiles are to be added to this factory in total.
- def add_profile(profile, index, langsize)
+ def add_profile(profile)
raise DuplicateProfilesError.new(profile.name) if @lang_list.include?(profile.name)
@lang_list << profile.name
+ last_lang_index = @lang_list.size - 1
+
profile.freq.keys.each do |word|
- if not @word_lang_prob_map.has_key?(word)
- @word_lang_prob_map[word] = Array.new(langsize, 0.0)
- end
+ @word_lang_prob_map[word] ||= []
prob = 1.0 * profile.freq[word] / profile.n_words[word.length - 1]
- @word_lang_prob_map[word][index] = prob
+ @word_lang_prob_map[word][last_lang_index] = prob
end
end
@@ -3,8 +3,8 @@ class LanguageDetectionFacade
def initialize
@factory = DetectorFactory.new
profiles = load_profiles()
- profiles.each_with_index do |profile, index|
- @factory.add_profile(profile, index, profiles.length)
+ profiles.each do |profile|
+ @factory.add_profile(profile)
end
end
@@ -8,8 +8,8 @@ def test_incorrect_guesses
profiles = Dir[File.join(PROFILES_PATH, '*')].map do |filename|
LangProfile.load_from_file(filename)
end
- profiles.each_with_index do |profile, index|
- factory.add_profile(profile, index, profiles.length)
+ profiles.each do |profile|
+ factory.add_profile(profile)
end
incorrect_guesses = 0.0
View
@@ -11,19 +11,19 @@ def setup
TRAINING_EN.split(/ /).each do |w|
profile_en.add(UCS2String.new(w))
end
- @factory.add_profile(profile_en, 0, 3)
+ @factory.add_profile(profile_en)
profile_fr = LangProfile.new("fr")
TRAINING_FR.split(/ /).each do |w|
profile_fr.add(UCS2String.new(w))
end
- @factory.add_profile(profile_fr, 1, 3)
+ @factory.add_profile(profile_fr)
profile_jp = LangProfile.new("jp")
TRAINING_JP.split(/ /).each do |w|
profile_jp.add(UCS2String.new(w))
end
- @factory.add_profile(profile_jp, 2, 3)
+ @factory.add_profile(profile_jp)
end
def test_detector1
@@ -5,7 +5,7 @@ def test_add_profile
profile = LangProfile.new
factory = DetectorFactory.new
- factory.add_profile(profile, 0, 1)
+ factory.add_profile(profile)
detector = factory.create(0.123)
assert_equal(0.123, detector.alpha)
@@ -19,18 +19,18 @@ def test_exceptions
factory.create()
end
- factory.add_profile(profile, 0, 2)
+ factory.add_profile(profile)
assert_raises(DuplicateProfilesError) do
- factory.add_profile(profile, 1, 2)
+ factory.add_profile(profile)
end
end
def test_inspect
profile = LangProfile.new
factory = DetectorFactory.new
- factory.add_profile(profile, 0, 1)
+ factory.add_profile(profile)
assert_match(Regexp.new(factory.object_ptr), factory.inspect)
assert_match(/1 profile\(s\)/, factory.inspect)
View
@@ -3,11 +3,10 @@
class LangustaTest < Test::Unit::TestCase
FACTORY = DetectorFactory.new
- profiles = Dir[File.join(PROFILES_PATH, '*')].map do |filename|
- LangProfile.load_from_file(filename)
- end
- profiles.each_with_index do |profile, index|
- FACTORY.add_profile(profile, index, profiles.length)
+
+ Dir[File.join(PROFILES_PATH, '*')].each do |filename|
+ profile = LangProfile.load_from_file(filename)
+ FACTORY.add_profile(profile)
end
Dir['test/test_data/*'].each do |filename|

0 comments on commit 65fbfdc

Please sign in to comment.