-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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 method for patch corpora.Dictionary
based on special tokens
#2200
Merged
menshikh-iv
merged 9 commits into
piskvorky:develop
from
Froskekongen:patch_dictionary_based_on_special_tokens
Jan 11, 2019
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e3e2aeb
Function to patch dictionary
Froskekongen d75e2a1
Typo.
Froskekongen af975bd
Fix bug
Froskekongen c4d4a06
Fix tests and logic
Froskekongen 6fade67
Added doc2bow test.
Froskekongen 8045716
Code review
Froskekongen 1b4a43b
Merge branch 'patch_dictionary_based_on_special_tokens' of github.com…
Froskekongen d2451ee
Merge remote-tracking branch 'upstream/develop' into patch_dictionary…
menshikh-iv 782b69b
fix doc
menshikh-iv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -597,13 +597,12 @@ def merge_with(self, other): | |
def patch_with_special_tokens(self, special_token_dict): | ||
"""Patch token2id and id2token using a dictionary of special tokens. | ||
|
||
Use case | ||
-------- | ||
When doing sequence modeling (e.g. named entity recognition), one may | ||
want to specify special tokens that behave differently than others. | ||
|
||
**Usecase:** when doing sequence modeling (e.g. named entity recognition), one may want to specify | ||
special tokens that behave differently than others. | ||
One example is the "unknown" token, and another is the padding token. | ||
It is usual to set the padding token to have index `0`, and patching | ||
the dictionary with `{'<PAD>': 0}` would be one way to specify this. | ||
It is usual to set the padding token to have index `0`, and patching the dictionary with `{'<PAD>': 0}` | ||
would be one way to specify this. | ||
|
||
Parameters | ||
---------- | ||
|
@@ -612,16 +611,21 @@ def patch_with_special_tokens(self, special_token_dict): | |
|
||
Examples | ||
-------- | ||
>>> from gensim.corpora import Dictionary | ||
>>> | ||
>>> corpus = [["máma", "mele", "maso"], ["ema", "má", "máma"]] | ||
>>> dct = Dictionary(corpus) | ||
>>> special_tokens = {'pad': 0, 'space': 1} | ||
>>> print(dct.token2id) | ||
{'maso': 0, 'mele': 1, 'máma': 2, 'ema': 3, 'má': 4} | ||
>>> dct.patch_with_special_tokens(special_tokens) | ||
>>> print(dct.token2id) | ||
{'maso': 6, 'mele': 7, 'máma': 2, 'ema': 3, 'má': 4, 'pad': 0, 'space': 1} | ||
.. sourcecode:: pycon | ||
|
||
>>> from gensim.corpora import Dictionary | ||
>>> | ||
>>> corpus = [["máma", "mele", "maso"], ["ema", "má", "máma"]] | ||
>>> dct = Dictionary(corpus) | ||
>>> | ||
>>> special_tokens = {'pad': 0, 'space': 1} | ||
>>> print(dct.token2id) | ||
{'maso': 0, 'mele': 1, 'máma': 2, 'ema': 3, 'má': 4} | ||
>>> | ||
>>> dct.patch_with_special_tokens(special_tokens) | ||
>>> print(dct.token2id) | ||
{'maso': 6, 'mele': 7, 'máma': 2, 'ema': 3, 'má': 4, 'pad': 0, 'space': 1} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @menshikh-iv |
||
|
||
""" | ||
possible_ids = [] | ||
for token, idx in special_token_dict.items(): | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's
pycon
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's special section name for using
flake8
on docstringsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see #2192
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aha, OK. Can you update our Developer wiki page with such tricks?
Also (unrelated to this PR) with the workflow process you currently use to label and manage issues and PRs (the
interesting PR
label, time until closed as abandoned, CI tricks etc).