Class: FreelingClient::LanguageDetector
- Defined in:
- lib/freeling_client/language_detector.rb
Instance Attribute Summary
Attributes inherited from Base
#config, #ident, #port, #server
Instance Method Summary collapse
-
#detect(text) ⇒ Object
Detects language.
-
#initialize(opt = {}) ⇒ LanguageDetector
constructor
A new instance of LanguageDetector.
Constructor Details
#initialize(opt = {}) ⇒ LanguageDetector
Returns a new instance of LanguageDetector.
9 10 11 12 13 |
# File 'lib/freeling_client/language_detector.rb', line 9 def initialize(opt = {}) @config = opt.fetch(:config, 'config/freeling/analyzer.cfg') @ident = opt.fetch(:ident, '/usr/local/share/freeling/common/lang_ident/ident.dat') @timeout = opt.fetch(:timeout, 120) end |
Instance Method Details
#detect(text) ⇒ Object
Detects language
Example:
>> detector = FreelingClient::LanguageDetector.new
>> detector.detect("Este texto está en español.")
=> "es"
Arguments:
text: (String)
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/freeling_client/language_detector.rb', line 26 def detect(text) output = [] file = Tempfile.new('foo', encoding: 'utf-8') begin file.write(text) file.close stdin, stdout, stderr = Open3.popen3(command(file.path)) Timeout::timeout(@timeout) { until (line = stdout.gets).nil? output << line.chomp end = stderr.readlines unless .empty? raise ExtractionError, .join("\n") end } rescue Timeout::Error raise ExtractionError, "Timeout" ensure file.close file.unlink end output[0].to_sym end |