Class: LanguageDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/language_detector.rb

Defined Under Namespace

Classes: Profile

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type = 'tc') ⇒ LanguageDetector

Supports two ngram databases:

  • fm - built from scratch texts included with gem

  • tc - textcat ngram database



13
14
15
# File 'lib/language_detector.rb', line 13

def initialize(type='tc')
  @profiles = load_model(type)
end

Class Method Details

.train_fmObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/language_detector.rb', line 65

def self.train_fm
  # For a full list of ISO 639 language tags visit:
  # http://www.loc.gov/standards/iso639-2/englangn.html
  # http://www.loc.gov/standards/iso639-2/php/English_list.php

  #LARGE profiles follow:

  #NOTE: These profiles taken from the "World War II" node on wikipedia
  #with the 'lang' and ?action=raw URI which results in a UTF8 encoded
  #file.  If we need to get more profile data for a language this is
  #always a good source of data.
  #
  # http:#en.wikipedia.org/wiki/World_War_II
  # EU corpus: http://wt.jrc.it/lt/Acquis/
  # 

  training_data = [
    [ "ar", "ar-utf8.txt", "utf8", "arabic" ],
    [ "bg", "bg-utf8.txt", "utf8", "bulgarian" ],
    [ "cs", "cs-utf8.txt", "utf8", "czech" ],
    [ "da", "da-utf8.txt", "utf8", "danish" ],
    [ "de", "de-utf8.txt", "utf8", "german" ],
    [ "el", "el-utf8.txt", "utf8", "greek" ],
    [ "en", "en-utf8.txt", "utf8", "english" ],
    [ "et", "et-utf8.txt", "utf8", "estonian" ],
    [ "es", "es-utf8.txt", "utf8", "spanish" ],
    [ "fa", "fa-utf8.txt", "utf8", "farsi" ],
    [ "fi", "fi-utf8.txt", "utf8", "finnish" ],
    [ "fr", "fr-utf8.txt", "utf8", "french" ],
    [ "ga", "ga-utf8.txt", "utf8", "irish" ],
    [ "he", "he-utf8.txt", "utf8", "hebrew" ],
    [ "hi", "hi-utf8.txt", "utf8", "hindi" ],
    [ "hr", "hr-utf8.txt", "utf8", "croatian" ],
    [ "it", "it-utf8.txt", "utf8", "italian" ],
    [ "ja", "ja-utf8.txt", "utf8", "japanese" ],
    [ "ko", "ko-utf8.txt", "utf8", "korean" ],
    [ "hu", "hu-utf8.txt", "utf8", "hungarian" ],
    [ "tk", "tk-utf8.txt", "utf8", "turkish" ],
    [ "nl", "nl-utf8.txt", "utf8", "dutch" ],
    [ "no", "no-utf8.txt", "utf8", "norwegian" ],
    [ "pl", "pl-utf8.txt", "utf8", "polish" ],
    [ "pt", "pt-utf8.txt", "utf8", "portuguese" ],
    [ "ro", "ro-utf8.txt", "utf8", "romanian" ],
    [ "ru", "ru-utf8.txt", "utf8", "russian" ],
    [ "sl", "sl-utf8.txt", "utf8", "slovenian" ],
    [ "sv", "sv-utf8.txt", "utf8", "swedish" ],
    [ "th", "th-utf8.txt", "utf8", "thai" ],
    [ "uk", "uk-utf8.txt", "utf8", "ukraninan" ],
    [ "vi", "vi-utf8.txt", "utf8", "vietnamese" ],
    [ "zh", "zh-utf8.txt", "utf8", "chinese" ]
    # id (indonesian)
    # ku (kurdish)
    # lt (lithuanian)
    # lv (latvian)
    # mk (macedonian)
    # ms (malay)
    # sr (serbian)
    # my (burmese)
    # [ "fy", "fy-utf8.txt", "utf8", "frisian" ],
    # [ "io", "io-utf8.txt", "utf8", "ido" ],
    # [ "is", "is-utf8.txt", "utf8", "icelandic" ],
  ]

  profiles = []
  training_data.each do |data|
    p = LanguageDetector::Profile.new(:name => data.last, :file => data[1])
    profiles.push p
  end

  puts 'saving model...'
  filename = File.expand_path(File.join(File.dirname(__FILE__), "model-fm.yml"))
  File.open(filename, 'w') {|f| YAML.dump(profiles, f)}
end

.train_tcObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/language_detector.rb', line 34

def self.train_tc
  profiles = []
  languages = Dir.glob("textcat_ngrams/*.lm").collect {|l| l.gsub(/\.lm$/,'')}.sort

  languages.each do |language|
    ngram = {}
    rang = 1

    lang = File.open("#{language}.lm", "r")
    lang.each_line do |line|

      line = line.chomp
      if line =~ /^[^0-9\s]+/o
        ngram[line.chomp.split(/\t/).first] = rang
        rang += 1
      end

    end
    lang.close

    p = LanguageDetector::Profile.new(:name => language.split('/').last.split('-').first)
    p.ngrams = ngram

    profiles.push p
  end

  puts 'saving model...'
  filename = File.expand_path(File.join(File.dirname(__FILE__), "model-tc.yml"))
  File.open(filename, 'w') {|f| YAML.dump(profiles, f)}
end

Instance Method Details

#detect(text) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/language_detector.rb', line 17

def detect(text)
  p = LanguageDetector::Profile.new(:text => text)
  best_profile = nil
  best_distance = nil

  @profiles.each do |profile|
    distance = profile.compute_distance(p)

    if !best_distance or distance < best_distance
      best_distance = distance
      best_profile = profile
    end
  end
  p best_distance
  best_profile.name
end

#load_model(name) ⇒ Object



139
140
141
142
# File 'lib/language_detector.rb', line 139

def load_model(name)
  filename = File.expand_path(File.join(File.dirname(__FILE__), "model-#{name}.yml"))
  @profiles = YAML.load_file(filename)
end