Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add utf8 character support #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions util/CharSplitLMMinibatchLoader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,11 @@ function CharSplitLMMinibatchLoader.text_to_tensor(in_textfile, out_vocabfile, o
print('creating vocabulary mapping...')
-- record all characters to a set
local unordered = {}
for char in rawdata:gmatch'.' do
local len = 0
-- code snippets taken from http://lua-users.org/wiki/LuaUnicode
for char in string.gfind(rawdata, "([%z\1-\127\194-\244][\128-\191]*)") do
if not unordered[char] then unordered[char] = true end
len = len + 1
end
-- sort into a table (i.e. keys become 1..N)
local ordered = {}
Expand All @@ -109,9 +112,11 @@ function CharSplitLMMinibatchLoader.text_to_tensor(in_textfile, out_vocabfile, o
end
-- construct a tensor with all the data
print('putting data into tensor...')
local data = torch.ByteTensor(#rawdata) -- store it into 1D first, then rearrange
for i=1, #rawdata do
data[i] = vocab_mapping[rawdata:sub(i, i)] -- lua has no string indexing using []
local data = torch.ShortTensor(len) -- store it into 1D first, then rearrange
local pos = 1
for char in string.gfind(rawdata, "([%z\1-\127\194-\244][\128-\191]*)") do
data[pos] = vocab_mapping[char]
pos = pos + 1
end

-- save output preprocessed files
Expand Down