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

Make token2id mapping reproducible #1715

Merged
merged 5 commits into from
Nov 23, 2017
Merged
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
8 changes: 4 additions & 4 deletions gensim/corpora/dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ def doc2bow(self, document, allow_update=False, return_missing=False):

token2id = self.token2id
if allow_update or return_missing:
missing = {w: freq for w, freq in iteritems(counter) if w not in token2id}
missing = sorted(x for x in iteritems(counter) if x[0] not in token2id)
if allow_update:
for w in missing:
for w, _ in missing:
# new id = number of ids made so far;
# NOTE this assumes there are no gaps in the id sequence!
token2id[w] = len(token2id)
Expand All @@ -169,7 +169,7 @@ def doc2bow(self, document, allow_update=False, return_missing=False):
# return tokenids, in ascending id order
result = sorted(iteritems(result))
if return_missing:
return result, missing
return result, dict(missing)
else:
return result

Expand Down Expand Up @@ -266,7 +266,7 @@ def compactify(self):
logger.debug("rebuilding dictionary, shrinking gaps")

# build mapping from old id -> new id
idmap = dict(izip(itervalues(self.token2id), xrange(len(self.token2id))))
idmap = dict(izip(sorted(itervalues(self.token2id)), xrange(len(self.token2id))))

# reassign mappings to new ids
self.token2id = {token: idmap[tokenid] for token, tokenid in iteritems(self.token2id)}
Expand Down