Class: UncleKryon::Languages

Inherits:
BaseIsos show all
Defined in:
lib/unclekryon/iso/language.rb

Constant Summary collapse

DEFAULT_FILEPATH =
"#{DEFAULT_DIR}/languages.yaml"

Constants inherited from BaseIsos

BaseIsos::DEFAULT_DIR

Instance Attribute Summary

Attributes inherited from BaseIsos

#id, #values

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseIsos

#[], #[]=, #find, #find_by_code, #find_by_name, get_class_name, #key?, #load_file, #save_to_file, #sort_keys!, #to_s

Methods included from Logging

#init_log, #log

Constructor Details

#initializeLanguages

Returns a new instance of Languages.



114
115
116
# File 'lib/unclekryon/iso/language.rb', line 114

def initialize
  super()
end

Class Method Details

.load_file(filepath = DEFAULT_FILEPATH) ⇒ Object



174
175
176
# File 'lib/unclekryon/iso/language.rb', line 174

def self.load_file(filepath=DEFAULT_FILEPATH)
  return Languages.new.load_file(filepath)
end

.parse_and_save_to_file(parse_filepath, save_filepath = DEFAULT_FILEPATH) ⇒ Object

Parameters:

  • parse_filepath (String)

    use web browser’s developer tools to copy & paste table HTML into local file

  • save_filepath (String) (defaults to: DEFAULT_FILEPATH)

    local file to save YAML to

See Also:



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/unclekryon/iso/language.rb', line 182

def self.parse_and_save_to_file(parse_filepath,save_filepath=DEFAULT_FILEPATH)
  doc = Nokogiri::HTML(URI(parse_filepath).open,nil,'utf-8')
  tds = doc.css('td')

  langs = Languages.new
  i = 0
  tr = []

  tds.each do |td|
    c = td.content
    c.gsub!(/[[:space:]]+/,' ')
    c.strip!
    tr.push(c)

    if (i += 1) >= 5
      #puts tr.inspect()

      add_it = true
      lang = Language.new(tr)

      if langs.key?(lang.code)
        # There were so many duplicates, so added comparison check
        raise "Language already exists: #{lang.inspect}" if lang != langs[lang.code]
        add_it = false
      else
        langs.values.each_value do |v|
          puts "Duplicate lang names: #{v.name}" if v.name == lang.name
        end
      end

      langs[lang.code] = lang if add_it
      tr.clear
      i = 0
    end
  end

  langs.sort_keys!
  langs.save_to_file(save_filepath)
end

Instance Method Details

#find_by_kryon(text, add_english: false, **options) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/unclekryon/iso/language.rb', line 118

def find_by_kryon(text,add_english: false,**options)
  langs = []
  regexes = [
    %r{[[:space:]]*[/\+][[:space:]]*}, # Multiple languages are usually separated by '/'
    /[[:space:]]+/                    # Sometimes separated by space/newline
  ]

  regexes.each_with_index do |regex,i|
    try_next_regex = false

    text.split(regex).each do |t|
      # Fix misspellings and/or weird shortenings
      t = t.clone
      t.gsub!(/\AFRENC\z/i,'French')
      t.gsub!(/[\+\*]+/,'') # Means more languages, but won't worry about it (since not listed)
      t.gsub!(/\ASPAN\z/i,'Spanish')
      t.gsub!(/\AENGLSH\z/i,'English')
      t.gsub!(/\AHUNGARY\z/i,'Hungarian')

      lang = find(t)

      if lang.nil?
        if i >= (regexes.length - 1)
          msg = "No language found for: #{t}"

          if DevOpts.instance.dev?
            raise msg
          else
            log.warn(msg)
          end
        else
          log.warn("Not a language; trying next regex: #{t}")

          # Try next regex.
          langs.clear
          try_next_regex = true
          break
        end
      else
        langs.push(lang.code)
      end
    end

    # No problem with this regex, so bail out.
    break unless try_next_regex
  end

  eng_code = find_by_code('eng').code

  if add_english && !langs.include?(eng_code)
    langs.push(eng_code)
  end

  return langs.empty? ? nil : langs
end