-
-
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 functionality in TextCorpus to convert document text to index vectors #1720
Changes from 9 commits
91ab157
ddf9e04
4ba3714
2a08ce9
7e7fef7
0e6f793
8e10138
655cd04
4e6957f
dec80e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -173,6 +173,40 @@ def doc2bow(self, document, allow_update=False, return_missing=False): | |
else: | ||
return result | ||
|
||
def doc2idx(self, document, unknown_word_index=-1): | ||
"""Convert `document` (a list of words) into a list of indexes = list of `token_id`. | ||
|
||
Each word is assumed to be a **tokenized and normalized** string | ||
(either unicode or utf8-encoded). | ||
No further preprocessing is done on the words in `document`; apply tokenization, stemming etc. before calling | ||
this method. | ||
|
||
Replace all unknown words i.e, words not in the dictionary with the index as set via `unknown_word_index`, | ||
defaults to -1. | ||
|
||
Notes | ||
----- | ||
This function is `const`, aka read-only | ||
|
||
Parameters | ||
---------- | ||
document : list | ||
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.
|
||
List of words tokenized, normalized and preprocessed. | ||
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. No need to mention type twice |
||
unknown_word_index : int, optional | ||
Index to use for words not in the dictionary. | ||
|
||
Returns | ||
------- | ||
list | ||
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.
|
||
List of indexes in the dictionary for words in the `document` | ||
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. No need to mention type twice + add |
||
|
||
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. Please add example section (simple example that works how to apply this method) |
||
""" | ||
if isinstance(document, string_types): | ||
raise TypeError("doc2idx expects an array of unicode tokens on input, not a single string") | ||
|
||
document = [word if isinstance(word, unicode) else unicode(word, 'utf-8') for word in document] | ||
return [self.token2id.get(word, unknown_word_index) for word in document] | ||
|
||
def filter_extremes(self, no_below=5, no_above=0.5, keep_n=100000, keep_tokens=None): | ||
""" | ||
Filter out tokens that appear in | ||
|
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.
No needed indentation here