Last active
January 27, 2018 06:20
-
-
Save gbrian/0b52ed57fe0181ec3375b96c076f900c to your computer and use it in GitHub Desktop.
Adding a new image comparer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(SembAI = { | |
loadAI: function() { | |
var d = jQuery.Deferred(); | |
var oThis = this; | |
jQuery.getScript("https://sdk.clarifai.com/js/clarifai-latest.js", () => { | |
oThis.app = new Clarifai.App( | |
'xZqcIvLNv0f9wNzuVtA97ntA2CB8IHSVFzKc0hzv', | |
'KYkdTnFHmT6D4Yv8gk5gEPc__1fZ2nJItBrrI8hM'); | |
console.log("Clarifai loaded"); | |
d.resolve(window.SembAI); | |
}); | |
return d.promise(); | |
}, | |
loadHotels: function() { | |
var d = jQuery.Deferred(); | |
var oThis = this; | |
var shown = jQuery(".search-result"); | |
var names = shown.map((i,e) => $(e).find(".hotel-details h2").text()).toArray(); | |
console.log(names); | |
var mis = (is) => is.map(i => ({url:"https://images.sembo.travel/ImageService/ImageHandler.ashx?service="+ | |
i.ImageService+"&nameOfImage="+ | |
encodeURIComponent(i.ImageName)+"&resizeMode=FitOutside&width=500&height=440&formatSettings=jpeg(quality-25)"})); | |
var mhs = (hs) => hs.filter(h => names.indexOf(h.Name) !== -1) | |
.map(h => { | |
var res = $(shown.get(names.indexOf(h.Name))); | |
return {el: res, | |
name: h.Name, | |
img: res.find(".hotel-image img"), | |
details: res.find(".hotel-details"), | |
imgs: mis(h.Images) | |
}; | |
}); | |
$.getJSON(window.location.origin + "/d/mvc/Hotels/GetHotels?apiVersion=11&c=16&SearchText=" + jQuery("#to_location").val(), | |
(res) => { | |
console.log("Hotels loaded"); | |
d.resolve(oThis.hotels = mhs(res.Hotels)); | |
}); | |
return d.promise(); | |
}, | |
predictReqs: [], | |
predict: function(url) { | |
var d = jQuery.Deferred(); | |
if(this.options.test){ | |
const ttags = "Beach,Outdoor,Kids Pool,Spa,Bed,Room,Bathroom".split(",") | |
.map(t => ({value:parseFloat((Math.random()+"").substring(0,3)),name:t})); | |
const rndIndex = () => Math.floor(Math.random() * (ttags.length - 1)) + 1; | |
var rndTags = () => { | |
var i = Math.min(rndIndex(ttags),0); | |
return ttags.slice(i,Math.max(i + rndIndex(ttags), 1)); | |
}; | |
d.resolve(rndTags()); | |
}else{ | |
// {"status":{"code":11005,"description":"Making too many requests","details":"exceeded limit of 10 requests per second"}} | |
this.predictReqs.push({d:d, url:url}); | |
this.notifyQueue(); | |
} | |
return d.promise(); | |
}, | |
interval: null, | |
notifyQueue: function(){ | |
if(this.interval) | |
return; | |
var oThis = this; | |
this.interval = window.setInterval(() => { | |
if(oThis.predictReqs.length == 0){ | |
window.clearInterval(oThis.interval); | |
oThis.interval = null; | |
return; | |
} | |
var rq = oThis.predictReqs.pop(); | |
var url = rq.url; | |
var d = rq.d; | |
oThis.app.models.predict(Clarifai.TRAVEL_MODEL, url) | |
.then((r) => d.resolve(r.outputs["0"].data.concepts | |
.map(c => ({name: c.name, value: c.value}))) | |
,() => d.resolve([])); | |
}, 250); | |
}, | |
tagHotels: function(){ | |
var oThis = this; | |
var promises = oThis.hotels.map(h => h.imgs.map(i => oThis.predict(i.url).then(tags => i.tags = tags))).reduce((a,b)=>a.concat(b)); | |
return $.when.apply($, promises); | |
}, | |
match: function(tag, score){ | |
var match = this.hotels.map(h => { h.match = h.imgs.filter(i => i.tags.find && i.tags.find(t => t.name == tag && (!score || t.value >= score))); return h;}) | |
.filter(h => h.match.length !== 0) | |
.map(h => h.img.attr("src", h.match[0].url)); | |
this.showTags(score); | |
return match; | |
}, | |
tags: function(){ | |
var res = {}; | |
this.hotels.map(h => h.imgs.map(i => i.tags).reduce((a,b) => a.concat(b))).reduce((a,b)=>a.concat(b)) | |
.map(t => res[t.name] = (res.hasOwnProperty(t.name) ? res[t.name]+1:0)); | |
return res; | |
}, | |
compareImage: function(i, ii){ | |
const ire = /(http.*\.(jpg|png|gif|bmp))/i; | |
var ires = ire.exec(i); | |
return ires && ires[0] === (ire.exec(ii)||[])[0]; | |
}, | |
showTags: function(score){ | |
jQuery('.aitag').remove(); | |
var ci = this.compareImage; | |
this.hotels.map(h => { | |
var curimg = h.imgs.filter(i => ci(i.url, h.img.attr("src")))[0]; | |
curimg && curimg.tags.filter(t => !score || t.value >= score).map(t => h.details.prepend("<span class='aitag' title='"+t.value+"'>"+t.name+"</span>")); | |
}); | |
}, | |
init: function(options){ | |
var oThis = this; | |
oThis.options = options; | |
$.when(oThis.loadAI(), oThis.loadHotels()) | |
.then(() => oThis.tagHotels()) | |
.then(() => oThis.showTags()); | |
jQuery("head").append("<style>.aitag{margin-right: 3px;background-color: #30a1d9;color: white;padding: 3px;}</style>"); | |
}}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment