diff options
Diffstat (limited to 'themes/new_theme/layouts/partials/ranklist_strus.html')
-rw-r--r-- | themes/new_theme/layouts/partials/ranklist_strus.html | 316 |
1 files changed, 316 insertions, 0 deletions
diff --git a/themes/new_theme/layouts/partials/ranklist_strus.html b/themes/new_theme/layouts/partials/ranklist_strus.html new file mode 100644 index 0000000..9b732d1 --- /dev/null +++ b/themes/new_theme/layouts/partials/ranklist_strus.html @@ -0,0 +1,316 @@ +<script src="/js/jquery.js" type="text/javascript"></script> +<script src="/js/url-search-params.js" type="text/javascript"></script> +<script src="/js/Snowball.min.js" type="text/javascript"></script> +<script src="/js/loadingoverlay.min.js" type="text/javascript"></script> +<script language="Javascript"> + + var englishStemmer = new Snowball( "english" ); + + function stem( s ) { + englishStemmer.setCurrent( s ); + englishStemmer.stem( ); + return englishStemmer.getCurrent( ); + } + + function normalize_term( s ) { + var r = stem( s ); + return r.toLowerCase( ); + } + + function get_terms( s ) { + var terms = []; + var words = s.trim( ).split( ' ' ); + for( var i in words ) { + terms.push( + { + term: { + type: "word", + value: normalize_term( words[i] ) + } + } + ); + } + return terms; + } + + function get_features( query ) { + var features = []; + var words = query.trim( ).split( ' ' ); + for( var i in words ) { + features.push( + { + name: "feat", + value: { + term: { + type: "word", + value: normalize_term( words[i] ) + } + }, + weight: 1.0 + } + ); + } + features.push( + { + name: "sel", + value: { + expression : { + operator: "union", + range: 0, + cardinality: 0, + terms: get_terms( query ) + } + }, + weight: 1.0 + } + ) + return features; + } + + function draw_ranklist( query, data ) { + var html = ''; + + if( data.ranklist.ranks.length == 0 ) { + return "<p>No hits for query '" + query + "'...</p>"; + } else { + html += "<p>" + data.ranklist.documents_ranked + " hits found</p>"; + } + + var start = 1; + html += "<ol class=\"ranklist\" start=\"" + start + "\">"; + for( var i = 0; i < data.ranklist.ranks.length; i++ ) { + var rank = data.ranklist.ranks[i]; + var abstract = ''; + var title = ''; + var title_attribute = 'No title'; + var docid = ''; + html += "<li>"; + for( var j = 0; j < rank.attributes.length; j++ ) { + var attribute = rank.attributes[j]; + if( attribute.key == 'title' ) { + title += ' ' + attribute.value; + } else if( attribute.key == 'title_attribute' ) { + title_attribute = attribute.value; + } else if( attribute.key == 'docid' ) { + docid = attribute.value; + } else if( attribute.key == 'text' ) { + abstract += ' ' + escapeHtml( attribute.value ); + } + } + if( title == '' ) { + title = title_attribute; + } + title = title.replaceAll( "#SMARK#", "<i>" ).replaceAll( "#EMARK#", "</i>" ); + html += "<div class='title'>" + html += "<a href=\"" + docid + "\">" + title + "</a></div>"; + if( abstract != '' ) { + abstract = abstract.replaceAll( "#SMARK#", "<b>" ).replaceAll( "#EMARK#", "</b>" ); + html += "<div class='abstract'>" + abstract + "</div>"; + } + html += "</li>"; + } + html += "</ol>"; + return html; + } + + String.prototype.replaceAll = function(search, replacement) { + var target = this; + return target.replace(new RegExp(search, 'g'), replacement); + }; + + function escapeHtml( text ) { + return text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">" ); + } + + function executeQuery( query ) { + var request = { + query: { + first_rank: 0, + nof_ranks: 100, + features: get_features( query ), + select: [ + "sel" + ], + restrict: [ + ], + exclude: [ + ], + weighting: [ + { + name: "bm25", + params: [ + { + key : "match", + value : "feat" + } + ], + weight : 1.0 + } + ], + summarizer: [ + { + attribute: "docid", + name: "attribute", + params: [ + { + key : "name", + value : "docid" + } + ] + }, + { + attribute: "title_attribute", + name: "attribute", + params: [ + { + key : "name", + value : "title" + } + ], + resultnames: [ + { + key : "resultname", + value : "title_attribute" + } + ] + }, + { + attribute: "title", + name: "test", + params: [ + { + key: "type", + value: "title" + }, + { + key: "match", + value: "feat" + }, + { + key: "N", + value: 50 + }, + { + key: "add_dots", + value: 0 + }, + { + key: "start_first_match", + value: 0 + }, + { + key: "mark", + value: "#SMARK#%1%#EMARK#" + } + ], + resultnames: [ + { + key: "resultname", + value: "title" + } + ] + }, + { + attribute: "abstract", + name: "test", + params: [ + { + key: "type", + value: "text" + }, + { + key: "match", + value: "feat" + }, + { + key: "N", + value: 50 + }, + { + key: "add_dots", + value: 1 + }, + //~ { + //~ key: "sentence", + //~ value: "sentence" + //~ }, + //~ { + //~ key: "nof_sentences", + //~ value: 3 + //~ }, + { + key: "start_first_match", + value: 1 + }, + { + key: "mark", + value: "#SMARK#%1%#EMARK#" + } + ], + resultnames: [ + { + key: "resultname", + value: "text" + } + ] + } + ] + } + }; + + var json = JSON.stringify( request, null, " " ); + console.log( "debug request: " + json ); + + $( "#query" ).LoadingOverlay( "show" ); + + $.ajax( { + type : 'POST', + url : {{ .Site.Params.strus_url }}, + data : json, + contentType : 'application/json', + cache : false, + timeout: 5000 + } ) + .error( function( x, t, m ) { + var html = ''; + if( x.status > 0 ) { + msg = x.responseText; + } else { + msg = t; + } + msg = escapeHtml( msg ); + html = "<pre><font color='red'>There was an error communicating to the strusWebService, reason: " + msg + "</font></pre>\n"; + $( '#ranklist' ).html( html ); + + $( "#query" ).LoadingOverlay( "hide", true ); + } ) + .success( function( data ) { + var json = JSON.stringify( data, null, " " ); + console.log( "debug answer: " + json ); + + if( data.result == 'ok' ) { + var html = draw_ranklist( query, data ); + $( '#ranklist' ).html( html ); + } else { + var json = JSON.stringify( data, null, " " ); + var html = "<p>The strusWebService returned the following error:</p><pre><font color='red'>" + json + "</font></pre>"; + $( '#ranklist' ).html( html ); + } + + $( "#query" ).LoadingOverlay( "hide" ); + } ); + }; + + $( window ).load( function( ) { + var paramsString = window.location.search; + var searchParams = new URLSearchParams( paramsString ); + if( searchParams.has( "q" ) ) { + executeQuery( searchParams.get( "q" ) ); + } else { + // TODO: come up with something here + } // Show full page LoadingOverlay + } ); + +</script> +<div id="ranklist"> +</div> |