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

avoid collisions when decoding bad unicode #2411

Merged
merged 5 commits into from
Apr 6, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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: 12 additions & 1 deletion gensim/models/_fasttext_bin.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import struct

import numpy as np
import six

_END_OF_WORD_MARKER = b'\x00'

Expand Down Expand Up @@ -160,6 +161,16 @@ def _load_vocab(fin, new_format, encoding='utf-8'):
"""
vocab_size, nwords, nlabels = _struct_unpack(fin, '@3i')

#
# We must use backslashreplace instead of replace or ignore here, because
# we must avoid collisions in the decoded word, e.g.
# https://github.com/RaRe-Technologies/gensim/issues/2402
#
# Unfortunately, backslashreplace is only available on Py3. On Py2, we
# can't really do anything to avoid collisions.
mpenkov marked this conversation as resolved.
Show resolved Hide resolved
#
errors = 'backslashreplace' if six.PY3 else 'replace'

# Vocab stored by [Dictionary::save](https://github.com/facebookresearch/fastText/blob/master/src/dictionary.cc)
if nlabels > 0:
raise NotImplementedError("Supervised fastText models are not supported")
Expand All @@ -182,7 +193,7 @@ def _load_vocab(fin, new_format, encoding='utf-8'):
try:
word = word_bytes.decode(encoding)
except UnicodeDecodeError:
word = word_bytes.decode(encoding, errors='ignore')
word = word_bytes.decode(encoding, errors=errors)
logger.error(
'failed to decode invalid unicode bytes %r; ignoring invalid characters, using %r',
word_bytes, word
Expand Down
16 changes: 12 additions & 4 deletions gensim/test/test_fasttext.py
Original file line number Diff line number Diff line change
Expand Up @@ -1261,10 +1261,18 @@ def test_bad_unicode(self):
buf.seek(0)

raw_vocab, vocab_size, nlabels = gensim.models._fasttext_bin._load_vocab(buf, False)
expected = {
u'英語版ウィキペディアへの投稿はいつでも': 1,
u'административно-территориальн': 2,
}

if six.PY3:
expected = {
u'英語版ウィキペディアへの投稿はいつでも\\xe6': 1,
u'административно-территориальн\\xd1': 2,
}
else:
expected = {
u'英語版ウィキペディアへの投稿はいつでも�': 1,
u'административно-территориальн�': 2,
}

self.assertEqual(expected, dict(raw_vocab))

self.assertEqual(vocab_size, 2)
Expand Down