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
-
.clear_profiles ⇒ Object
Clear all the profiles.
-
.distances(text, options = {}) ⇒ Object
An array of arrays of [language, distance] arrays.
-
.guess(source, options = {}) ⇒ Object
Guess the language of a text.
-
.learn(lang, text, options = {}) ⇒ Object
Learn that a text is in a given language.
-
.load_profile(file, options = {}) ⇒ Object
Load a single profile Options are: * :merge see Babel.register_profile for details.
-
.load_profiles(options = {}) ⇒ Object
Load all the profiles from a given directory.
-
.profile(lang) ⇒ Object
find the profile for a language.
-
.register_profile(profile, options = {}) ⇒ Object
register a profile pass :merge => true to merge into an existing profile.
-
.save_profile(lang, options = {}) ⇒ Object
Save a specific profile Options are: * :dir -> the directory wo save the files to.
-
.save_profiles(options = {}) ⇒ Object
Save the profiles to a specifified directory.
Class Method Details
.clear_profiles ⇒ Object
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, = {}) source = Profile.new.learn(text, ) @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, = {}) distances = Babel.distances(source, ) 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, = {}) lang = lang.to_s profile = @profiles[lang] ||= Profile.new(lang) profile.learn(text, ) 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, = {}) Babel.register_profile(YAML.load_file(file), ) 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( = {}) dir = [:directory] || PROFILE_DIR Dir[File.join(PROFILE_DIR, '*.yml')].each do |file| Babel.load_profile(file, ) 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, = {}) if [: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, = {}) dir = [:dir] || PROFILE_DIR profile = Babel.profile(lang) profile.limit([:limit]) if [: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( = {}) @profiles.each do |lang, profile| Babel.save_profile(lang, ) end end |