def _get_word_mentions_in_work(word_name, work_title): """Get all mentions of a word that appear in a certain work. Args: word_name: the string of the word being searched (lowercase). work_title: the title of the work (titlecase). Returns: A dictionary first indexed by work and second by character. The work is inserted to comply with the data pattern. """ word = Word.get_by_id(word_name) if not word: return {}, 0 work = Work.get_by_id(work_title, parent=word.key) if not work: return {}, 0 chars = Character.query(ancestor=work.key).fetch() mentions_dict = {work_title: {}} for char in chars: mentions = char.get_string_mentions() bold_mentions = _bold_mentions(word_name, mentions) mentions_dict[work_title][char.name] = bold_mentions return mentions_dict, work.count
def test_clear_datastore(self): """Tests if the database is being cleared and considers non-empty lists of instances for all the models used by the application. """ self.assertNotEquals(Word.query().fetch(), []) self.assertNotEquals(Work.query().fetch(), []) self.assertNotEquals(Character.query().fetch(), []) self.assertNotEquals(list(FileMetadata.all().run()), []) self.testapp.get('/') self.assertEquals(Word.query().fetch(), []) self.assertEquals(Work.query().fetch(), []) self.assertEquals(Character.query().fetch(), []) self.assertEquals(list(FileMetadata.all().run()), [])
def get(self): """Clears the datastore.""" ndb.delete_multi(Word.query().fetch(keys_only=True)) ndb.delete_multi(Work.query().fetch(keys_only=True)) ndb.delete_multi(Character.query().fetch(keys_only=True)) ndb.delete_multi(Line.query().fetch(keys_only=True)) db.delete(FileMetadata.all(keys_only=True).run()) self.redirect('/admin')
def test_filter_entities_using_query_works(self): '''We can search for all the entities starting from a word.''' retrieved_word = Word.get_by_id("death") self.assertEqual('death', retrieved_word.name) self.assertEqual(2, retrieved_word.count) retrieved_works = Work.query(ancestor=self.word.key).fetch() self.assertEqual(len(retrieved_works), 1) work = retrieved_works[0] retrieved_character = Character.query(ancestor=work.key).fetch() self.assertEqual(len(retrieved_character), 1) char = retrieved_character[0] self.assertEqual(1, len(char.mentions)) self.assertEqual("Though yet of Hamlet our dear brother's death", char.mentions[0].get().line)
def _get_work_characters(word_name, work_title): """Retrieves all the characters that mentions a word in a given work. Args: word_name: the string of the word which the characters mention (lowercase). work_title: the title of the work of interest (titlecase). Returns: A list with the names of the characters. """ word_db = Word.get_by_id(word_name) if not word_db: return [] work_db = Work.get_by_id(work_title, parent=word_db.key) if not work_db: return [] char_names = [char_db.name for char_db in Character.query(ancestor=work_db.key).fetch()] return char_names
def _get_all_word_mentions(word_name): """Get all the mentions of a certain word string representation accessed first by work and then by character. Args: word_name: the string representation of the word. Returns: A dictionary of dictionaries, being the first key the work title and the second, the character name. """ all_mentions = {} word = Word.get_by_id(word_name) if not word: return {}, 0 works = Work.query(ancestor=word.key) for work in works: work_chars = Character.query(ancestor=work.key) all_mentions[work.title] = {} for char in work_chars: mentions = char.get_string_mentions() bold_mentions = _bold_mentions(word.name, mentions) all_mentions[work.title][char.name] = bold_mentions return all_mentions, word.count