-
Notifications
You must be signed in to change notification settings - Fork 5
/
myutils.lua
96 lines (85 loc) · 1.98 KB
/
myutils.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
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
------------------------------------------------------------------------------
-- Multimodal Residual Networks for Visual QA
-- Jin-Hwa Kim, Sang-Woo Lee, Dong-Hyun Kwak, Min-Oh Heo,
-- Jeonghee Kim, Jung-Woo Ha, Byoung-Tak Zhang
-- https://arxiv.org/abs/1606.01455
-----------------------------------------------------------------------------
if not cjson then
cjson = require('cjson')
end
------------------------------------------------------------------------
-- Write to / Read from Json file
------------------------------------------------------------------------
function writeAll(file,data)
local f = io.open(file, "w")
f:write(data)
f:close()
end
function readAll(file)
local f = io.open(file, 'r')
local text = f:read()
f:close()
local json_file = cjson.decode(text)
return json_file
end
function saveJson(fname,t)
return writeAll(fname,cjson.encode(t))
end
function readFileAll(path)
local f = io.open(path)
local b = f:read('*all')
f:close()
return b
end
function paths.numfiles(s,regex)
local cnt = 0
for f in paths.iterfiles(s) do
if f:find(regex) then
cnt = cnt + 1
end
end
return cnt
end
function table.keys(tab)
local keys = {}
for k,v in pairs(tab) do
table.insert(keys, k)
end
return keys
end
function table.values(tab, unique)
local values = {}
if unique then
for k,v in pairs(tab) do
values[v] = 1
end
return table.keys(values)
else
for k,v in pairs(tab) do
table.insert(values, v)
end
return values
end
end
function table.intersect(a, b)
local union = {}
for i=1,#b do
for j=1,#a do
if b[i] == a[j] then
table.insert(union, a[j])
break
end
end
end
return union
end
function table.inverse(tab)
local _tab = {}
for k,v in pairs(tab) do
_tab[v] = k
end
return _tab
end
function string.trim(self)
return self:gsub('^%s+', ''):gsub('%s+$', '')
end