Module: Babel

Defined in:
lib/babel/babel.rb,
lib/babel/profile.rb

Defined Under Namespace

Classes: Profile

Constant Summary collapse

PROFILE_DIR =
File.join(File.dirname(__FILE__), '..', 'profiles')

Class Method Summary collapse

Class Method Details

.clear_profilesObject

Clear all the profiles



27
28
29
# File 'lib/babel/babel.rb', line 27

def self.clear_profiles
  @profiles = {}
end

.distances(text, options = {}) ⇒ Object

An array of arrays of [language, distance] arrays. The language with the shortest distance is the most probable solution. Sorted by distance, ascending (first item is most probable)



59
60
61
62
# File 'lib/babel/babel.rb', line 59

def self.distances(text, options = {})
  source = Profile.new.learn(text, options)
  @profiles.map { |lang, target| [lang, source.distance(target)] }.sort {|o1, o2| o1.last <=> o2.last}
end

.guess(source, options = {}) ⇒ Object

Guess the language of a text. As soon as there is at least one profile, this method always returns a value (perhaps the wrong) one… I.e. if only “eng” profile is registered, then this method will always retun “eng” not matter what text pass



51
52
53
54
# File 'lib/babel/babel.rb', line 51

def self.guess(source, options = {})
  distances = Babel.distances(source, options)
  distances.first.first if distances.first
end

.learn(lang, text, options = {}) ⇒ Object

Learn that a text is in a given language. Calls Profile.learn for the profile with the given language.



20
21
22
23
24
# File 'lib/babel/babel.rb', line 20

def self.learn(lang, text, options = {})
  lang = lang.to_s
  profile = @profiles[lang] ||= Profile.new(lang)
  profile.learn(text, options)
end

.load_profile(file, options = {}) ⇒ Object

Load a single profile Options are:

* :merge see Babel.register_profile for details


80
81
82
# File 'lib/babel/babel.rb', line 80

def self.load_profile(file, options = {})
  Babel.register_profile(YAML.load_file(file), options)
end

.load_profiles(options = {}) ⇒ Object

Load all the profiles from a given directory. Loads all .yml files so be careful what directory you specify. options are:

* :dir the directory, defaults to Babel::PROFILE_DIR

See Babel.load_profile() for other options



70
71
72
73
74
75
# File 'lib/babel/babel.rb', line 70

def self.load_profiles(options = {})
  dir = options[:directory] || PROFILE_DIR
  Dir[File.join(PROFILE_DIR, '*.yml')].each do |file|
    Babel.load_profile(file, options)
  end
end

.profile(lang) ⇒ Object

find the profile for a language



31
32
33
# File 'lib/babel/babel.rb', line 31

def self.profile(lang)
  @profiles[lang]
end

.register_profile(profile, options = {}) ⇒ Object

register a profile pass :merge => true to merge into an existing profile



37
38
39
40
41
42
43
# File 'lib/babel/babel.rb', line 37

def self.register_profile(profile, options = {})
  if options[:merge] && @profiles[profile.language]
    @profiles[profile.language].merge(profile)
  else
    @profiles[profile.language] = profile
  end
end

.save_profile(lang, options = {}) ⇒ Object

Save a specific profile Options are:

  • :dir -> the directory wo save the files to. Defaults to Babel::PROFILE_DIR

 * :limit -> Call limit() on the profile before save. This reduces the size of the profile

for the cost of (possibly) less accurate language guessing


97
98
99
100
101
102
103
104
# File 'lib/babel/babel.rb', line 97

def self.save_profile(lang, options = {})
  dir = options[:dir] || PROFILE_DIR
  profile = Babel.profile(lang)
  profile.limit(options[:limit]) if options[:limit]
  File.open(file_name(dir, lang), 'wb') do |file|
    file.write(profile.ya2yaml)
  end
end

.save_profiles(options = {}) ⇒ Object

Save the profiles to a specifified directory. See Babel.save_profile() for options



86
87
88
89
90
# File 'lib/babel/babel.rb', line 86

def self.save_profiles(options = {})
  @profiles.each do |lang, profile|
    Babel.save_profile(lang, options)
  end
end