-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
75 lines (62 loc) · 1.29 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
# frozen_string_literal: true
require "sinatra/base"
require "pascoale"
class Punct
def initialize(value)
@value = value
end
def to_s
@value.to_s
end
def word?
false
end
end
class Word
include Pascoale
def initialize(value)
@value = value
end
def separated
parts = SyllableSeparator.new(@value.downcase).separated
parts = tone(parts)
parts.join("-")
rescue => e
raise unless e.message.match?(/^Cannot separate/)
"#{@value.downcase} <i>(erro!)</i>"
end
def tone(parts)
reflector = Reflector.new(@value.downcase)
rslt = parts.dup
if reflector.oxytone?
rslt[-1] = "<b>#{parts[-1]}</b>"
elsif reflector.paroxytone?
rslt[-2] = "<b>#{parts[-2]}</b>"
elsif reflector.proparoxytone?
rslt[-3] = "<b>#{parts[-3]}</b>"
end
rslt
end
def to_s
return @value.to_s if @value.match?(/\d+/)
separated.to_s
end
def word?
true
end
end
class App < Sinatra::Base
get "/" do
erb :main
end
post "/pasqualize" do
@text = params[:text]
@tokens = tokenize(@text).map(&:to_s)
erb :pasqualized
end
def tokenize(text)
tokens = text.scan(/([[:blank:]]|[[:punct:]])|([[:word:]]+)/)
tokens.map { |(p, w)| w.nil? ? Punct.new(p) : Word.new(w) }
.select(&:word?)
end
end