Source code for pypolibox.propositions

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Arne Neumann <arne-neumann@web.de>

"""
The ``propositions`` module evaluates the facts generated by the 
:class:`pypolibox.facts` module and stores its results as nested dictionaries.
"""

[docs]class AllPropositions: """ contains propositions about ALL the books that were listed in a query result """ def __init__ (self, allfacts): """ :type allfacts: ``AllFacts`` """ self.books = [] for book in allfacts.books: self.books.append(Propositions(book)) def __str__(self): return_string = "" for index, book in enumerate(self.books): return_string += "propositions about book #{0}:\n".format(index) + \ "----------------------------\n" + \ "{0}\n\n".format(book) return return_string
[docs]class Propositions(): """ represents propositions (positive/negative/neutral ratings) of a single book. Propositions() are generated from Facts() about a Book(). """ def __init__ (self, facts): """ :type facts: ``Facts`` """ facts = facts.facts # a Facts() stores its facts in .facts; this line saves some typing self.book_score = facts['query_facts']['book_score'] propositions = {} propositions['usermodel_match'] = {} propositions['usermodel_nomatch'] = {} propositions['lastbook_match'] = {} propositions['lastbook_nomatch'] = {} propositions['extra'] = {} propositions['id'] = {} for attribute, value in facts['query_facts']['usermodel_match'].iteritems(): propositions['usermodel_match'][attribute] = (value, 'positive') for attribute, value in facts['query_facts']['usermodel_nomatch'].iteritems(): propositions['usermodel_nomatch'][attribute] = (value, 'negative') if facts.has_key('lastbook_facts'): # 1st book doesn't have this for attribute, value in facts['lastbook_facts']['lastbook_match'].iteritems(): propositions['lastbook_match'][attribute] = (value, 'neutral') # neutral (not positive, since it's not related 2 usermodel) for attribute, value in facts['lastbook_facts']['lastbook_nomatch'].iteritems(): propositions['lastbook_nomatch'][attribute] = (value, 'neutral') if facts['extra_facts'].has_key('year'): if facts['extra_facts']['year'] == 'recent': propositions['extra']['year'] = (facts['extra_facts']['year'], 'positive') elif facts['extra_facts']['year'] == 'old': propositions['extra']['year'] = (facts['extra_facts']['year'], 'negative') if facts['extra_facts'].has_key('pages'): propositions['extra']['pages'] = (facts['extra_facts']['pages'], 'neutral') for fact in facts['id_facts']: already_used_propositions = self.__do_not_use_twice(propositions) if fact not in already_used_propositions: propositions['id'][fact] = (facts['id_facts'][fact], 'neutral') self.propositions = propositions def __do_not_use_twice(self, propositions): """generates the set of proposition attributes that have been used before (e.g. as usermodel propositions, lastbook propositions, extra propositions) and should therefore not be used again to generate id propositions Example: If there is an Extra/UserModelMatch etc. Proposition about "Pages" (e.g. >= 600) or Year, there should be no ID Proposition about the same fact. """ attributes = set() for proposition_type in propositions.keys(): attrib_list = propositions[proposition_type].keys() for attribute in attrib_list: attributes.add(attribute) return attributes def __str__(self): """returns a string representation of a Propositions() instance omitting empty values""" return_string = "" return_string += "book score: {0}\n".format(self.book_score) for key, value in self.propositions.iteritems(): if value: # if value is not empty return_string += "\n{0}:\n".format(key) for attrib, val in value.iteritems(): if val: return_string += "\t{0}: {1}\n".format(attrib, val) return return_string