def normalizeDB(): women_data = cdb.getFullData("looksmash_db", "looksmash_women") for doc in women_data: dn.normalize_data(doc) men_data = cdb.getFullData("looksmash_db", "looksmash_men") for doc in men_data: dn.normalize_data(doc) return "done" #cdb.printColVals("Sub_category") # # # with open('../DataFiles/snapdeal_data.json', 'rb') as f: # data = f.readlines() # # data = map(lambda x: x.rstrip(), data) # data_json_str = "[" + ','.join(data) + "]" # # data = json.loads(data_json_str) # # # # # # scored_data = [] # for curDoc in data: # # if curDoc['Sex'] != "Men": # doc = dn.normalize_data(curDoc) # # doc['score'] = re.computeAccentuateScores(doc, userPrefs) + re.computeSkinToneScores(doc, "fair") + re.computeColorScores(doc, colors) # #print doc # scored_data.append(doc) # # sorted_data = sorted(scored_data, key=itemgetter('score'), reverse=True) # # # top10 = itertools.islice(sorted_data, 10) # # print "Result" # # for doc in top10: # print doc # file = cStringIO.StringIO(urllib.urlopen(doc["Image"]).read()) # img = Image.open(file) # img.show()
def extractPrefsFromPhrase(phrase): mappedAttributes = {} standards = [] colors = [e.value for e in ls.Colors] mappedAttributes = mapAttributes(colors, "Colors", mappedAttributes) types = [e.value for e in ls.Type] occasions = ["Everyday wear", "Regular wear", "Party"] subCategories = [e.value for e in ls.Sub_Category] mappedAttributes = mapAttributes(subCategories, "Sub_category", mappedAttributes) mappedAttributes = mapAttributes(types, "Type", mappedAttributes) mappedAttributes = mapAttributes(occasions, "Occasions", mappedAttributes) print "here",mappedAttributes typeNormalization = cdb.getFullData("looksmash_normalization", "normalization")[0] typeMappings = dn.unionDict(nr.types_dict, dn.formatDict(typeNormalization["Type"])) standards.extend(occasions) standards.extend(colors) standards.extend(types) standards.extend(subCategories) standards = sorted(standards, key=len, reverse=True) ltypes = createMapper(types) accentuate = ["arms", "bust", "legs", "waist"] hide = ["arms", "bust", "stomach", "hips and thighs"] mapper = { "colors": [], "types": [], "accentuate": [], "hide": [], "skinTone": "", "bodyType": [] } words = tokenizeImpWords(phrase) phrases = [] phrases.extend(getTrivariates(words)) phrases.extend(getBivariates(words)) phrases.extend(words) thresholdMatches = thresholdPartialMatches(replaceSynonyms(phrases), standards) print thresholdMatches result = removeTypeDependency(getMappedAttributes(thresholdMatches, mappedAttributes), typeMappings) print "here",result return result
def getRatings(record, userPrefs): ratings = {"overall": 0, "body type": 0, "skin tone": 0, "message": ""} skinTone, smsg = re.computeSkinToneScores(record, userPrefs["skinTone"], []) #print type(skinTone) if record[ls.Attributes.Sex.value] == "Women": accentuate, amsg = re.computeAccentuateScores( record, userPrefs, cdb.getFullData("looksmash_rules", "accentuate_women")[0]) hide = re.computeHideScores( record, userPrefs, cdb.getFullData("looksmash_rules", "hide")[0]) #print "A", accentuate #print "H", hide bodyType = 0.6 * accentuate + 0.4 * hide overall = max(.3, float(bodyType + skinTone) / 2) message = getRatingMessage(overall) ratings["overall"] = overall ratings["body type"] = bodyType ratings["skin tone"] = skinTone ratings["message"] = message return ratings elif record[ls.Attributes.Sex.value] == "Men": bodyType, msg = re.computeAccentuateScoresForMen( record, userPrefs, cdb.getFullData("looksmash_rules", "accentuate_men")[0]) #print type(bodyType) overall = max(.3, float(bodyType + skinTone) / 2) message = getRatingMessage(overall) ratings["overall"] = overall ratings["body type"] = bodyType ratings["skin tone"] = skinTone ratings["message"] = message return ratings
def getPhraseProducts(userprefs): if "gender" not in userprefs.keys(): userprefs["gender"] = "Female" bodyPrefs = { "accentuate": { "arms": False, "bust": False, "waist": False, "legs" : False }, "bodyType": { "Oval": False, "Trapezoid": False, "Rectangle": False }, "hide": { "arms": False, "bust": False, "stomach": False, "hips and thighs": False } } phrase = userprefs["phrase"] mappedAttrs = pp.extractPrefsFromPhrase(phrase) colors = [] types = [] subCats = [] occasions = [] scores = cdb.getFullData("looksmash_rules", "accentuate_women")[0] scores_men = cdb.getFullData("looksmash_rules", "accentuate_men")[0] hideScores = cdb.getFullData("looksmash_rules", "hide")[0] if "Colors" in mappedAttrs.keys(): colors = mappedAttrs["Colors"] if "Type" in mappedAttrs.keys(): types = mappedAttrs["Type"] if "Sub_category" in mappedAttrs.keys(): subCats = mappedAttrs["Sub_category"] if "Occasion" in mappedAttrs.keys(): occasions = mappedAttrs["Occasion"] for color in userprefs["colors"]: colors.append(color) products = cdb.phraseQuery("looksmash_db", ["ShoppersStop"], colors, types, subCats, occasions) if userprefs["gender"] == "Female": for bodyPart in userprefs["accentuate"]: bodyPrefs["accentuate"][bodyPart.lower()] = True for bodyPart in userprefs["hide"]: bodyPrefs["hide"][bodyPart.lower()] = True elif userprefs["gender"] == "Male": for bodyType in userprefs["bodyType"]: bodyPrefs["bodyType"][bodyType.lower()] = True for doc in products: accentuateScore, accentuateMsg = re.computeAccentuateScores(doc, bodyPrefs, scores) if userprefs["gender"] == "Female" else re.computeAccentuateScoresForMen(doc, bodyPrefs, scores_men) hideScore = re.computeHideScores(doc, bodyPrefs, hideScores) if userprefs["gender"] == "Female" else 0 doc["score"] = accentuateScore + hideScore feed = sorted(products, key=itemgetter('score')) images = {} i=0 for product in feed: images[i]={"image" :product["Image"], # "msg" : product["msg"], "url" : product["Url"], "domain": product["Domain"], "score": product["score"], "Color": product["Color"], "Sub_category": product["Sub_category"], "Product Name": product["Product Name"], "Cost": product["Cost"], "Brand": product["Brand"], "Category": product["Category"], } if "Neck" in product.keys(): images[i]["Neck"] = product["Neck"], if "Sleeves" in product.keys(): images[i]["Sleeves"] = product["Sleeves"] i = i+1 return {"result": json.dumps(images)}
import RuleRepository.looksmashStandards as ls import DatabaseLayer.converseWithDB as cdb import operator import random rules = cdb.getFullData("looksmash_rules", "pairing")[0] domain = "Jabong" singleBMs = ["Skirts"] def getBucket(score): if score == 1 or score == .8: return 1 if score == .6: return .6 if score == .4 or score == .2: return .2 def createPairFeed(sorted_final): grouped_data = {} for pair in sorted_final: bucket = getBucket(pair["Score"]) if bucket not in grouped_data.keys(): grouped_data[bucket] = [] grouped_data[bucket].append(pair) sorted_keys = grouped_data.keys() sorted_keys.sort(reverse=True)
def getPairingRules(): rules = cdb.getFullData("looksmash_rules", "pairing")[0] return rules
def testFeed(userprefs): print userprefs accentuateWeight = .7 colorWeight = .5 skinToneWeight = .1 scores = cdb.getFullData("looksmash_rules", "accentuate_women")[0] scores_men = cdb.getFullData("looksmash_rules", "accentuate_men")[0] hideScores = cdb.getFullData("looksmash_rules", "hide")[0] normalization = cdb.getFullData("looksmash_normalization", "normalization")[0] bodyPrefs = { "accentuate": { "arms": False, "bust": False, "waist": False, "legs" : False }, "bodyType": { "Oval": False, "Trapezoid": False, "Rectangle": False }, "hide": { "arms": False, "bust": False, "stomach": False, "hips and thighs": False } } colors = userprefs['colors'] types = userprefs['types'] skinTone = userprefs['skinTone'] domains = ["Jabong"] if userprefs["gender"] == "Female": for bodyPart in userprefs["accentuate"]: bodyPrefs["accentuate"][bodyPart] = True for bodyPart in userprefs["hide"]: bodyPrefs["hide"][bodyPart] = True jabong_data = cdb.getFullDataWithColorsAndTypesFromDomain("looksmash_db", "looksmash_women", domains, ru.getColorList(colors,skinTone), types) elif userprefs["gender"] == "Male": for bodyType in userprefs["bodyType"]: bodyPrefs["bodyType"][bodyType] = True jabong_data = cdb.getFullDataWithColorsAndTypesFromDomain("looksmash_db", "looksmash_men", domains, ru.getColorList(colors,skinTone), types) scored_data = [] grouped_data = {} for doc in jabong_data: #doc = dn.normalize_data(doc, normalization) accentuateScore, accentuateMsg = re.computeAccentuateScores(doc, bodyPrefs, scores) if userprefs["gender"] == "Female" else re.computeAccentuateScoresForMen(doc, bodyPrefs, scores_men) hideScore = re.computeHideScores(doc, bodyPrefs, hideScores) if userprefs["gender"] == "Female" else 0 colorScore, colorMsg = re.computeColorScores(doc, colors) skinToneScore, skinToneMsg = re.computeSkinToneScores(doc,skinTone,colors) #print accentuateScore, hideScore, colorScore, skinToneScore doc['score'] = accentuateWeight*(accentuateScore + hideScore) + colorWeight*colorScore + skinToneWeight*skinToneScore doc['msg'] = me.getMsg(accentuateMsg + colorMsg + skinToneMsg) #print doc if doc['score'] not in grouped_data.keys(): grouped_data[doc['score']] = { ls.Category.Topwear.value : [], ls.Category.Bottomwear.value : [], ls.Category.Footwear.value : [] } del doc['_id'] grouped_data[doc['score']][doc['Category']].append(doc) scored_data.append(doc) #json.dump(grouped_data, open("grouped.txt",'w')) sorted_keys = grouped_data.keys() sorted_keys.sort(reverse=True) feed = createFeed(sorted_keys, grouped_data) #print feed images = {} i=0 for product in feed: images[i]={"image" :product["Image"], "msg" : product["msg"], "url" : product["Url"], "domain": product["Domain"], "score": product["score"], "Color": product["Color"], "Sub_category": product["Sub_category"], "Product Name": product["Product Name"], "Cost": product["Cost"], "Brand": product["Brand"], "Category": product["Category"], "Test": "" } if "Neck" in product.keys(): images[i]["Neck"] = product["Neck"], if "Sleeves" in product.keys(): images[i]["Sleeves"] = product["Sleeves"] if "Pattern" in product.keys(): images[i]["Pattern"] = product["Pattern"] i = i+1 return {"result": json.dumps(images)}