This repository has been archived by the owner on Feb 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsetup.lua
141 lines (124 loc) · 4.41 KB
/
setup.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
--[[
greedy-ocr
Original Work Copyright (c) 2015 Sebastian Spaar
------------------------------------------------------------------------
setup.lua
]]
local config = require "_config"
local PROTOTYPE_DIR = config.prototypes_directory
local PAGES_DIR = config.pages_directory
local BIGRAM_FILE = config.corpus_filename
-- load_prototypes:
-- Checks the Prototypes directory (specified in `_config.lua') and
-- creates a Prototype for each image found.
-- IMPORTANT: Files that start with a `.' or an `_' are ignored. This
-- is to ignore hidden files, e.g. `.DS_Store', and to enable the user
-- to put images inside the Prototypes directory that get loaded at
-- elsewhere at another time.
--
-- @params:
-- @returns:
function load_prototypes ()
local content = love.filesystem.getDirectoryItems(PROTOTYPE_DIR)
local prototypes = {}
for _, directory in pairs(content) do
if love.filesystem.isDirectory(PROTOTYPE_DIR .. "/" .. directory)
and directory:sub(1, 1) ~= "."
and directory:sub(1, 1) ~= "_" then
table.insert(prototypes, directory)
end
if directory == "_capitals" then
local capitals = love.filesystem.getDirectoryItems(PROTOTYPE_DIR .. "/_capitals")
for _, prototype in pairs(capitals) do
if prototype:sub(1, 1) ~= "." and prototype:sub(1, 1) ~= "_" then
print(prototype)
local prot_image = love.graphics.newImage(PROTOTYPE_DIR .. "/_capitals/" .. prototype)
local literal = explode(".", prototype)[1]
Entities.Prototype(literal, prot_image)
end
end
end
end
local images = {}
for _, prototype in pairs(prototypes) do
images = love.filesystem.getDirectoryItems(PROTOTYPE_DIR .. "/" .. prototype)
for _, image in pairs(images) do
if image:sub(1, 1) ~= "." and image:sub(1, 1) ~= "_" then
local prot_image = love.graphics.newImage(PROTOTYPE_DIR .. "/" .. prototype .. "/" .. image)
Entities.Prototype(prototype, prot_image)
end
end
end
for _, prototype in pairs(config.additional_prototypes) do
Entities.Prototype(prototype[1], love.graphics.newImage(prototype[2]))
end
end
-- load_image:
-- Goes inside the Pages directory (specified in `_config.lua') and
-- looks for a pair of an image and a Lua file with the same name.
-- This Lua file should contain the bounding boxes of the words found
-- in the image, and will be read using `dofile()'.
--
-- @params:
-- @returns:
function load_image ()
local pages = love.filesystem.getDirectoryItems(PAGES_DIR)
for _, filename in pairs(pages) do
if filename:sub(1, 1) ~= "."
and filename:sub(1, 1) ~= "_" then
local file = explode(".", filename)
local name = file[1]
local suffix = file[2]
if suffix ~= "lua" then
local image_filename = PAGES_DIR .. "/" .. name .. "." .. suffix
local image = love.graphics.newImage(image_filename)
local lua_filename = PAGES_DIR .. "/" .. name .. "." .. "lua"
return Entities.Page(image, dofile(lua_filename))
end
end
end
end
-- load_bigram:
-- Takes the corpus specified in `_config.lua' and creates a bigram
-- model over the letters.
--
-- @params:
-- @returns: MODEL: The bigram
-- @type: table
function load_bigram (reverse)
local function allletters (corpus_file)
local line = corpus_file:read()
local pos = 1
return function ()
while line do
local s, e = string.find(line, "[%g%s]", pos)
if s then
pos = e + 1
return string.sub(line, s, e)
else
line = corpus_file:read()
pos = 1
end
end
return nil
end
end
local corpus_file = io.open(BIGRAM_FILE)
local w1, w2 = "", ""
local MODEL = {}
for w in allletters(corpus_file) do
if reverse then
w2 = w1; w1 = w;
else
w1 = w2; w2 = w;
end
if MODEL[w1] == nil then
MODEL[w1] = LanguageModel.Bag()
MODEL[w1]:insert(w2)
else
MODEL[w1]:insert(w2)
end
end
corpus_file:close()
return MODEL
end