Class: Langusta::Command
- Inherits:
-
Object
- Object
- Langusta::Command
- Defined in:
- lib/langusta/command.rb
Class Method Summary collapse
Instance Method Summary collapse
- #batch_test(profile_directory, test_files, alpha = nil) ⇒ Object
- #detect_lang(profile_directory, test_files, alpha = nil) ⇒ Object
- #detect_single_lang(filename, alpha) ⇒ Object
-
#initialize ⇒ Command
constructor
A new instance of Command.
- #initialize_factory(profile_directory) ⇒ Object
- #load_profiles(directory) ⇒ Object
Constructor Details
#initialize ⇒ Command
Returns a new instance of Command.
42 43 44 |
# File 'lib/langusta/command.rb', line 42 def initialize @detector_factory = DetectorFactory.new end |
Class Method Details
.run(argv) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/langusta/command.rb', line 3 def self.run(argv) = {} opts = OptionParser.new do |opts| opts.on("--detectlang", "Detect the language from the given text") do |d| [:operation] = :detectlang if d end opts.on("--batchtest", "Batch test of language detection") do |b| [:operation] = :batchtest if b end opts.on("-d [profile directory]") do |pd| [:profile_directory] = pd end opts.on("-a [alpha]", Float) do |alpha| [:alpha] = alpha end end.parse!(argv) arguments = [[:profile_directory]] + [argv] arguments << [:alpha] if [:alpha] case [:operation] when :detectlang self.new.send(:detect_lang, *arguments) when :batchtest self.new.send(:batch_test, *arguments) else $stderr.puts <<EOF Usage: langusta --detectlang -d [profile directory] -a [alpha] [test file(s)] langusta --batchtest -d [profile directory] -a [alpha] [test file(s)] EOF end 0 end |
Instance Method Details
#batch_test(profile_directory, test_files, alpha = nil) ⇒ Object
54 55 |
# File 'lib/langusta/command.rb', line 54 def batch_test(profile_directory, test_files, alpha=nil) end |
#detect_lang(profile_directory, test_files, alpha = nil) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/langusta/command.rb', line 46 def detect_lang(profile_directory, test_files, alpha=nil) initialize_factory(profile_directory) test_files.each do |filename| language = detect_single_lang(filename, alpha) puts "%s: %s" % [filename, language] end end |
#detect_single_lang(filename, alpha) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/langusta/command.rb', line 57 def detect_single_lang(filename, alpha) ucs2_content = Langusta.utf82cp(File.open(filename).read) detector = @detector_factory.create(alpha) detector.append(ucs2_content) language = detector.detect() end |
#initialize_factory(profile_directory) ⇒ Object
65 66 67 68 69 70 |
# File 'lib/langusta/command.rb', line 65 def initialize_factory(profile_directory) profiles = load_profiles(profile_directory) profiles.each do |profile| @detector_factory.add_profile(profile) end end |
#load_profiles(directory) ⇒ Object
72 73 74 75 76 |
# File 'lib/langusta/command.rb', line 72 def load_profiles(directory) @profiles = Dir[File.join(directory, '/*')].map do |filename| LangProfile.load_from_file(filename) end end |