Created
April 8, 2014 20:08
-
-
Save josephcc/10183294 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # Place all the behaviors and hooks related to the matching controller here. | |
| # All this logic will automatically be available in application.js. | |
| # You can use CoffeeScript in this file: http://coffeescript.org/ | |
| # | |
| clips = null | |
| id2idx = null | |
| textToHtml = (text, keywords) -> | |
| out = '' | |
| for token in text | |
| term = clean_term(token) | |
| if keywords.indexOf(term) >= 0 | |
| out += '<span class="token keyword">' + token + '</span> ' | |
| else | |
| if keywords.length == 0 | |
| out += '<span class="token">' + token + '</span> ' | |
| else | |
| out += token + ' ' | |
| return out | |
| loadRandomSeeds = (targets) -> | |
| clusters = $('.cluster') | |
| seeds = [] | |
| for cluster in clusters | |
| id = $(cluster).attr('data-cluster-id') | |
| if id | |
| seeds.push id | |
| for cluster in targets | |
| $(cluster).children('.seed').html() | |
| id = $(cluster).attr('data-cluster-id') | |
| if id | |
| index = seeds.indexOf(id) | |
| seeds.splice(index, 1) | |
| $(cluster).removeAttr('data-cluster-id') | |
| for cluster in targets | |
| seed = Math.random() * clips.length | |
| seed = Math.round(seed) | |
| if seeds.indexOf(seed) >= 0 | |
| continue | |
| seeds.push seed | |
| clip = clips[seed] | |
| $(cluster).children('.seed').html(textToHtml(clip['text'], [])) | |
| $(cluster).attr('data-cluster-id', clip['id']) | |
| $(cluster).children('.list').html('') | |
| bindEvents() | |
| get_tf_score = (clip, terms) -> | |
| _score = 0 | |
| for term in terms | |
| score = clip['terms'][term] | |
| if score? | |
| score = score[0] | |
| else | |
| score = 0 | |
| _score += score | |
| score = _score / terms.length | |
| clean_term = (term) -> | |
| term = term.replace(/^[^\w]/gi, '').toLowerCase() | |
| term = term.replace(/[^\w]$/gi, '').toLowerCase() | |
| term = term.replace(/'s$/gi, '').toLowerCase() | |
| term = term.replace(/’s$/gi, '').toLowerCase() | |
| return term | |
| clean_terms = (terms) -> | |
| _terms = [] | |
| for term in terms | |
| _terms.push clean_term(term) | |
| terms = _terms | |
| similar_ranker = (clip_idx, terms, similarity, clips) -> | |
| _ranker = (x, y) -> | |
| # x-y # small -> large | |
| # y-x # large -> small | |
| tx = get_tf_score(clips[x], terms) | |
| ty = get_tf_score(clips[y], terms) | |
| if ty - tx != 0 | |
| return ty - tx | |
| else | |
| return similarity[y] - similarity[x] | |
| return _ranker | |
| bindEvents = () -> | |
| $('.add-clip').click (e) -> | |
| console.log 'add-clip clicked' | |
| e.preventDefault | |
| $(this).toggleClass 'glyphicon-plus' | |
| $(this).toggleClass 'glyphicon-ok' | |
| $('.token').click (e) -> | |
| console.log 'token clicked' | |
| e.preventDefault | |
| console.log $(this) | |
| hasClass = $(this).hasClass 'keyword' | |
| console.log 'pre hasclass: ' + hasClass | |
| $(this).toggleClass('keyword') | |
| console.log $(this) | |
| hasClass = $(this).hasClass 'keyword' | |
| console.log 'post hasclass: ' + hasClass | |
| keywords = $(this).parent().children('.token.keyword') | |
| console.log keywords | |
| cluster = $(this).parent().parent() | |
| if keywords.length == 0 | |
| $(cluster).children('.list').html('') | |
| return | |
| console.log 'continued' | |
| terms = [] | |
| for keyword in keywords | |
| terms.push $(keyword).html() | |
| terms = clean_terms(terms) | |
| id = cluster.attr('data-cluster-id') | |
| clip_idx = id2idx[id] | |
| clip = clips[clip_idx] | |
| idxs = for i in [0...clips.length] | |
| i | |
| idxs.sort(similar_ranker(clip_idx, terms, clip['similarity'], clips)) | |
| html = '' | |
| for idx in idxs.slice(0, 5) | |
| if idx == clip_idx | |
| continue | |
| similarity = clip['similarity'][idx] | |
| score = get_tf_score(clips[idx], terms) | |
| tfs = [] | |
| for term in terms | |
| tfs.push [term, get_tf_score(clips[idx], [term])] | |
| stats = 'score:' + score.toFixed(4) + ' sim:' + similarity.toFixed(4) + '<br>' | |
| for tf in tfs | |
| stats += '"' + tf[0] + '":' + tf[1].toFixed(2) + ' ' | |
| stats = '<div class="stats col-xs-11">' + stats + '</div>' | |
| stats = '<div class="row"><div class="col-xs-1 add-clip glyphicon glyphicon-plus"></div>' + stats + '</div>' | |
| _html = textToHtml(clips[idx]['text'], terms) | |
| html += '<div class="clip" data-cluster-id="' + clips[idx]['id'] + '">' + stats + _html + '</div><hr>' | |
| $(cluster).children('.list').html(html) | |
| bindEvents() | |
| jQuery -> | |
| if not jQuery('body').hasClass('v-option-manager') | |
| return | |
| jQuery.ajax( | |
| url: "/api/clusters/" + ka.current.question_id | |
| type: 'GET' | |
| dataType: 'json' | |
| ).done( (data) -> | |
| clips = data[0] | |
| id2idx = data[1] | |
| ) | |
| $('.start').click (e) -> | |
| console.log 'start clicked' | |
| e.preventDefault | |
| clusters = $('.cluster') | |
| loadRandomSeeds(clusters) | |
| $('.next').click (e) -> | |
| console.log 'next clicked' | |
| e.preventDefault | |
| target = $($(this).parent().parent()) | |
| loadRandomSeeds(target) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment