Class: Ipa
- Inherits:
-
Object
- Object
- Ipa
- Defined in:
- lib/ipa.rb
Constant Summary collapse
- ROOT =
File.("../..", __FILE__)
Instance Attribute Summary collapse
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Instance Method Summary collapse
- #conversion ⇒ Object
- #convert_to_ipa(texte) ⇒ Object
- #exceptions ⇒ Object
-
#initialize ⇒ Ipa
constructor
A new instance of Ipa.
- #parseCSV(path) ⇒ Object
Constructor Details
#initialize ⇒ Ipa
Returns a new instance of Ipa.
12 13 14 |
# File 'lib/ipa.rb', line 12 def initialize @timeout = 0.2 end |
Instance Attribute Details
#timeout ⇒ Object
Returns the value of attribute timeout.
10 11 12 |
# File 'lib/ipa.rb', line 10 def timeout @timeout end |
Instance Method Details
#conversion ⇒ Object
24 25 26 |
# File 'lib/ipa.rb', line 24 def conversion @conversion ||= parseCSV "conversion" end |
#convert_to_ipa(texte) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/ipa.rb', line 28 def convert_to_ipa(texte) texte = texte.downcase texte.gsub(SPE, "").split.map do |mot| exceptions[mot] || "".tap do |result| Timeout::timeout(timeout) do conversion.select { |regle| mot =~ /#{regle}/ }.first.tap do |regle, api| mot.sub! /#{regle}/, "" result << api.to_s end until mot.empty? end end end rescue Timeout::Error return [] end |
#exceptions ⇒ Object
20 21 22 |
# File 'lib/ipa.rb', line 20 def exceptions @exceptions ||= JSON.parse(File.read("#{ROOT}/data/dict.json")) end |
#parseCSV(path) ⇒ Object
16 17 18 |
# File 'lib/ipa.rb', line 16 def parseCSV(path) Hash[File.open("#{ROOT}/data/#{path}.csv").read.split("\n").map {|ligne| ligne.split("#")}] end |