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 3 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), key=lambda x: (x[1], x[0]))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorting by (freq, w), are you sure that this is correct?

Copy link
Contributor Author

@formi23 formi23 Nov 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@menshikh-iv, that's a good point. Either way works and ensures reproducibility. It can be assumed that sorting integers is faster than strings, but then the branching for words with the same frequency seems to introduce overhead (see the figure, where freq indicates frequency-word sorting and string indicates word-frequency sorting, i, f, c, and d methods are as in the previous example).
string-integer

Since the words in token2id guaranteed to be unique but the frequencies are not (and will likely overlap given patterns within natural language), sorting by token then frequency is probably the better choice but will vary on a dataset-by-dataset basis. What's your preference?

Copy link
Contributor

@menshikh-iv menshikh-iv Nov 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO (w, freq) better than (freq, w) (just because of the uniqueness of the tokens). For bigger corpus, the situation will be even worse for (freq, w) because frequencies obey zipf's law -> more and more duplicates by freq in "tail" of distribution.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made the changes.

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