witten
/
luminotes
Archived
1
0
Fork 0

Added very basic note summarizing for search results if the backend doesn't return a note summary (e.g. for SQLite).

This commit is contained in:
Dan Helfman 2008-08-19 22:15:54 -07:00
parent ee38d64302
commit 2fa2010dd9
2 changed files with 27 additions and 11 deletions

View File

@ -426,18 +426,30 @@ class Notebooks( object ):
note = summarize and self.summarize_note( note ) or note,
)
def summarize_note( self, note ):
def summarize_note( self, note, max_summary_length = None, word_count = None ):
"""
Create a truncated note summary for the given note, and then return the note with its summary
set.
Create a truncated, HTML-free note summary for the given note, and then return the note with
its summary set.
@type note: model.Note or NoneType
@param note: note to summarize, or None
@type max_summary_length: int or NoneType
@param max_summary_length: the length to which the summary is truncated (optional, defaults
to a reasonable length)
@type word_count: int or NoneType
@param word_count: the number of words to which the summary is truncated (optional, defaults
to a reasonable number of words)
@rtype: model.Note or NoneType
@return: note with its summary member set, or None if no note was provided
"""
MAX_SUMMARY_LENGTH = 40
word_count = 10
DEFAULT_MAX_SUMMARY_LENGTH = 40
DEFAULT_WORD_COUNT = 10
if not max_summary_length:
max_summary_length = DEFAULT_MAX_SUMMARY_LENGTH
if not word_count:
word_count = DEFAULT_WORD_COUNT
if note is None:
return None
@ -460,13 +472,13 @@ class Notebooks( object ):
truncated = False
summary = first_words( words, word_count )
while len( summary ) > MAX_SUMMARY_LENGTH:
while len( summary ) > max_summary_length:
word_count -= 1
summary = first_words( words, word_count )
# if the first word is just ridiculously long, truncate it without finding a word boundary
if word_count == 1:
summary = summary[ : MAX_SUMMARY_LENGTH ]
summary = summary[ : max_summary_length ]
truncated = True
break
@ -1111,6 +1123,9 @@ class Notebooks( object ):
notes = self.__database.select_many( Note, Notebook.sql_search_notes( user_id, notebook_id, search_text, self.__database.backend ) )
# make a summary for each note that doesn't have one
notes = [ note.summary and note or self.summarize_note( note, max_summary_length = 80, word_count = 30 ) for note in notes ]
return dict(
notes = notes,
)

View File

@ -3182,6 +3182,7 @@ class Test_notebooks( Test_controller ):
assert len( notes ) == 1
assert notes[ 0 ].object_id == self.note.object_id
assert notes[ 0 ].summary
def test_search_without_login( self ):
search_text = u"bla"
@ -3220,6 +3221,7 @@ class Test_notebooks( Test_controller ):
assert len( notes ) == 1
assert notes[ 0 ].object_id == self.note.object_id
assert notes[ 0 ].summary
def test_search_empty( self ):
self.login()
@ -3231,8 +3233,6 @@ class Test_notebooks( Test_controller ):
search_text = search_text,
), session_id = self.session_id )
notes = result.get( "notes" )
assert result[ "error" ]
assert u"missing" in result[ "error" ]
@ -3246,8 +3246,6 @@ class Test_notebooks( Test_controller ):
search_text = search_text,
), session_id = self.session_id )
notes = result.get( "notes" )
assert result[ "error" ]
assert u"too long" in result[ "error" ]
@ -3284,7 +3282,9 @@ class Test_notebooks( Test_controller ):
assert len( notes ) == 2
assert notes[ 0 ].object_id == note3.object_id
assert notes[ 0 ].summary
assert notes[ 1 ].object_id == self.note.object_id
assert notes[ 1 ].summary
def test_search_character_refs( self ):
self.login()
@ -3303,6 +3303,7 @@ class Test_notebooks( Test_controller ):
assert len( notes ) == 1
assert notes[ 0 ].object_id == note3.object_id
assert notes[ 0 ].summary
def test_search_titles( self ):
self.login()