-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathshiro-lab2seg.lua
More file actions
112 lines (93 loc) · 3.1 KB
/
Copy pathshiro-lab2seg.lua
File metadata and controls
112 lines (93 loc) · 3.1 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
--[[
SHIRO
===
Copyright (c) 2017-2018 Kanru Hua. All rights reserved.
This file is part of SHIRO.
SHIRO is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
SHIRO is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with SHIRO. If not, see <http://www.gnu.org/licenses/>.
]]
local mypath = arg[0]:match("(.-)[^\\/]+$")
package.path = package.path .. ";" ..
mypath .. "?.lua;" .. mypath .. "external/?.lua"
json = require("dkjson")
getopt = require("getopt")
shiro_cli = require("cli-common")
opts = getopt(arg, "mdteE")
if opts.h then
print("Usage:")
print("shiro-lab2seg.lua path-to-index-file\n" ..
" -m path-to-phonemap -d label-directory -t hop-time\n" ..
" -e label-extension -E feature-extension")
return
end
local input_index = opts._[1]
local input_phonemap = opts.m
local hop_time = tonumber(opts.t)
local ext = opts.e or ".txt"
local ext_feature = opts.E or ".f"
if input_index == nil then
print("Error: shiro-lab2seg requires an input index file.")
return
end
if input_phonemap == nil then
print("Error: shiro-lab2seg requires an input phonemap.")
return
end
if hop_time == nil then
hop_time = 0.01
end
-- read and parse index file
local file_list = shiro_cli.load_index_file(input_index,
(opts.d or ".") .. "/", {}, {})
if file_list == false then return end
local fh = io.open(input_phonemap, "r")
if fh == nil then
print("Error: cannot open " .. input_phonemap)
return
end
local pm = json.decode(fh:read("*a"))
io.close(fh)
if shiro_cli.checkpm(pm) == false then return end
local seg = {file_list = {}}
for i = 1, #file_list do
local label_path = file_list[i].path .. ext
local feature_path = file_list[i].path .. ext_feature
fh = io.open(label_path)
if fh == nil then
print("Error: cannot open " .. label_path)
return
end
local lab = shiro_cli.parse_lab(fh:read("*a"))
local states = {}
for j = 1, #lab do
local p = lab[j].label
local pst = pm.phone_map[p]
if pst == nil then
print("Error: phoneme " .. p .. " is not defined in the phone map.")
return
end
local t0, t1 = lab[j].t0, lab[j].t1
local nst = #pst.states
for k = 1, nst do
states[#states + 1] = {
time = math.ceil((t0 + (t1 - t0) * k / nst) / hop_time),
dur = pst.states[k].dur, -- duration state
out = pst.states[k].out, -- output state for each stream
jmp = {}, -- jumps (besides forward transition)
ext = {p, k - 1} -- extra information
}
end
end
seg.file_list[i] = {filename = feature_path, states = states}
io.close(fh)
end
print(json.encode(seg, {indent = true,
keyorder = {"filename", "states", "time", "dur", "out", "jmp", "ext"}}))