def __init__(self, name="", description=""): """ Loads an `Item` object from its description and an associated name. :param name: The Item's name. :param description: A textual description of what the Item is about or describes. This is mined at initialization for the concepts which are associated with this Item's ConceptModel(). """ self.name = name self.description = description if len(description) > 0: self.model = model_input(description) else: self.model = ConceptModel()
def __init__(self, model=ConceptModel(), user_id='', exceptions=None, password=''): """ :param model: The ConceptModel() initially associated with the user. An empty one by default. :param user_id: The id associated with the user. An empty string by default. :param exceptions: The exceptions (items already viewed and either acted upon or passed on) associated with \ the user. :param password: The password associated with the user. An empty string by default. """ self.id = user_id self.model = model if exceptions: self.exceptions = exceptions self.password = password
def input_interest(self, interest, level=0, limit=20): """ Resolves arbitrary user input to concepts, explodes the resultant nodes, and adds the resultant graph to the user's present one. :param interest: Arbitrary user input. :param level: The limit placed on the depth of the graph. A limit of 0 is highest, corresponding with the most popular articles; a limit of 5 is the broadest and graphs to the widest cachet of articles. This parameter is a parameter that is passed directly to the IBM Watson API call. :param limit: a cutoff placed on the number of related concepts to be returned. This parameter is passed directly to the IBM Watson API call. """ mapped_concept = conceptualize(interest) if mapped_concept: mapped_model = ConceptModel([mapped_concept]) mapped_model.get_node(mapped_concept).properties['relevance'] = 1.0 mapped_model.explode(level=level, limit=limit) # Set relevancies based on edge weights. for node in list(mapped_model.graph[mapped_model.get_node(mapped_concept)].keys()): node.properties['relevance'] = mapped_model.graph[mapped_model.get_node(mapped_concept)][node]['weight'] self.model.merge_with(mapped_model)
class Item: """ The Item object is a generic container for the objects that the application is trying to recommend to its users. """ description = "" name = "" model = None def __init__(self, name="", description=""): """ Loads an `Item` object from its description and an associated name. :param name: The Item's name. :param description: A textual description of what the Item is about or describes. This is mined at initialization for the concepts which are associated with this Item's ConceptModel(). """ self.name = name self.description = description if len(description) > 0: self.model = model_input(description) else: self.model = ConceptModel() def nodes(self): """ :return: The nodes in the Item model. """ return self.model.nodes() def concepts(self): """ :return: The concepts in the Item model. """ return self.model.concepts() def relevancies(self): """ :return: Sorted (relevance, concept) pairs associated with the Item. """ return sorted( [("{0:.3f}".format(node.properties['relevance']), node.concept) for node in self.nodes()], reverse=True) def to_json(self): """ Returns a JSON of the Item object suitable for storage. Counter-operation to `load_from_json()`. :return: A JSON serialization of the Item object which is suitable for storage. """ return { "name": self.name, "model": self.model.to_json(), "description": self.description, } def load_from_json(self, data): """ Loads an Item from its JSON serialization. Counter-operation to `to_json()`. :param data: The JSON data being loaded into an Item object. """ self.model.load_from_json(data['model']) self.description = data['description'] def save(self, filename='items.json'): """ Saves the Item to a JSON representation. :param filename: The filename for the items storage file; `items.json` is the default. """ item_schema = self.to_json() if filename not in [f for f in os.listdir('.') if os.path.isfile(f)]: new_file_schema = {"items": [item_schema]} f = open(filename, 'w') f.write(json.dumps(new_file_schema, indent=4)) f.close() else: data = json.load(open(filename)) names = [item['name'] for item in data['items']] if self.name not in names: data['items'].append(item_schema) with open(filename, 'w') as outfile: json.dump(data, outfile, indent=4) if self.name in names: user_index = 0 for i in range(0, len(data['items'])): if data['items'][i]['name'] == self.name: user_index = i break data['items'][user_index] = item_schema with open(filename, 'w') as outfile: json.dump(data, outfile, indent=4) def load(self, filename="items.json"): """ Loads the Item from a JSON representation. :param filename: The filename for the items storage file; `items.json` is the default. """ if filename not in [f for f in os.listdir('.') if os.path.isfile(f)]: raise IOError("The item definitions file" + filename + " appears to be missing!") list_of_items = json.load(open(filename))['items'] for item in list_of_items: if item['name'] != self: continue else: self.load_from_json(item)
class Item: """ The Item object is a generic container for the objects that the application is trying to recommend to its users. """ description = "" name = "" model = None def __init__(self, name="", description=""): """ Loads an `Item` object from its description and an associated name. :param name: The Item's name. :param description: A textual description of what the Item is about or describes. This is mined at initialization for the concepts which are associated with this Item's ConceptModel(). """ self.name = name self.description = description if len(description) > 0: self.model = model_input(description) else: self.model = ConceptModel() def nodes(self): """ :return: The nodes in the Item model. """ return self.model.nodes() def concepts(self): """ :return: The concepts in the Item model. """ return self.model.concepts() def relevancies(self): """ :return: Sorted (relevance, concept) pairs associated with the Item. """ return sorted([("{0:.3f}".format(node.properties['relevance']), node.concept) for node in self.nodes()], reverse=True) def to_json(self): """ Returns a JSON of the Item object suitable for storage. Counter-operation to `load_from_json()`. :return: A JSON serialization of the Item object which is suitable for storage. """ return { "name": self.name, "model": self.model.to_json(), "description": self.description, } def load_from_json(self, data): """ Loads an Item from its JSON serialization. Counter-operation to `to_json()`. :param data: The JSON data being loaded into an Item object. """ self.model.load_from_json(data['model']) self.description = data['description'] def save(self, filename='items.json'): """ Saves the Item to a JSON representation. :param filename: The filename for the items storage file; `items.json` is the default. """ item_schema = self.to_json() if filename not in [f for f in os.listdir('.') if os.path.isfile(f)]: new_file_schema = { "items": [item_schema] } f = open(filename, 'w') f.write(json.dumps(new_file_schema, indent=4)) f.close() else: data = json.load(open(filename)) names = [item['name'] for item in data['items']] if self.name not in names: data['items'].append(item_schema) with open(filename, 'w') as outfile: json.dump(data, outfile, indent=4) if self.name in names: user_index = 0 for i in range(0, len(data['items'])): if data['items'][i]['name'] == self.name: user_index = i break data['items'][user_index] = item_schema with open(filename, 'w') as outfile: json.dump(data, outfile, indent=4) def load(self, filename="items.json"): """ Loads the Item from a JSON representation. :param filename: The filename for the items storage file; `items.json` is the default. """ if filename not in [f for f in os.listdir('.') if os.path.isfile(f)]: raise IOError("The item definitions file" + filename + " appears to be missing!") list_of_items = json.load(open(filename))['items'] for item in list_of_items: if item['name'] != self: continue else: self.load_from_json(item)