Subversion Repositories SmartDukaan

Rev

Rev 3453 | Rev 4198 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3232 varun.gupt 1
'''
2
Created on 26-Aug-2011
3
 
4
@author: Varun Gupta
5
'''
3350 varun.gupt 6
import json, sys, os
3232 varun.gupt 7
 
3350 varun.gupt 8
cmd_folder = os.path.dirname(os.path.abspath(os.environ["HOME"] + "/code/trunk/PyProj/src/shop2020/"))
9
if cmd_folder not in sys.path:
10
    sys.path.insert(0, cmd_folder)
11
 
12
from shop2020.clients.CatalogClient import CatalogClient
13
 
3453 varun.gupt 14
CHARACTER_ENCODING = 'ISO-8859-1'
15
 
16
class BrandAndModelExtracter:
17
 
18
    def __init__(self):
19
 
20
        try:
21
            client = CatalogClient().get_client()
22
            self.brands = client.getAllBrandsByCategory(10001)
23
        except Exception:
24
            self.brands = ['Micromax', 'BlackBerry', 'Blackberry', 'Motorola', 'Alcatel', 'Sony Ericsson', 'Apple', \
25
                      'Spice', 'Nokia', 'HTC', 'Samsung', 'LG', 'Dell', 'Karbonn', 'Lava']
26
 
27
        self.brands.append('Blackberry') #To resolve issue of 'BlackBerry' and 'Blackberry'
28
 
29
    def extract(self, full_name):
4039 varun.gupt 30
        full_name = full_name.strip()
3453 varun.gupt 31
 
32
        for brand in self.brands:
33
            if full_name.startswith(brand):  return (brand, full_name.replace(brand, '').strip())
34
 
35
        return ("", full_name)
36
 
3232 varun.gupt 37
def isValidRule(rule):
38
    try:
39
        if rule is None:
40
            return False
41
 
42
        elif rule['url'] is None:
43
            return False
44
 
45
        elif rule['source'] is None:
46
            return False
47
 
48
        else:
49
            return True
50
 
51
    except KeyError:
52
        return False
53
 
54
def getItemsWithTopScore(items):
55
    filterd_items = []
56
    top_score = -1.0
57
 
58
    for item in items:
59
        if item['score'] >= top_score:
60
            filterd_items.append(item)
61
            top_score = item['score'] 
62
        else:
63
            return filterd_items
64
 
65
    return filterd_items
66
 
67
def isPriceSame(items):
68
    for i in range(0, items.__len__() - 1):
69
        if items[i]['price'] != items[i + 1]['price']:    return False
70
 
3313 varun.gupt 71
    return True
72
 
73
def getProductClusters(products):
74
    '''
75
    Receives a list of products (returned from search results) &
76
    returns a clustered dictionary, where products are grouped by
77
    the 'source'
78
    '''
79
    clustered_results = {'flipkart': [], 'homeshop18': [], 'infibeam': [], 'letsbuy': []}
80
 
81
    for product in products:
82
        clustered_results[product['source']].append(product)
83
 
84
    return clustered_results
85
 
86
def getFilteredClustersWithTopScores(product_clusters):
87
    filtered_cluster = {}
88
 
89
    for source, products in product_clusters.iteritems():
90
        filtered_cluster[source] = getItemsWithTopScore(products)
91
 
92
    return filtered_cluster
93
 
94
def removePriceFormatting(price_string):
95
    return price_string.replace('Rs.', '').replace(',', '').strip()
96
 
97
def getDisplayInfo(filtered_cluster):
98
    display_info = {'flipkart': {}, 'homeshop18': {}, 'infibeam': {}, 'letsbuy': {}}
99
 
100
    for source, products in filtered_cluster.iteritems():
101
 
102
        if len(products) > 0:
103
            if isPriceSame(products):
104
                display_info[source]['price'] = products[0]['price']
105
                display_info[source]['data'] = None
106
                display_info[source]['url'] = products[0]['url']
107
                display_info[source]['text'] = removePriceFormatting(products[0]['price'])
3453 varun.gupt 108
                display_info[source]['title'] = products[0]['name']
3313 varun.gupt 109
            else:
110
                display_info[source]['price'] = None
111
                display_info[source]['data'] = json.dumps(products)
112
                display_info[source]['url'] = None
113
                display_info[source]['text'] = 'Conflict'
114
        else:
115
            display_info[source]['price'] = None
116
            display_info[source]['data'] = None
117
            display_info[source]['url'] = None
118
            display_info[source]['text'] = 'Not Found'
119
 
3453 varun.gupt 120
    return display_info
121
 
122
def getSynonyms():
123
    file_path = '/tmp/synonyms.json'
124
    file = open(file_path, 'r')
125
    synonyms_json = file.read()
126
 
127
    synonyms = {}
128
    for key, value in json.loads(synonyms_json).iteritems():
129
        list_synonyms = []
130
 
131
        if 'MODEL_NAME' in value:
132
            list_synonyms.extend(value['MODEL_NAME'])
133
 
134
        if 'MODEL_NUMBER' in value:
135
            list_synonyms.extend(value['MODEL_NUMBER'])
136
 
137
        synonyms[int(key)] = list_synonyms
138
 
139
    return synonyms
140
 
141
if __name__ == '__main__':
142
    extracter = BrandAndModelExtracter()
143
#    print extracter.extract('Nokia X5-01 (Pink)')
144
    print getSynonyms()