Skip to content

Commit

Permalink
Added support for retreiving 'mentions' in search results that show w…
Browse files Browse the repository at this point in the history
…here hits came from in the document's text. Also added a test that passes. Fixes #42.
  • Loading branch information
palewire committed Apr 19, 2011
1 parent 6d6e621 commit 2d656c0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
12 changes: 12 additions & 0 deletions documentcloud/__init__.py
Expand Up @@ -144,6 +144,7 @@ def _get_search_page(self, query, page, per_page):
'q': query,
'page': page,
'per_page': per_page,
'mentions': 3,
}
data = self.fetch(u'search.json', params)
return data.get("documents")
Expand Down Expand Up @@ -370,6 +371,7 @@ class Document(BaseAPIObject):
def __init__(self, d):
self.__dict__ = d
self.resources = Resource(d.get("resources"))
self.mentions = [Mention(i) for i in d.get("mentions", [])] or None
self.created_at = dateparser(d.get("created_at"))
self.updated_at = dateparser(d.get("updated_at"))

Expand Down Expand Up @@ -740,6 +742,16 @@ def __init__(self, top, right, bottom, left):
self.left = left


class Mention(BaseAPIObject):
"""
A mention of a search found in the document.
"""
def __unicode__(self):
return unicode("%s: %s" % (self.page, self.text[:50]))




class Project(BaseAPIObject):
"""
A project returned by the API.
Expand Down
9 changes: 8 additions & 1 deletion test.py
Expand Up @@ -17,7 +17,7 @@
from documentcloud import DocumentCloud
from documentcloud import CredentialsMissingError, DuplicateObjectError
from documentcloud import CredentialsFailedError, DoesNotExistError
from documentcloud import Annotation, Document, Project, Section, Entity
from documentcloud import Annotation, Document, Project, Section, Entity, Mention
from private_settings import DOCUMENTCLOUD_USERNAME, DOCUMENTCLOUD_PASSWORD

#
Expand Down Expand Up @@ -157,6 +157,13 @@ def test_get_entities(self):
obj = self.public_client.documents.get(self.test_id)
self.assertEqual(type(obj.entities[0]), Entity)

def test_get_mentions(self):
"""
Test whether mentions exist.
"""
obj = self.public_client.documents.search(self.test_search)[0]
self.assertEqual(type(obj.mentions[0]), Mention)

def test_get_put(self):
"""
Test whether we can put random noise to all the editable fields.
Expand Down

0 comments on commit 2d656c0

Please sign in to comment.