-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.lua
59 lines (50 loc) · 1.48 KB
/
options.lua
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
options = {}
function options.appendOptions(text, options, ignore)
for o, v in pairs(options) do
if type(v) == 'number' then
v = string.format('%3.2e',v)
end
if not ignore[o] then
text = text..'-'..o..'-'..tostring(v)
end
end
return text
end
function options.parseOptions(text, default)
local results = {}
for k, v in pairs(default) do
results[k] = v
end
for pattern in string.gmatch(text, '%-.-%-%d*') do
local i1, i2 = string.find(pattern, '%a+')
local key = string.sub(pattern, i1, i2)
i1, i2 = string.find(pattern, '%d+')
local value = string.sub(pattern, i1, i2)
value = tonumber(value) or value
results[key] = value
end
return results
end
function options.toT7(infile, outfile, vocabfile)
local f = torch.DiskFile(infile)
local rawdata = f:readString('*a')
f:close()
local unordered = {}
for char in rawdata:gmatch'.' do
if not unordered[char] then unordered[char] = true end
end
local ordered = {}
for char in pairs(unordered) do ordered[#ordered+1]=char end
table.sort(ordered)
local vocab_mapping = {}
for i, char in ipairs(ordered) do
vocab_mapping[char] = i
end
local data = torch.ByteTensor(#rawdata)
for i=1, #rawdata do
data[i] = vocab_mapping[rawdata:sub(i, i)]
end
torch.save(outfile, data)
torch.save(vocabfile, vocab_mapping)
end
return options