Ejemplo n.º 1
0
def _get_word_mentions_by_char(word_name, work_title, char_name):
    """Get the words that a said by a character of a certain work

    Args:
        word_name: the string of the word being searched (lowercase).
        work_title: the title of the work in which the character appears
            (titlecase).
        char_name: the name of the character (titlecase).

    Returns:
        A dictionary indexed by the work and the characters. This redundant data
        is created in order 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
    char = Character.get_by_id(char_name, parent=work.key)
    if not char:
        return {}, 0
    mentions = char.get_string_mentions()
    bold_mentions = _bold_mentions(word_name, mentions)
    mentions_dict = {work_title: {char_name: bold_mentions}}
    return mentions_dict, char.count
Ejemplo n.º 2
0
    def get(self):
        """Retrieves formatted information to the treemap visualization. It
           expects a list of elements, and each element is a list of the
           following type:

           [name, parent's name, value, color value]

           In which name and parent's name are strings, value is an integer
           proportional to the size of the resulting rectangle on the treemap
           and color value is the value to be used as color acording to the
           color range.

           It is called the function get_all_word_mentions to obtain a
           dictionary that maps from work and character to mentions.
        """
        searched_value = cgi.escape(self.request.get('searched_word').lower())

        if not searched_value:
            return
        
        all_mentions, count = _get_all_word_mentions(searched_value)
        if not count:
            return

        treemap_data = [['Location', 'Parent', 'Word Occurrences'],
            ['Shakespeare\'s Corpus', None, count]]

        word_db = Word.get_by_id(searched_value)
        for work in all_mentions:
            work_db = Work.get_by_id(work, parent=word_db.key)
            treemap_data.append([work, 'Shakespeare\'s Corpus', work_db.count]) 
            for char in all_mentions[work]:
                if not char:
                    continue
                char_db = Character.get_by_id(char, parent=work_db.key)
                treemap_data.append([{'v': work + '+' + char, 'f': char}, work, 
                    char_db.count])

        self.response.headers['Content-Type'] = 'text/json'
        self.response.out.write(json.encode({"array": treemap_data}))
Ejemplo n.º 3
0
 def get_short_string_characters(self, channel=None):
     characters = [Character.get_by_id(id) for id in self.characters]
     characters = ', '.join(f'***{c.name}***' for c in characters if c)
     return f'({characters})'
Ejemplo n.º 4
0
 def get_string_characters(self, channel=None):
     characters = [Character.get_by_id(id) for id in self.characters]
     characters = '***\n                ***'.join(c.name for c in characters
                                                  if c)
     return f'            _Characters:_\n                ***{characters}***'
Ejemplo n.º 5
0
 def get_short_string_opposition(self, channel=None):
     opposition = [Character.get_by_id(id) for id in self.opposition]
     opposition = ', '.join(c.name for c in opposition if c)
     return f' _({opposition})_'
Ejemplo n.º 6
0
 def get_string_opposition(self, channel=None):
     opposition = [Character.get_by_id(id) for id in self.opposition]
     opposition = '***\n                ***'.join(c.name for c in opposition
                                                  if c)
     return f'            _Opposition:_\n                ***{opposition}***'