Rev 3453 | Rev 4199 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
'''Created on 26-Aug-2011@author: Varun Gupta'''import json, sys, oscmd_folder = os.path.dirname(os.path.abspath(os.environ["HOME"] + "/code/trunk/PyProj/src/shop2020/"))if cmd_folder not in sys.path:sys.path.insert(0, cmd_folder)from shop2020.clients.CatalogClient import CatalogClientCHARACTER_ENCODING = 'ISO-8859-1'class BrandAndModelExtracter:def __init__(self):try:client = CatalogClient().get_client()self.brands = client.getAllBrandsByCategory(10001)except Exception:self.brands = ['Micromax', 'BlackBerry', 'Blackberry', 'Motorola', 'Alcatel', 'Sony Ericsson', 'Apple', \'Spice', 'Nokia', 'HTC', 'Samsung', 'LG', 'Dell', 'Karbonn', 'Lava']self.brands.append('Blackberry') #To resolve issue of 'BlackBerry' and 'Blackberry'def extract(self, full_name):full_name = full_name.strip()for brand in self.brands:if full_name.startswith(brand): return (brand, full_name.replace(brand, '').strip())return ("", full_name)def isValidRule(rule):try:if rule is None:return Falseelif rule['url'] is None:return Falseelif rule['source'] is None:return Falseelse:return Trueexcept KeyError:return Falsedef getItemsWithTopScore(items):filterd_items = []top_score = -1.0for item in items:if item['score'] >= top_score:filterd_items.append(item)top_score = item['score']else:return filterd_itemsreturn filterd_itemsdef isPriceSame(items):for i in range(0, items.__len__() - 1):if items[i]['price'] != items[i + 1]['price']: return Falsereturn Truedef getProductClusters(products):'''Receives a list of products (returned from search results) &returns a clustered dictionary, where products are grouped bythe 'source''''clustered_results = {'flipkart': [], 'homeshop18': [], 'infibeam': [], 'letsbuy': []}for product in products:clustered_results[product['source']].append(product)return clustered_resultsdef getFilteredClustersWithTopScores(product_clusters):filtered_cluster = {}for source, products in product_clusters.iteritems():filtered_cluster[source] = getItemsWithTopScore(products)return filtered_clusterdef removePriceFormatting(price_string):return price_string.replace('Rs.', '').replace(',', '').strip()def getDisplayInfo(filtered_cluster):display_info = {'flipkart': {}, 'homeshop18': {}, 'infibeam': {}, 'letsbuy': {}}for source, products in filtered_cluster.iteritems():if len(products) > 0:if isPriceSame(products):display_info[source]['price'] = products[0]['price']display_info[source]['data'] = Nonedisplay_info[source]['url'] = products[0]['url']display_info[source]['text'] = removePriceFormatting(products[0]['price'])display_info[source]['title'] = products[0]['name']else:display_info[source]['price'] = Nonedisplay_info[source]['data'] = json.dumps(products)display_info[source]['url'] = Nonedisplay_info[source]['text'] = 'Conflict'else:display_info[source]['price'] = Nonedisplay_info[source]['data'] = Nonedisplay_info[source]['url'] = Nonedisplay_info[source]['text'] = 'Not Found'return display_infodef getSynonyms():file_path = '/tmp/synonyms.json'file = open(file_path, 'r')synonyms_json = file.read()synonyms = {}for key, value in json.loads(synonyms_json).iteritems():list_synonyms = []if 'MODEL_NAME' in value:list_synonyms.extend(value['MODEL_NAME'])if 'MODEL_NUMBER' in value:list_synonyms.extend(value['MODEL_NUMBER'])synonyms[int(key)] = list_synonymsreturn synonymsif __name__ == '__main__':extracter = BrandAndModelExtracter()# print extracter.extract('Nokia X5-01 (Pink)')print getSynonyms()