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

Class Method Details

.trainObject



24
25
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
53
54
55
56
57
58
59
60
61
62
63
64
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
# File 'lib/language_detector.rb', line 24

def self.train

  # For a full list of ISO 639 language tags visit:

  # http:#www.loc.gov/standards/iso639-2/englangn.html

  #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

  training_data = [
    # af (afrikaans)
    [ "ar", "ar-utf8.txt", "utf8", "arabic" ],
    [ "bg", "bg-utf8.txt", "utf8", "bulgarian" ],
    # bs (bosnian )
    # ca (catalan)
    [ "cs", "cs-utf8.txt", "utf8", "czech" ],
    # cy (welsh)
    [ "da", "da-iso-8859-1.txt", "iso-8859-1", "danish" ],
    [ "de", "de-utf8.txt", "utf8", "german" ],
    [ "el", "el-utf8.txt", "utf8", "greek" ],
    [ "en", "en-iso-8859-1.txt", "iso-8859-1", "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" ],
    [ "fy", "fy-utf8.txt", "utf8", "frisian" ],
    [ "ga", "ga-utf8.txt", "utf8", "irish" ],
    #gd (gaelic)
    #haw (hawaiian)
    [ "he", "he-utf8.txt", "utf8", "hebrew" ],
    [ "hi", "hi-utf8.txt", "utf8", "hindi" ],
    [ "hr", "hr-utf8.txt", "utf8", "croatian" ],
    #id (indonesian)
    [ "io", "io-utf8.txt", "utf8", "ido" ],
    [ "is", "is-utf8.txt", "utf8", "icelandic" ],
    [ "it", "it-utf8.txt", "utf8", "italian" ],
    [ "ja", "ja-utf8.txt", "utf8", "japanese" ],
    [ "ko", "ko-utf8.txt", "utf8", "korean" ],
    #ku (kurdish)
    #la ?
    #lb ?
    #lt (lithuanian)
    #lv (latvian)
    [ "hu", "hu-utf8.txt", "utf8", "hungarian" ],
    #mk (macedonian)
    #ms (malay)
    #my (burmese)
    [ "nl", "nl-iso-8859-1.txt", "iso-8859-1", "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" ],
    #sr (serbian)
    [ "sv", "sv-iso-8859-1.txt", "iso-8859-1", "swedish" ],
    #[ "sv", "sv-utf8.txt", "utf8", "swedish" ],
    [ "th", "th-utf8.txt", "utf8", "thai" ],
    #tl (tagalog)
    #ty (tahitian)
    [ "uk", "uk-utf8.txt", "utf8", "ukraninan" ],
    [ "vi", "vi-utf8.txt", "utf8", "vietnamese" ],
    #wa (walloon)
    #yi (yidisih)
    [ "zh-CN", "zh-utf8.txt", "utf8", "chinese simplified" ],
    [ "zh-TW", "zh-TW-utf8.txt", "utf8", "chinese traditional"]
  ]

  profiles = []
  training_data.each {|data|
    p = LanguageDetector::Profile.new data[0]
    p.init_with_file data[1]
    profiles << p
  }
  puts 'saving model...'
  filename = File.expand_path(File.join(File.dirname(__FILE__), "model.yml"))
  File.open(filename, 'w') {|f|
    YAML.dump(profiles, f)
  }
end

Instance Method Details

#detect(text) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/language_detector.rb', line 6

def detect text
  @profiles ||= load_model

  p = LanguageDetector::Profile.new("")
  p.init_with_string text
  best_profile = nil
  best_distance = nil
  @profiles.each {|profile|
    distance = profile.compute_distance(p)

    if !best_distance || distance < best_distance
      best_distance = distance
      best_profile = profile
    end
  }
  return best_profile.name
end

#load_modelObject



112
113
114
115
# File 'lib/language_detector.rb', line 112

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