A have a dictionary as
dict = {
body = "Körper",
child = "Kind",
eye = "Auge"
face = "Gesicht"
}
And I have something like lookup function as:
function performLookup(words, dict)
for i, word in ipairs(words) do
if dict[word] then
words[i] = dict[word]
end
end
end
How to open the file, find the line (one line has just one word) and save it to other file? Ideally the dictionary must be also in the file and loaded as lookup table.
Update:
So, by having files dict.tsv
and locale_EN.txt
we creating a new locale file, but with accordingly replaced words in the file locale_DE.txt
.
(Updated with question edit)
function performLookup(words, dict)
for i, word in ipairs(words) do
if dict[word] then
words[i] = dict[word]
end
end
end
function readDict(fname)
local dict = {}
local file = io.open(fname, "r")
for line in file:lines() do
local english, german = line:match("(%S+)%s+(%S+)")
-- or \t instead of %s+ for single tab, or \t+ if multiple tabs allowed
dict[english] = german
end
file:close()
return dict
end
function readInput(fname)
local words = {}
local file = io.open(fname, "r")
for line in file:lines() do
table.insert(words, line)
end
file:close()
return words
end
function writeFile(fname, dict, words)
file = io.open(fname, "w")
performLookup(words, dict)
for i, line in ipairs(words) do
file:write((i == 1 and "" or "\n") .. line)
end
file:close()
end
dict = readDict("dict.tsv")
words = readInput("locale_EN.txt")
writeFile("locale_DE.txt", dict, words)
The input file (locale_EN.txt) looks like:
some
random
words
eye
and
a
few
more
face
The output file (locale_DE.txt) looks like:
some
random
words
Auge
and
a
few
more
Gesicht
The dictionary (dict.tsv) looks like:
body Körper
child Kind
eye Auge
face Gesicht
The question could have better clarity but is this what you are looking for? Would need some error checking too, since it assumes correct format.