Class: Ipa

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

Constant Summary collapse

ROOT =
File.expand_path("../..", __FILE__)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeIpa

Returns a new instance of Ipa.



12
13
14
# File 'lib/ipa.rb', line 12

def initialize
	@timeout = 0.2
end

Instance Attribute Details

#timeoutObject

Returns the value of attribute timeout.



10
11
12
# File 'lib/ipa.rb', line 10

def timeout
  @timeout
end

Instance Method Details

#conversionObject



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

#exceptionsObject



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