-
Notifications
You must be signed in to change notification settings - Fork 17
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
Feature/corpus analysis #183
Open
flavioamieiro
wants to merge
3
commits into
NAMD:develop
Choose a base branch
from
flavioamieiro:feature/corpus-analysis
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 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
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
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
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# coding: utf-8 | ||
# | ||
# Copyright 2012 NAMD-EMAP-FGV | ||
# | ||
# This file is part of PyPLN. You can get more information at: http://pypln.org/. | ||
# | ||
# PyPLN is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# PyPLN is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with PyPLN. If not, see <http://www.gnu.org/licenses/>. | ||
from pypln.backend.celery_task import PyPLNCorpusTask | ||
|
||
from collections import Counter | ||
|
||
class CorpusFreqDist(PyPLNCorpusTask): | ||
|
||
def process(self, documents): | ||
result = Counter() | ||
for document in documents: | ||
d = {} | ||
for word, count in document['freqdist']: | ||
d[word] = count | ||
result += Counter(d) | ||
return {'freqdist': result.most_common()} |
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# coding: utf-8 | ||
# | ||
# Copyright 2015 NAMD-EMAP-FGV | ||
# | ||
# This file is part of PyPLN. You can get more information at: http://pypln.org/. | ||
# | ||
# PyPLN is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# PyPLN is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with PyPLN. If not, see <http://www.gnu.org/licenses/>. | ||
from pypln.backend.celery_task import PyPLNCorpusTask | ||
from mock import MagicMock | ||
from utils import TaskTest | ||
|
||
|
||
class FakeCorpusTask(PyPLNCorpusTask): | ||
def process(self, documents): | ||
return {'result': sum([d["input"] for d in documents])} | ||
|
||
class TestCeleryCorpusTask(TaskTest): | ||
def test_task_should_only_get_the_correct_documents(self): | ||
# This is just preparing the expected input in the database | ||
wrong_doc_id = self.collection.insert({'input': 999}, w=1) | ||
correct_doc_id_1 = self.collection.insert({'input': 1}, w=1) | ||
correct_doc_id_2 = self.collection.insert({'input': 1}, w=1) | ||
fake_corpus_id = 1 | ||
|
||
FakeCorpusTask.process = MagicMock(return_value={'result': 2}) | ||
|
||
corpus_task = FakeCorpusTask() | ||
|
||
corpus_task.delay(fake_corpus_id, [correct_doc_id_1, correct_doc_id_2]) | ||
|
||
corpus_task.process.assert_called() | ||
|
||
# We need to compare the call args because it's called with a mongo | ||
# cursor, not a list. | ||
# We're getting [0][0] because we want the args (not kwargs) for the | ||
# first call to the method. | ||
call_args = list(corpus_task.process.call_args[0][0]) | ||
for arg in call_args: | ||
self.assertEqual(arg['input'], 1) | ||
|
||
def test_task_is_saving_the_result_to_mongo_with_the_corpus_id(self): | ||
expected_result = 42 | ||
doc_id_1 = self.collection.insert({'input': 21}, w=1) | ||
doc_id_2 = self.collection.insert({'input': 21}, w=1) | ||
fake_corpus_id = 1 | ||
|
||
FakeCorpusTask().delay(fake_corpus_id, [doc_id_1, doc_id_2]) | ||
|
||
resulting_corpus_analysis = self.corpora_collection.find_one( | ||
{'corpus_id': fake_corpus_id})['result'] | ||
|
||
self.assertEqual(resulting_corpus_analysis, expected_result) |
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# coding: utf-8 | ||
# | ||
# Copyright 2012 NAMD-EMAP-FGV | ||
# | ||
# This file is part of PyPLN. You can get more information at: http://pypln.org/. | ||
# | ||
# PyPLN is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# PyPLN is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with PyPLN. If not, see <http://www.gnu.org/licenses/>. | ||
from pypln.backend.workers import CorpusFreqDist | ||
from utils import TaskTest | ||
|
||
|
||
class TestCorpusFreqDistWorker(TaskTest): | ||
def test_freqdist_should_return_a_list_of_tuples_with_frequency_distribution(self): | ||
|
||
freqdist_1 = [[u'is', 2], [u'the', 2], [u'blue', 1], [u'sun', 1], | ||
[u'sky', 1], [u',', 1], [u'yellow', 1], [u'.', 1]] | ||
|
||
freqdist_2 = [[u'the', 2], [u'brown', 1], [u'lazy', 1], | ||
[u'over', 1], [u'fox', 1], [u'dog', 1], [u'.', 1], | ||
[u'quick', 1], [u'jumps', 1]] | ||
|
||
corpus_fd = [(u'the', 4), (u'is', 2), (u'.', 2), (u'blue', 1), | ||
(u'brown', 1), (u'lazy', 1), (u'fox', 1), (u'jumps', 1), | ||
(u'sun', 1), (u'dog', 1), (u'sky', 1), (u',', 1), | ||
(u'yellow', 1), (u'quick', 1), (u'over', 1)] | ||
|
||
result = CorpusFreqDist().process([{'freqdist': freqdist_1}, | ||
{'freqdist': freqdist_2}]) | ||
|
||
resulting_corpus_fd = result['freqdist'] | ||
|
||
self.assertEqual(resulting_corpus_fd, corpus_fd) |
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.
Wouldn't it be better to test
PyPLNCorpusTask
separately fromCorpusFreqDist
? Then later if another subclass ofPyPLNCorpusTask
is created only the returned dict would need to be checked.Also, is this hitting an actual mongo instance? If so, would you consider mocking the db methods?
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.
You are right. I was testing both in the same test case (and not testing correctly). I separated the tests and I think it's better now.
It is really hitting an actual mongo instance. This is inherited from the old days when MongoDict was still part of our codebase. It's also one of the reasons our tests are slow. I would be very glad to mock everything and have better, more isolated and quicker tests. I would probably need your help, though @geron :)