-
-
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
Create local random generator for sample_text & add lenght #1422
Merged
menshikh-iv
merged 4 commits into
piskvorky:develop
from
vlejd:308_random_sample_on_stream
Jun 22, 2017
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
|
@@ -98,28 +98,56 @@ def get_texts(self): | |
else: | ||
yield utils.tokenize(line, lowercase=True) | ||
|
||
def sample_texts(self, n): | ||
def sample_texts(self, n, seed=None, length=None): | ||
""" | ||
Yield n random texts from the corpus without replacement. | ||
Yield n random documents from the corpus without replacement. | ||
|
||
Given the the number of remaingin elements in stream is remaining and we need | ||
to choose n elements, the probability for current element to be chosen is n/remaining. | ||
If we choose it, we just decreese the n and move to the next element. | ||
Given the number of remaining documents in a corpus, we need to choose n elements. | ||
The probability for the current element to be chosen is n/remaining. | ||
If we choose it, we just decrease the n and move to the next element. | ||
Computing the corpus length may be a costly operation so you can use the optional | ||
parameter `length` instead. | ||
|
||
Args: | ||
n (int): number of documents we want to sample. | ||
seed (int|None): if specified, use it as a seed for local random generator. | ||
length (int|None): if specified, use it as a guess of corpus length. | ||
It must be positive and not greater than actual corpus length. | ||
|
||
Yields: | ||
list[str]: document represented as a list of tokens. See get_texts method. | ||
|
||
Raises: | ||
ValueError: when n is invalid or length was set incorrectly. | ||
""" | ||
length = len(self) | ||
if not n <= length: | ||
raise ValueError("sample larger than population") | ||
random_generator = None | ||
if seed is None: | ||
random_generator = random | ||
else: | ||
random_generator = random.Random(seed) | ||
|
||
if length is None: | ||
length = len(self) | ||
|
||
if not n <= length: | ||
raise ValueError("n is larger than length of corpus.") | ||
if not 0 <= n: | ||
raise ValueError("negative sample size") | ||
raise ValueError("Negative sample size.") | ||
|
||
for i, sample in enumerate(self.get_texts()): | ||
remaining_in_stream = length - i | ||
chance = random.randint(1, remaining_in_stream) | ||
if i == length: | ||
break | ||
remaining_in_corpus = length - i | ||
chance = random_generator.randint(1, remaining_in_corpus) | ||
if chance <= n: | ||
n -= 1 | ||
yield sample | ||
|
||
if n != 0: | ||
# This means that length was set to be greater than number of items in corpus | ||
# and we were not able to sample enough documents before the stream ended. | ||
raise ValueError("length greater than number of documents in corpus") | ||
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. Can you make the message more concrete? Include the actual lengths. 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. ok |
||
|
||
def __len__(self): | ||
if not hasattr(self, 'length'): | ||
# cache the corpus length | ||
|
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
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.
Can you make the message more specific? Include the actual lengths.
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.
ok