var concordiaUrl = 'http://localhost';
$(document).ready(function() {
$('#searchInput').bind("enterKey",function(e){
searchHandle();
});
$('#searchInput').keyup(function(e){
if(e.keyCode == 13) {
$(this).trigger("enterKey");
}
});
});
function searchHandle() {
var concordiaRequest = {
operation: 'concordiaSearch',
pattern:$("#searchInput").val()
}
$.ajax({
url: concordiaUrl,
type: 'post',
dataType: 'json',
success: function (data) {
$('#result').html(renderResult(data));
},
data: JSON.stringify(concordiaRequest)
});
}
function renderResult(data) {
var res = '';
var score = data['result']['bestOverlayScore']*100;
res += '
Concordia score: '+score.toFixed(0)+'%
';
var inputSentence = $('#searchInput').val();
var markedSentence = '';
var fragments = '';
lastInsertedEnd = 0;
for(var i = 0; i < data['result']['bestOverlay'].length; i++) {
var fragment = data['result']['bestOverlay'][i];
//previous unmarked fragment
markedSentence += inputSentence.slice(lastInsertedEnd, fragment['matchedPatternStart']);
//the marked fragment
markedSentence += ''+inputSentence.slice(fragment['matchedPatternStart'], fragment['matchedPatternEnd'])+'';
lastInsertedEnd = fragment['matchedPatternEnd'];
fragments += renderFragment(fragment, i);
}
//remaining unmarked fragment
markedSentence += inputSentence.slice(lastInsertedEnd);
res += ''+markedSentence+'
';
res += '
'+fragments;
return res;
}
function renderFragment(fragment, number) {
var result = '';
var sourceSegment = fragment['sourceSegment'];
result += sourceSegment.slice(0, fragment['matchedExampleStart']);
result += '';
result += sourceSegment.slice(fragment['matchedExampleStart'], fragment['matchedExampleEnd']);
result += '';
result += sourceSegment.slice(fragment['matchedExampleEnd']);
result += ' |
'+fragment['targetSegment']+' |
';
return result;
}
function displayDetails(caller, number) {
$('#result-sentence .matchedFragmentSelected').attr("class","matchedFragment");
caller.className='matchedFragmentSelected';
$('.fragmentDetails').css('display', 'none');
$('#fragment'+number).css('display', 'block');
}
function searchText(text) {
$("#searchInput").val(text);
searchHandle();
}
function showHideSuggestions() {
$('#suggestions').toggleClass('suggestionsInvisible');
}