[15c7d39] | 1 | /*----------------------------------------------------------------------------
|
---|
| 2 | * JavaScript for webhelp search
|
---|
| 3 | *----------------------------------------------------------------------------
|
---|
| 4 | This file is part of the webhelpsearch plugin for DocBook WebHelp
|
---|
| 5 | Copyright (c) 2007-2008 NexWave Solutions All Rights Reserved.
|
---|
| 6 | www.nexwave.biz Nadege Quaine
|
---|
| 7 | http://kasunbg.blogspot.com/ Kasun Gajasinghe
|
---|
| 8 | */
|
---|
| 9 |
|
---|
| 10 | //string initialization
|
---|
| 11 | var htmlfileList = "htmlFileInfoList.js";
|
---|
| 12 | var htmlfileinfoList = "htmlFileInfoList.js";
|
---|
| 13 | var useCJKTokenizing = false;
|
---|
| 14 |
|
---|
| 15 | var w = new Object();
|
---|
| 16 | var scoring = new Object();
|
---|
| 17 |
|
---|
| 18 | var searchTextField = '';
|
---|
| 19 | var no = 0;
|
---|
| 20 | var noWords = 0;
|
---|
| 21 | var partialSearch = "<font class=\"highlightText\">There is no page containing all the search terms.<br>Partial results:</font>";
|
---|
| 22 | var warningMsg = '<div style="padding: 5px;margin-right:5px;;background-color:#FFFF00;">';
|
---|
| 23 | warningMsg+='<b>Please note that due to security settings, Google Chrome does not highlight';
|
---|
| 24 | warningMsg+=' the search results in the right frame.</b><br>';
|
---|
| 25 | warningMsg+='This happens only when the WebHelp files are loaded from the local file system.<br>';
|
---|
| 26 | warningMsg+='Workarounds:';
|
---|
| 27 | warningMsg+='<ul>';
|
---|
| 28 | warningMsg+='<li>Try using another web browser.</li>';
|
---|
| 29 | warningMsg+='<li>Deploy the WebHelp files on a web server.</li>';
|
---|
| 30 | warningMsg+='</div>';
|
---|
| 31 | txt_filesfound = 'Results';
|
---|
| 32 | txt_enter_at_least_1_char = "You must enter at least one character.";
|
---|
| 33 | txt_enter_more_than_10_words = "Only first 10 words will be processed.";
|
---|
| 34 | txt_browser_not_supported = "Your browser is not supported. Use of Mozilla Firefox is recommended.";
|
---|
| 35 | txt_please_wait = "Please wait. Search in progress...";
|
---|
| 36 | txt_results_for = "Results for: ";
|
---|
| 37 |
|
---|
| 38 | /* This function verify the validity of search input by the user
|
---|
| 39 | Cette fonction verifie la validite de la recherche entrre par l utilisateur */
|
---|
| 40 | function Verifie(searchForm) {
|
---|
| 41 |
|
---|
| 42 | // Check browser compatibility
|
---|
| 43 | if (navigator.userAgent.indexOf("Konquerer") > -1) {
|
---|
| 44 |
|
---|
| 45 | alert(txt_browser_not_supported);
|
---|
| 46 | return;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | searchTextField = trim(document.searchForm.textToSearch.value);
|
---|
| 50 | searchTextField = searchTextField.replace(/['"]/g,'');
|
---|
| 51 | var expressionInput = searchTextField;
|
---|
| 52 | $.cookie('textToSearch', expressionInput);
|
---|
| 53 |
|
---|
| 54 | if (expressionInput.length < 1) {
|
---|
| 55 |
|
---|
| 56 | // expression is invalid
|
---|
| 57 | alert(txt_enter_at_least_1_char);
|
---|
| 58 | // reactive la fenetre de search (utile car cadres)
|
---|
| 59 |
|
---|
| 60 | document.searchForm.textToSearch.focus();
|
---|
| 61 | }
|
---|
| 62 | else {
|
---|
| 63 | var splitSpace = searchTextField.split(" ");
|
---|
| 64 | var splitWords = [];
|
---|
| 65 | for (var i = 0 ; i < splitSpace.length ; i++) {
|
---|
| 66 | var splitDot = splitSpace[i].split(".");
|
---|
| 67 |
|
---|
| 68 | if(!(splitDot.length == 1)){
|
---|
| 69 | splitWords.push(splitSpace[i]);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | for (var i1 = 0; i1 < splitDot.length; i1++) {
|
---|
| 73 | var splitColon = splitDot[i1].split(":");
|
---|
| 74 | for (var i2 = 0; i2 < splitColon.length; i2++) {
|
---|
| 75 | var splitDash = splitColon[i2].split("-");
|
---|
| 76 | for (var i3 = 0; i3 < splitDash.length; i3++) {
|
---|
| 77 | if (splitDash[i3].split("").length > 0) {
|
---|
| 78 | splitWords.push(splitDash[i3]);
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 | noWords = splitWords;
|
---|
| 85 | if (noWords.length > 9){
|
---|
| 86 | // Allow to search maximum 10 words
|
---|
| 87 | alert(txt_enter_more_than_10_words);
|
---|
| 88 | expressionInput = '';
|
---|
| 89 | for (var x = 0 ; x < 10 ; x++){
|
---|
| 90 | expressionInput = expressionInput + " " + noWords[x];
|
---|
| 91 | }
|
---|
| 92 | Effectuer_recherche(expressionInput);
|
---|
| 93 | document.searchForm.textToSearch.focus();
|
---|
| 94 | } else {
|
---|
| 95 | // Effectuer la recherche
|
---|
| 96 | expressionInput = '';
|
---|
| 97 | for (var x = 0 ; x < noWords.length ; x++) {
|
---|
| 98 | expressionInput = expressionInput + " " + noWords[x];
|
---|
| 99 | }
|
---|
| 100 | Effectuer_recherche(expressionInput);
|
---|
| 101 | // reactive la fenetre de search (utile car cadres)
|
---|
| 102 | document.searchForm.textToSearch.focus();
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | var stemQueryMap = new Array(); // A hashtable which maps stems to query words
|
---|
| 108 |
|
---|
| 109 | /* This function parses the search expression, loads the indices and displays the results*/
|
---|
| 110 | function Effectuer_recherche(expressionInput) {
|
---|
| 111 |
|
---|
| 112 | /* Display a waiting message */
|
---|
| 113 | //DisplayWaitingMessage();
|
---|
| 114 |
|
---|
| 115 | /*data initialisation*/
|
---|
| 116 | var searchFor = ""; // expression en lowercase et sans les caracte res speciaux
|
---|
| 117 | //w = new Object(); // hashtable, key=word, value = list of the index of the html files
|
---|
| 118 | scriptLetterTab = new Scriptfirstchar(); // Array containing the first letter of each word to look for
|
---|
| 119 | var wordsList = new Array(); // Array with the words to look for
|
---|
| 120 | var finalWordsList = new Array(); // Array with the words to look for after removing spaces
|
---|
| 121 | var linkTab = new Array();
|
---|
| 122 | var fileAndWordList = new Array();
|
---|
| 123 | var txt_wordsnotfound = "";
|
---|
| 124 |
|
---|
| 125 |
|
---|
| 126 | // --------------------------------------
|
---|
| 127 | // Begin Thu's patch
|
---|
| 128 | /*nqu: expressionInput, la recherche est lower cased, plus remplacement des char speciaux*/
|
---|
| 129 | //The original replacement expression is:
|
---|
| 130 | //searchFor = expressionInput.toLowerCase().replace(/<\//g, "_st_").replace(/\$_/g, "_di_").replace(/\.|%2C|%3B|%21|%3A|@|\/|\*/g, " ").replace(/(%20)+/g, " ").replace(/_st_/g, "</").replace(/_di_/g, "%24_");
|
---|
| 131 | //The above expression was error prone because it did not deal with words that have a . as part of the word correctly, for example, document.txt
|
---|
| 132 |
|
---|
| 133 | //Do not automatically replace a . with a space
|
---|
| 134 | searchFor = expressionInput.toLowerCase().replace(/<\//g, "_st_").replace(/\$_/g, "_di_").replace(/%2C|%3B|%21|%3A|@|\/|\*/g, " ").replace(/(%20)+/g, " ").replace(/_st_/g, "</").replace(/_di_/g, "%24_");
|
---|
| 135 |
|
---|
| 136 | //If it ends with a period, replace it with a space
|
---|
| 137 | searchFor = searchFor.replace(/[.]$/,"");
|
---|
| 138 | // End Thu's Patch
|
---|
| 139 | // ------------------------------------------
|
---|
| 140 |
|
---|
| 141 | searchFor = searchFor.replace(/ +/g, " ");
|
---|
| 142 | searchFor = searchFor.replace(/ $/, "").replace(/^ /, "");
|
---|
| 143 |
|
---|
| 144 | wordsList = searchFor.split(" ");
|
---|
| 145 | wordsList.sort();
|
---|
| 146 |
|
---|
| 147 | //set the tokenizing method
|
---|
| 148 | useCJKTokenizing = typeof indexerLanguage != "undefined" && (indexerLanguage == "zh" || indexerLanguage == "ja" || indexerLanguage == "ko");
|
---|
| 149 | //If Lucene CJKTokenizer was used as the indexer, then useCJKTokenizing will be true. Else, do normal tokenizing.
|
---|
| 150 | // 2-gram tokenizinghappens in CJKTokenizing,
|
---|
| 151 | //If doStem then make tokenize with Stemmer
|
---|
| 152 | var finalArray;
|
---|
| 153 | if (doStem){
|
---|
| 154 | if(useCJKTokenizing){
|
---|
| 155 | finalWordsList = cjkTokenize(wordsList);
|
---|
| 156 | finalArray = finalWordsList;
|
---|
| 157 | } else {
|
---|
| 158 | finalWordsList = tokenize(wordsList);
|
---|
| 159 | finalArray = finalWordsList;
|
---|
| 160 | }
|
---|
| 161 | } else if(useCJKTokenizing){
|
---|
| 162 | finalWordsList = cjkTokenize(wordsList);
|
---|
| 163 | finalArray = finalWordsList;
|
---|
| 164 | } else{
|
---|
| 165 |
|
---|
| 166 | //load the scripts with the indices: the following lines do not work on the server. To be corrected
|
---|
| 167 | /*if (IEBrowser) {
|
---|
| 168 | scriptsarray = loadTheIndexScripts (scriptLetterTab);
|
---|
| 169 | } */
|
---|
| 170 |
|
---|
| 171 | /**
|
---|
| 172 | * Compare with the indexed words (in the w[] array), and push words that are in it to tempTab.
|
---|
| 173 | */
|
---|
| 174 | var tempTab = new Array();
|
---|
| 175 |
|
---|
| 176 | // ---------------------------------------
|
---|
| 177 | // Thu's patch
|
---|
| 178 | //Do not use associative array in for loop, for example:
|
---|
| 179 | //for(var t in finalWordsList)
|
---|
| 180 | //it causes errors when finalWordList contains
|
---|
| 181 | //stemmed words such as: kei from the stemmed word: key
|
---|
| 182 | for(var t=0;t<finalWordsList.length;++t){
|
---|
| 183 | var aWord=finalWordsList[t];
|
---|
| 184 | //w is a Map like Object, use the current word in finalWordList as the key
|
---|
| 185 | if(w[aWord] == undefined){
|
---|
| 186 | txt_wordsnotfound += aWord + " ";
|
---|
| 187 | }
|
---|
| 188 | else{
|
---|
| 189 | tempTab.push(aWord);
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
| 192 | finalWordsList = tempTab;
|
---|
| 193 | //Check all the inputs to see if the root words are in the finalWordsList, if not add them there
|
---|
| 194 | var inputs = expressionInput.split(' ');
|
---|
| 195 | // Thu's Patch
|
---|
| 196 | // -------------------------------------------
|
---|
| 197 |
|
---|
| 198 |
|
---|
| 199 | txt_wordsnotfound = expressionInput;
|
---|
| 200 | finalWordsList = removeDuplicate(finalWordsList);
|
---|
| 201 |
|
---|
| 202 | }
|
---|
| 203 | if (finalWordsList.length) {
|
---|
| 204 | //search 'and' and 'or' one time
|
---|
| 205 | fileAndWordList = SortResults(finalWordsList);
|
---|
| 206 |
|
---|
| 207 | if (fileAndWordList == undefined){
|
---|
| 208 | var cpt = 0;
|
---|
| 209 | } else {
|
---|
| 210 | var cpt = fileAndWordList.length;
|
---|
| 211 | var maxNumberOfWords = fileAndWordList[0][0].motsnb;
|
---|
| 212 | }
|
---|
| 213 | if (cpt > 0){
|
---|
| 214 | var searchedWords = noWords.length;
|
---|
| 215 | var foundedWords = fileAndWordList[0][0].motslisteDisplay.split(",").length;
|
---|
| 216 | //console.info("search : " + noWords.length + " found : " + fileAndWordList[0][0].motslisteDisplay.split(",").length);
|
---|
| 217 | if (searchedWords != foundedWords){
|
---|
| 218 | linkTab.push(partialSearch);
|
---|
| 219 | }
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 |
|
---|
| 223 | for (var i = 0; i < cpt; i++) {
|
---|
| 224 |
|
---|
| 225 | var hundredProcent = fileAndWordList[i][0].scoring + 100 * fileAndWordList[i][0].motsnb;
|
---|
| 226 | var ttScore_first = fileAndWordList[i][0].scoring;
|
---|
| 227 | var numberOfWords = fileAndWordList[i][0].motsnb;
|
---|
| 228 |
|
---|
| 229 | if (fileAndWordList[i] != undefined) {
|
---|
| 230 | linkTab.push("<p>" + txt_results_for + " " + "<span class=\"searchExpression\">" + fileAndWordList[i][0].motslisteDisplay + "</span>" + "</p>");
|
---|
| 231 |
|
---|
| 232 | linkTab.push("<ul class='searchresult'>");
|
---|
| 233 | for (t in fileAndWordList[i]) {
|
---|
| 234 | //linkTab.push("<li><a href=\"../"+fl[fileAndWordList[i][t].filenb]+"\">"+fl[fileAndWordList[i][t].filenb]+"</a></li>");
|
---|
| 235 |
|
---|
| 236 | var ttInfo = fileAndWordList[i][t].filenb;
|
---|
| 237 | // Get scoring
|
---|
| 238 | var ttScore = fileAndWordList[i][t].scoring;
|
---|
| 239 | var tempInfo = fil[ttInfo];
|
---|
| 240 |
|
---|
| 241 | var pos1 = tempInfo.indexOf("@@@");
|
---|
| 242 | var pos2 = tempInfo.lastIndexOf("@@@");
|
---|
| 243 | var tempPath = tempInfo.substring(0, pos1);
|
---|
| 244 | var tempTitle = tempInfo.substring(pos1 + 3, pos2);
|
---|
| 245 | var tempShortdesc = tempInfo.substring(pos2 + 3, tempInfo.length);
|
---|
| 246 |
|
---|
| 247 |
|
---|
| 248 | // toc.html will not be displayed on search result
|
---|
| 249 | if (tempPath == 'toc.html'){
|
---|
| 250 | continue;
|
---|
| 251 | }
|
---|
| 252 | /*
|
---|
| 253 | //file:///home/kasun/docbook/WEBHELP/webhelp-draft-output-format-idea/src/main/resources/web/webhelp/installation.html
|
---|
| 254 | var linkString = "<li><a href=" + tempPath + ">" + tempTitle + "</a>";
|
---|
| 255 | // var linkString = "<li><a href=\"installation.html\">" + tempTitle + "</a>";
|
---|
| 256 | */
|
---|
| 257 | var split = fileAndWordList[i][t].motsliste.split(",");
|
---|
| 258 | // var splitedValues = expressionInput.split(" ");
|
---|
| 259 | // var finalArray = split.concat(splitedValues);
|
---|
| 260 |
|
---|
| 261 | arrayString = 'Array(';
|
---|
| 262 | for(var x in finalArray){
|
---|
| 263 | if (finalArray[x].length > 2 || useCJKTokenizing){
|
---|
| 264 | arrayString+= "'" + finalArray[x] + "',";
|
---|
| 265 | }
|
---|
| 266 | }
|
---|
| 267 | arrayString = arrayString.substring(0,arrayString.length - 1) + ")";
|
---|
| 268 | var idLink = 'foundLink' + no;
|
---|
| 269 | var linkString = '<li><a id="' + idLink + '" href="' + tempPath + '" class="foundResult">' + tempTitle + '</a>';
|
---|
| 270 | var starWidth = (ttScore * 100/ hundredProcent)/(ttScore_first/hundredProcent) * (numberOfWords/maxNumberOfWords);
|
---|
| 271 | starWidth = starWidth < 10 ? (starWidth + 5) : starWidth;
|
---|
| 272 | // Keep the 5 stars format
|
---|
| 273 | if (starWidth > 85){
|
---|
| 274 | starWidth = 85;
|
---|
| 275 | }
|
---|
| 276 | /*
|
---|
| 277 | var noFullStars = Math.ceil(starWidth/17);
|
---|
| 278 | var fullStar = "curr";
|
---|
| 279 | var emptyStar = "";
|
---|
| 280 | if (starWidth % 17 == 0){
|
---|
| 281 | // am stea plina
|
---|
| 282 |
|
---|
| 283 | } else {
|
---|
| 284 |
|
---|
| 285 | }
|
---|
| 286 | console.info(noFullStars);
|
---|
| 287 | */
|
---|
| 288 | // Also check if we have a valid description
|
---|
| 289 | if ((tempShortdesc != "null" && tempShortdesc != '...')) {
|
---|
| 290 |
|
---|
| 291 | linkString += "\n<div class=\"shortdesclink\">" + tempShortdesc + "</div>";
|
---|
| 292 | }
|
---|
| 293 | linkString += "</li>";
|
---|
| 294 |
|
---|
| 295 | // Add rating values for scoring at the list of matches
|
---|
| 296 | linkString += "<div id=\"rightDiv\">";
|
---|
| 297 | linkString += "<div id=\"star\">";
|
---|
| 298 | //linkString += "<div style=\"color: rgb(136, 136, 136);\" id=\"starUser0\" class=\"user\">"
|
---|
| 299 | // + ((ttScore * 100/ hundredProcent)/(ttScore_first/hundredProcent)) * 1 + "</div>";
|
---|
| 300 | linkString += "<ul id=\"star0\" class=\"star\">";
|
---|
| 301 | linkString += "<li id=\"starCur0\" class=\"curr\" style=\"width: " + starWidth + "px;\"></li>";
|
---|
| 302 | linkString += "</ul>";
|
---|
| 303 |
|
---|
| 304 | linkString += "<br style=\"clear: both;\">";
|
---|
| 305 | linkString += "</div>";
|
---|
| 306 | linkString += "</div>";
|
---|
| 307 | //linkString += '<b>Rating: ' + ttScore + '</b>';
|
---|
| 308 |
|
---|
| 309 | linkTab.push(linkString);
|
---|
| 310 | no++;
|
---|
| 311 | }
|
---|
| 312 | linkTab.push("</ul>");
|
---|
| 313 | }
|
---|
| 314 | }
|
---|
| 315 | }
|
---|
| 316 |
|
---|
| 317 | var results = "";
|
---|
| 318 | if (linkTab.length > 0) {
|
---|
| 319 | /*writeln ("<p>" + txt_results_for + " " + "<span class=\"searchExpression\">" + cleanwordsList + "</span>" + "<br/>"+"</p>");*/
|
---|
| 320 | results = "<p>";
|
---|
| 321 | //write("<ul class='searchresult'>");
|
---|
| 322 | for (t in linkTab) {
|
---|
| 323 | results += linkTab[t].toString();
|
---|
| 324 | }
|
---|
| 325 | results += "</p>";
|
---|
| 326 | } else {
|
---|
| 327 | results = "<p>" + localeresource.search_no_results + " <span class=\"searchExpression\">" + txt_wordsnotfound + "</span>" + "</p>";
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 |
|
---|
| 331 | // Verify if the browser is Google Chrome and the WebHelp is used on a local machine
|
---|
| 332 | // If browser is Google Chrome and WebHelp is used on a local machine a warning message will appear
|
---|
| 333 | // Highlighting will not work in this conditions. There is 2 workarounds
|
---|
| 334 | if (verifyBrowser()){
|
---|
| 335 | document.getElementById('searchResults').innerHTML = results;
|
---|
| 336 | } else {
|
---|
| 337 | document.getElementById('searchResults').innerHTML = warningMsg + results;
|
---|
| 338 | }
|
---|
| 339 |
|
---|
| 340 | }
|
---|
| 341 |
|
---|
| 342 |
|
---|
| 343 | // Verify if the stemmed word is aproximately the same as the searched word
|
---|
| 344 | function verifyWord(word, arr){
|
---|
| 345 | for (var i = 0 ; i < arr.length ; i++){
|
---|
| 346 | if (word[0] == arr[i][0]
|
---|
| 347 | && word[1] == arr[i][1]
|
---|
| 348 | //&& word[2] == arr[i][2]
|
---|
| 349 | ){
|
---|
| 350 | return true;
|
---|
| 351 | }
|
---|
| 352 | }
|
---|
| 353 | return false;
|
---|
| 354 | }
|
---|
| 355 |
|
---|
| 356 | // Look for elements that start with searchedValue.
|
---|
| 357 | function wordsStartsWith(searchedValue){
|
---|
| 358 | var toReturn = '';
|
---|
| 359 | for (var sv in w){
|
---|
| 360 | if (searchedValue.length < 3){
|
---|
| 361 | continue;
|
---|
| 362 | } else {
|
---|
| 363 | if (sv.toLowerCase().indexOf(searchedValue.toLowerCase()) == 0){
|
---|
| 364 | toReturn+=sv + ",";
|
---|
| 365 | }
|
---|
| 366 | }
|
---|
| 367 | }
|
---|
| 368 | return toReturn.length > 0 ? toReturn : undefined;
|
---|
| 369 | }
|
---|
| 370 |
|
---|
| 371 |
|
---|
| 372 | function tokenize(wordsList){
|
---|
| 373 | var stemmedWordsList = new Array(); // Array with the words to look for after removing spaces
|
---|
| 374 | var cleanwordsList = new Array(); // Array with the words to look for
|
---|
| 375 | // -------------------------------------------------
|
---|
| 376 | // Thu's patch
|
---|
| 377 | for(var j=0;j<wordsList.length;++j){
|
---|
| 378 | var word = wordsList[j];
|
---|
| 379 | var originalWord=word;
|
---|
| 380 | if(typeof stemmer != "undefined" ){
|
---|
| 381 | var stemmedWord=stemmer(word);
|
---|
| 382 | if(w[stemmedWord]!=undefined){
|
---|
| 383 | stemQueryMap[stemmer(word)] = word;
|
---|
| 384 | }
|
---|
| 385 | else{
|
---|
| 386 | stemQueryMap[originalWord]=originalWord;
|
---|
| 387 | }
|
---|
| 388 | } else {
|
---|
| 389 | if(w[word]!=undefined){
|
---|
| 390 | stemQueryMap[word] = word;
|
---|
| 391 | }
|
---|
| 392 | else{
|
---|
| 393 | stemQueryMap[originalWord]=originalWord;
|
---|
| 394 | }
|
---|
| 395 | }
|
---|
| 396 | }
|
---|
| 397 | //stemmedWordsList is the stemmed list of words separated by spaces.
|
---|
| 398 | for (var t=0;t<wordsList.length;++t) {
|
---|
| 399 | wordsList[t] = wordsList[t].replace(/(%22)|^-/g, "");
|
---|
| 400 | if (wordsList[t] != "%20") {
|
---|
| 401 | scriptLetterTab.add(wordsList[t].charAt(0));
|
---|
| 402 | cleanwordsList.push(wordsList[t]);
|
---|
| 403 | }
|
---|
| 404 | }
|
---|
| 405 |
|
---|
| 406 | if(typeof stemmer != "undefined" ){
|
---|
| 407 | //Do the stemming using Porter's stemming algorithm
|
---|
| 408 | for (var i = 0; i < cleanwordsList.length; i++) {
|
---|
| 409 | var stemWord = stemmer(cleanwordsList[i]);
|
---|
| 410 | if(w[stemWord]!=undefined){
|
---|
| 411 | stemmedWordsList.push(stemWord);
|
---|
| 412 | }
|
---|
| 413 | else{
|
---|
| 414 | stemmedWordsList.push(cleanwordsList[i]);
|
---|
| 415 | }
|
---|
| 416 | }
|
---|
| 417 | // End Thu's patch
|
---|
| 418 | // -------------------------------------------
|
---|
| 419 | } else {
|
---|
| 420 | stemmedWordsList = cleanwordsList;
|
---|
| 421 | }
|
---|
| 422 | return stemmedWordsList;
|
---|
| 423 | }
|
---|
| 424 |
|
---|
| 425 | //Invoker of CJKTokenizer class methods.
|
---|
| 426 | function cjkTokenize(wordsList){
|
---|
| 427 | var allTokens= new Array();
|
---|
| 428 | var notCJKTokens= new Array();
|
---|
| 429 | var j=0;
|
---|
| 430 | for(j=0;j<wordsList.length;j++){
|
---|
| 431 | var word = wordsList[j];
|
---|
| 432 | if(getAvgAsciiValue(word) < 127){
|
---|
| 433 | notCJKTokens.push(word);
|
---|
| 434 | } else {
|
---|
| 435 | var tokenizer = new CJKTokenizer(word);
|
---|
| 436 | var tokensTmp = tokenizer.getAllTokens();
|
---|
| 437 | allTokens = allTokens.concat(tokensTmp);
|
---|
| 438 | }
|
---|
| 439 | }
|
---|
| 440 | allTokens = allTokens.concat(tokenize(notCJKTokens));
|
---|
| 441 | return allTokens;
|
---|
| 442 | }
|
---|
| 443 |
|
---|
| 444 | //A simple way to determine whether the query is in english or not.
|
---|
| 445 | function getAvgAsciiValue(word){
|
---|
| 446 | var tmp = 0;
|
---|
| 447 | var num = word.length < 5 ? word.length:5;
|
---|
| 448 | for(var i=0;i<num;i++){
|
---|
| 449 | if(i==5) break;
|
---|
| 450 | tmp += word.charCodeAt(i);
|
---|
| 451 | }
|
---|
| 452 | return tmp/num;
|
---|
| 453 | }
|
---|
| 454 |
|
---|
| 455 | //CJKTokenizer
|
---|
| 456 | function CJKTokenizer(input){
|
---|
| 457 | this.input = input;
|
---|
| 458 | this.offset=-1;
|
---|
| 459 | this.tokens = new Array();
|
---|
| 460 | this.incrementToken = incrementToken;
|
---|
| 461 | this.tokenize = tokenize;
|
---|
| 462 | this.getAllTokens = getAllTokens;
|
---|
| 463 | this.unique = unique;
|
---|
| 464 |
|
---|
| 465 | function incrementToken(){
|
---|
| 466 | if(this.input.length - 2 <= this.offset){
|
---|
| 467 | // console.log("false "+offset);
|
---|
| 468 | return false;
|
---|
| 469 | }
|
---|
| 470 | else {
|
---|
| 471 | this.offset+=1;
|
---|
| 472 | return true;
|
---|
| 473 | }
|
---|
| 474 | }
|
---|
| 475 |
|
---|
| 476 | function tokenize(){
|
---|
| 477 | //document.getElementById("content").innerHTML += x.substring(offset,offset+2)+"<br>";
|
---|
| 478 | return this.input.substring(this.offset,this.offset+2);
|
---|
| 479 | }
|
---|
| 480 |
|
---|
| 481 | function getAllTokens(){
|
---|
| 482 | while(this.incrementToken()){
|
---|
| 483 | var tmp = this.tokenize();
|
---|
| 484 | this.tokens.push(tmp);
|
---|
| 485 | }
|
---|
| 486 | return this.unique(this.tokens);
|
---|
| 487 | // document.getElementById("content").innerHTML += tokens+" ";
|
---|
| 488 | // document.getElementById("content").innerHTML += "<br>dada"+sortedTokens+" ";
|
---|
| 489 | // console.log(tokens.length+"dsdsds");
|
---|
| 490 | /*for(i=0;i<tokens.length;i++){
|
---|
| 491 | console.log(tokens[i]);
|
---|
| 492 | var ss = tokens[i] == sortedTokens[i];
|
---|
| 493 |
|
---|
| 494 | // document.getElementById("content").innerHTML += "<br>dada"+un[i]+"- "+stems[i]+" "+ ss;
|
---|
| 495 | document.getElementById("content").innerHTML += "<br>"+sortedTokens[i];
|
---|
| 496 | }*/
|
---|
| 497 | }
|
---|
| 498 |
|
---|
| 499 | function unique(a)
|
---|
| 500 | {
|
---|
| 501 | var r = new Array();
|
---|
| 502 | o:for(var i = 0, n = a.length; i < n; i++)
|
---|
| 503 | {
|
---|
| 504 | for(var x = 0, y = r.length; x < y; x++)
|
---|
| 505 | {
|
---|
| 506 | if(r[x]==a[i]) continue o;
|
---|
| 507 | }
|
---|
| 508 | r[r.length] = a[i];
|
---|
| 509 | }
|
---|
| 510 | return r;
|
---|
| 511 | }
|
---|
| 512 | }
|
---|
| 513 |
|
---|
| 514 |
|
---|
| 515 | /* Scriptfirstchar: to gather the first letter of index js files to upload */
|
---|
| 516 | function Scriptfirstchar() {
|
---|
| 517 | this.strLetters = "";
|
---|
| 518 | this.add = addLettre;
|
---|
| 519 | }
|
---|
| 520 |
|
---|
| 521 | function addLettre(caract) {
|
---|
| 522 |
|
---|
| 523 | if (this.strLetters == 'undefined') {
|
---|
| 524 | this.strLetters = caract;
|
---|
| 525 | } else if (this.strLetters.indexOf(caract) < 0) {
|
---|
| 526 | this.strLetters += caract;
|
---|
| 527 | }
|
---|
| 528 |
|
---|
| 529 | return 0;
|
---|
| 530 | }
|
---|
| 531 | /* end of scriptfirstchar */
|
---|
| 532 |
|
---|
| 533 | /*main loader function*/
|
---|
| 534 | /*tab contains the first letters of each word looked for*/
|
---|
| 535 | function loadTheIndexScripts(tab) {
|
---|
| 536 |
|
---|
| 537 | //alert (tab.strLetters);
|
---|
| 538 | var scriptsarray = new Array();
|
---|
| 539 |
|
---|
| 540 | for (var i = 0; i < tab.strLetters.length; i++) {
|
---|
| 541 |
|
---|
| 542 | scriptsarray[i] = "..\/search" + "\/" + tab.strLetters.charAt(i) + ".js";
|
---|
| 543 | }
|
---|
| 544 | // add the list of html files
|
---|
| 545 | i++;
|
---|
| 546 | scriptsarray[i] = "..\/search" + "\/" + htmlfileList;
|
---|
| 547 |
|
---|
| 548 | //debug
|
---|
| 549 | for (var t in scriptsarray) {
|
---|
| 550 | //alert (scriptsarray[t]);
|
---|
| 551 | }
|
---|
| 552 |
|
---|
| 553 | tab = new ScriptLoader();
|
---|
| 554 | for (t in scriptsarray) {
|
---|
| 555 | tab.add(scriptsarray[t]);
|
---|
| 556 | }
|
---|
| 557 | tab.load();
|
---|
| 558 | //alert ("scripts loaded");
|
---|
| 559 | return (scriptsarray);
|
---|
| 560 | }
|
---|
| 561 |
|
---|
| 562 | /* ScriptLoader: to load the scripts and wait that it's finished */
|
---|
| 563 | function ScriptLoader() {
|
---|
| 564 | this.cpt = 0;
|
---|
| 565 | this.scriptTab = new Array();
|
---|
| 566 | this.add = addAScriptInTheList;
|
---|
| 567 | this.load = loadTheScripts;
|
---|
| 568 | this.onScriptLoaded = onScriptLoadedFunc;
|
---|
| 569 | }
|
---|
| 570 |
|
---|
| 571 | function addAScriptInTheList(scriptPath) {
|
---|
| 572 | this.scriptTab.push(scriptPath);
|
---|
| 573 | }
|
---|
| 574 |
|
---|
| 575 | function loadTheScripts() {
|
---|
| 576 | var script;
|
---|
| 577 | var head;
|
---|
| 578 |
|
---|
| 579 | head = document.getElementsByTagName('head').item(0);
|
---|
| 580 |
|
---|
| 581 | //script = document.createElement('script');
|
---|
| 582 |
|
---|
| 583 | for (var el in this.scriptTab) {
|
---|
| 584 | //alert (el+this.scriptTab[el]);
|
---|
| 585 | script = document.createElement('script');
|
---|
| 586 | script.src = this.scriptTab[el];
|
---|
| 587 | script.type = 'text/javascript';
|
---|
| 588 | script.defer = false;
|
---|
| 589 |
|
---|
| 590 | head.appendChild(script);
|
---|
| 591 | }
|
---|
| 592 |
|
---|
| 593 | }
|
---|
| 594 |
|
---|
| 595 | function onScriptLoadedFunc(e) {
|
---|
| 596 | e = e || window.event;
|
---|
| 597 | var target = e.target || e.srcElement;
|
---|
| 598 | var isComplete = true;
|
---|
| 599 | if (typeof target.readyState != undefined) {
|
---|
| 600 |
|
---|
| 601 | isComplete = (target.readyState == "complete" || target.readyState == "loaded");
|
---|
| 602 | }
|
---|
| 603 | if (isComplete) {
|
---|
| 604 | ScriptLoader.cpt++;
|
---|
| 605 | if (ScriptLoader.cpt == ScriptLoader.scripts.length) {
|
---|
| 606 | ScriptLoader.onLoadComplete();
|
---|
| 607 | }
|
---|
| 608 | }
|
---|
| 609 | }
|
---|
| 610 |
|
---|
| 611 | /*
|
---|
| 612 | function onLoadComplete() {
|
---|
| 613 | alert("loaded !!");
|
---|
| 614 | } */
|
---|
| 615 |
|
---|
| 616 | /* End of scriptloader functions */
|
---|
| 617 |
|
---|
| 618 | // Array.unique( strict ) - Remove duplicate values
|
---|
| 619 | function unique(tab) {
|
---|
| 620 | var a = new Array();
|
---|
| 621 | var i;
|
---|
| 622 | var l = tab.length;
|
---|
| 623 |
|
---|
| 624 | if (tab[0] != undefined) {
|
---|
| 625 | a[0] = tab[0];
|
---|
| 626 | }
|
---|
| 627 | else {
|
---|
| 628 | return -1;
|
---|
| 629 | }
|
---|
| 630 |
|
---|
| 631 | for (i = 1; i < l; i++) {
|
---|
| 632 | if (indexof(a, tab[i], 0) < 0) {
|
---|
| 633 | a.push(tab[i]);
|
---|
| 634 | }
|
---|
| 635 | }
|
---|
| 636 | return a;
|
---|
| 637 | }
|
---|
| 638 | function indexof(tab, element, begin) {
|
---|
| 639 | for (var i = begin; i < tab.length; i++) {
|
---|
| 640 | if (tab[i] == element) {
|
---|
| 641 | return i;
|
---|
| 642 | }
|
---|
| 643 | }
|
---|
| 644 | return -1;
|
---|
| 645 |
|
---|
| 646 | }
|
---|
| 647 | /* end of Array functions */
|
---|
| 648 |
|
---|
| 649 |
|
---|
| 650 | /*
|
---|
| 651 | Param: mots= list of words to look for.
|
---|
| 652 | This function creates an hashtable:
|
---|
| 653 | - The key is the index of a html file which contains a word to look for.
|
---|
| 654 | - The value is the list of all words contained in the html file.
|
---|
| 655 |
|
---|
| 656 | Return value: the hashtable fileAndWordList
|
---|
| 657 | */
|
---|
| 658 | function SortResults(mots) {
|
---|
| 659 |
|
---|
| 660 | var fileAndWordList = new Object();
|
---|
| 661 | if (mots.length == 0 || mots[0].length == 0) {
|
---|
| 662 | return null;
|
---|
| 663 | }
|
---|
| 664 |
|
---|
| 665 |
|
---|
| 666 | // In generated js file we add scoring at the end of the word
|
---|
| 667 | // Example word1*scoringForWord1,word2*scoringForWord2 and so on
|
---|
| 668 | // Split after * to obtain the right values
|
---|
| 669 | var scoringArr = Array();
|
---|
| 670 | for (var t in mots) {
|
---|
| 671 | // get the list of the indices of the files.
|
---|
| 672 | var listNumerosDesFicStr = w[mots[t].toString()];
|
---|
| 673 |
|
---|
| 674 | if (listNumerosDesFicStr != undefined) {
|
---|
| 675 |
|
---|
| 676 | //alert ("listNumerosDesFicStr "+listNumerosDesFicStr);
|
---|
| 677 | var tab = listNumerosDesFicStr.split(",");
|
---|
| 678 | //for each file (file's index):
|
---|
| 679 | for (var t2 in tab) {
|
---|
| 680 | var tmp = '';
|
---|
| 681 | var idx = '';
|
---|
| 682 | var temp = tab[t2].toString();
|
---|
| 683 | if (temp.indexOf('*') != -1) {
|
---|
| 684 | idx = temp.indexOf('*');
|
---|
| 685 | tmp = temp.substring(idx + 3, temp.length);
|
---|
| 686 | temp = temp.substring(0, idx);
|
---|
| 687 | }
|
---|
| 688 | scoringArr.push(tmp);
|
---|
| 689 | if (fileAndWordList[temp] == undefined) {
|
---|
| 690 | fileAndWordList[temp] = "" + mots[t];
|
---|
| 691 | } else {
|
---|
| 692 | fileAndWordList[temp] += "," + mots[t];
|
---|
| 693 | }
|
---|
| 694 | //console.info("fileAndWordList[" + temp + "]=" + fileAndWordList[temp] + " : " + tmp);
|
---|
| 695 | }
|
---|
| 696 |
|
---|
| 697 | }
|
---|
| 698 | }
|
---|
| 699 | var fileAndWordListValuesOnly = new Array();
|
---|
| 700 | // sort results according to values
|
---|
| 701 | var temptab = new Array();
|
---|
| 702 | finalObj = new Array();
|
---|
| 703 | for (t in fileAndWordList) {
|
---|
| 704 | finalObj.push(new newObj(t,fileAndWordList[t]));
|
---|
| 705 | }
|
---|
| 706 |
|
---|
| 707 | if ( finalObj.length == 0 ) { // None of the queried words are not in the index (stemmed or not)
|
---|
| 708 | return null;
|
---|
| 709 | }
|
---|
| 710 | finalObj = removeDerivates(finalObj);
|
---|
| 711 | for (t in finalObj) {
|
---|
| 712 | tab = finalObj[t].wordList.split(',');
|
---|
| 713 | var tempDisplay = new Array();
|
---|
| 714 | for (var x in tab) {
|
---|
| 715 | if(stemQueryMap[tab[x]] != undefined && doStem){
|
---|
| 716 | tempDisplay.push(stemQueryMap[tab[x]]); //get the original word from the stem word.
|
---|
| 717 | } else {
|
---|
| 718 | tempDisplay.push(tab[x]); //no stem is available. (probably a CJK language)
|
---|
| 719 | }
|
---|
| 720 | }
|
---|
| 721 | var tempDispString = tempDisplay.join(", ");
|
---|
| 722 | var index;
|
---|
| 723 | for (x in fileAndWordList) {
|
---|
| 724 | if (x === finalObj[t].filesNo) {
|
---|
| 725 | index = x;
|
---|
| 726 | break;
|
---|
| 727 | }
|
---|
| 728 | }
|
---|
| 729 | var scoring = findRating(fileAndWordList[index], index);
|
---|
| 730 | temptab.push(new resultPerFile(finalObj[t].filesNo, finalObj[t].wordList, tab.length, tempDispString, scoring));
|
---|
| 731 | fileAndWordListValuesOnly.push(finalObj[t].wordList);
|
---|
| 732 | }
|
---|
| 733 | fileAndWordListValuesOnly = unique(fileAndWordListValuesOnly);
|
---|
| 734 | fileAndWordListValuesOnly = fileAndWordListValuesOnly.sort(compare_nbMots);
|
---|
| 735 |
|
---|
| 736 | var listToOutput = new Array();
|
---|
| 737 | for (var fawlvoIdx in fileAndWordListValuesOnly) {
|
---|
| 738 | for (t in temptab) {
|
---|
| 739 | if (temptab[t].motsliste == fileAndWordListValuesOnly[fawlvoIdx]) {
|
---|
| 740 | if (listToOutput[fawlvoIdx] == undefined) {
|
---|
| 741 | listToOutput[fawlvoIdx] = new Array(temptab[t]);
|
---|
| 742 | } else {
|
---|
| 743 | listToOutput[fawlvoIdx].push(temptab[t]);
|
---|
| 744 | }
|
---|
| 745 | }
|
---|
| 746 | }
|
---|
| 747 | }
|
---|
| 748 | // Sort results by scoring, descending on the same group
|
---|
| 749 | for (var ltoIdx in listToOutput) {
|
---|
| 750 | listToOutput[ltoIdx].sort(function(a, b){
|
---|
| 751 | return b.scoring - a.scoring;
|
---|
| 752 | });
|
---|
| 753 | }
|
---|
| 754 | // If we have groups with same number of words,
|
---|
| 755 | // will sort groups by higher scoring of each group
|
---|
| 756 | for (var i = 0; i < listToOutput.length - 1; i++) {
|
---|
| 757 | for (var j = i + 1; j < listToOutput.length; j++) {
|
---|
| 758 | if (listToOutput[i][0].motsnb < listToOutput[j][0].motsnb
|
---|
| 759 | || (listToOutput[i][0].motsnb == listToOutput[j][0].motsnb
|
---|
| 760 | && listToOutput[i][0].scoring < listToOutput[j][0].scoring)
|
---|
| 761 | ) {
|
---|
| 762 | var x = listToOutput[i];
|
---|
| 763 | listToOutput[i] = listToOutput[j];
|
---|
| 764 | listToOutput[j] = x;
|
---|
| 765 | }
|
---|
| 766 | }
|
---|
| 767 | }
|
---|
| 768 |
|
---|
| 769 | return listToOutput;
|
---|
| 770 | }
|
---|
| 771 |
|
---|
| 772 | // Remove derivates words from the list of words
|
---|
| 773 | function removeDerivates(obj){
|
---|
| 774 | var toResultObject = new Array();
|
---|
| 775 | for (i in obj){
|
---|
| 776 | var filesNo = obj[i].filesNo;
|
---|
| 777 | var wordList = obj[i].wordList;
|
---|
| 778 | var wList = wordList.split(",");
|
---|
| 779 | var searchedWords = searchTextField.toLowerCase().split(" ");
|
---|
| 780 | for (var k = 0 ; k < searchedWords.length ; k++){
|
---|
| 781 | for (var j = 0 ; j < wList.length ; j++){
|
---|
| 782 | if (wList[j].startsWith(searchedWords[k])){
|
---|
| 783 | wList[j] = searchedWords[k];
|
---|
| 784 | }
|
---|
| 785 | }
|
---|
| 786 | }
|
---|
| 787 | wList = removeDuplicate(wList);
|
---|
| 788 | var recreateList = '';
|
---|
| 789 | for(var x in wList){
|
---|
| 790 | recreateList+=wList[x] + ",";
|
---|
| 791 | }
|
---|
| 792 | recreateList = recreateList.substr(0, recreateList.length - 1);
|
---|
| 793 | toResultObject.push(new newObj(filesNo, recreateList));
|
---|
| 794 | }
|
---|
| 795 | return toResultObject;
|
---|
| 796 | }
|
---|
| 797 |
|
---|
| 798 | function newObj(filesNo, wordList){
|
---|
| 799 | this.filesNo = filesNo;
|
---|
| 800 | this.wordList = wordList;
|
---|
| 801 | }
|
---|
| 802 |
|
---|
| 803 | // Add a new parameter. Scoring.
|
---|
| 804 | function resultPerFile(filenb, motsliste, motsnb, motslisteDisplay, scoring, group) {
|
---|
| 805 | //10 - spring,time - 2 - spring, time - 55 - 3
|
---|
| 806 | this.filenb = filenb;
|
---|
| 807 | this.motsliste = motsliste;
|
---|
| 808 | this.motsnb = motsnb;
|
---|
| 809 | this.motslisteDisplay= motslisteDisplay;
|
---|
| 810 |
|
---|
| 811 | this.scoring = scoring;
|
---|
| 812 |
|
---|
| 813 | }
|
---|
| 814 |
|
---|
| 815 |
|
---|
| 816 | function findRating(words, nr){
|
---|
| 817 | var sum = 0;
|
---|
| 818 | var xx = words.split(',');
|
---|
| 819 | for (jj = 0 ; jj < xx.length ; jj++){
|
---|
| 820 | var wrd = w[xx[jj]].split(',');
|
---|
| 821 | for (var ii = 0 ; ii < wrd.length ; ii++){
|
---|
| 822 | var wrdno = wrd[ii].split('*');
|
---|
| 823 | if (wrdno[0] == nr){
|
---|
| 824 | sum+=parseInt(wrdno[1]);
|
---|
| 825 | }
|
---|
| 826 | }
|
---|
| 827 | }
|
---|
| 828 | return sum;
|
---|
| 829 | }
|
---|
| 830 |
|
---|
| 831 | function compare_nbMots(s1, s2) {
|
---|
| 832 | var t1 = s1.split(',');
|
---|
| 833 | var t2 = s2.split(',');
|
---|
| 834 | //alert ("s1:"+t1.length + " " +t2.length)
|
---|
| 835 | if (t1.length == t2.length) {
|
---|
| 836 | return 0;
|
---|
| 837 | } else if (t1.length > t2.length) {
|
---|
| 838 | return 1;
|
---|
| 839 | } else {
|
---|
| 840 | return -1;
|
---|
| 841 | }
|
---|
| 842 | //return t1.length - t2.length);
|
---|
| 843 | }
|
---|
| 844 |
|
---|
| 845 | // return false if browser is Google Chrome and WebHelp is used on a local machine, not a web server
|
---|
| 846 | function verifyBrowser(){
|
---|
| 847 | var returnedValue = true;
|
---|
| 848 | var browser = BrowserDetect.browser;
|
---|
| 849 | var addressBar = window.location.href;
|
---|
| 850 | if (browser == 'Chrome' && addressBar.indexOf('file://') === 0){
|
---|
| 851 | returnedValue = false;
|
---|
| 852 | }
|
---|
| 853 |
|
---|
| 854 | return returnedValue;
|
---|
| 855 | }
|
---|
| 856 |
|
---|
| 857 | // Remove duplicate values from an array
|
---|
| 858 | function removeDuplicate(arr) {
|
---|
| 859 | var r = new Array();
|
---|
| 860 | o:for(var i = 0, n = arr.length; i < n; i++) {
|
---|
| 861 | for(var x = 0, y = r.length; x < y; x++) {
|
---|
| 862 | if(r[x]==arr[i]) continue o;
|
---|
| 863 | }
|
---|
| 864 | r[r.length] = arr[i];
|
---|
| 865 | }
|
---|
| 866 | return r;
|
---|
| 867 | }
|
---|
| 868 |
|
---|
| 869 | // Create startsWith method
|
---|
| 870 | String.prototype.startsWith = function(str) {
|
---|
| 871 | return (this.match("^"+str)==str);
|
---|
| 872 | }
|
---|
| 873 |
|
---|
| 874 | function trim(str, chars) {
|
---|
| 875 | return ltrim(rtrim(str, chars), chars);
|
---|
| 876 | }
|
---|
| 877 |
|
---|
| 878 | function ltrim(str, chars) {
|
---|
| 879 | chars = chars || "\\s";
|
---|
| 880 | return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
|
---|
| 881 | }
|
---|
| 882 |
|
---|
| 883 | function rtrim(str, chars) {
|
---|
| 884 | chars = chars || "\\s";
|
---|
| 885 | return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
|
---|
| 886 | }
|
---|