-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcarmel.lua
More file actions
96 lines (68 loc) · 2.34 KB
/
Copy pathcarmel.lua
File metadata and controls
96 lines (68 loc) · 2.34 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
require 'util.misc'
-- WFST class using pretrained data
Carmel = {}
-- Create a Carmel
function Carmel:Create(carmelDir, wfstDir)
local carmel = carmelDir
local wfst = wfstDir
local t = {}
local cache = {}
t._et = {}
-- Executes Carmel process to retrieve all possible stress patterns for a line
function t:get_stresses(input, stress_pattern)
local cmd = ("echo '$foo' | "..carmel.."/carmel -sliOEQbk 1 "..wfst):gsub('$foo', input:upper())
local stress = os.capture(cmd)
if string.match(stress, "0 0 0 0 0 0 0 0 0 0") then return {} end
local current_patterns = {}
local pattern = ""
for i,v in pairs(split(stress, " ")) do
-- print(string.match(v, "%d+"))
if string.match(v, "%d+") then
local sub_pattern = string.sub(stress_pattern, 0, #pattern)
local levenshtein = EditDistance(sub_pattern, pattern)
if #pattern > 0 then
if sub_pattern == pattern then
current_patterns[pattern] = v
elseif levenshtein < opt.stress_strictness then
current_patterns[pattern] = v
end
end
pattern = ""
else
pattern = pattern .. string.gsub(string.gsub(v, "S%*", "/"), "S", "*")
end
end
return current_patterns
end
function t:matches_stress(line, pattern, word_stack)
if not cache[line] then
local stresses = self:get_stresses(line, long_pattern)
--if not opt.stress_strictness == -1 then
if count(stresses) == 0 then
word_stack:pop(1)
attempts = attempts + 1
if attempts > 5 then
word_stack:pop(1)
attempts=0
end
return false, 0, 0
end
--end
local most_likely_stress, stress_prob = argmax(stresses)
if #most_likely_stress > opt.syllables+1 then
return false, 0, #most_likely_stress
elseif math.abs(#most_likely_stress-opt.syllables) <= 1 then
print("New Line: "..word_stack_to_string(word_stack))
lines = lines + 1
-- word_stack:pop(word_stack:getn())
end
cache[line] = {}
cache[line][1] = stress_prob
cache[line][2] = #most_likely_stress
return true, stress_prob, #most_likely_stress
else
return true, cache[line][1], cache[line][2]
end
end
return t
end