- package com.danehansen
- {
- public class Search
- {
- private static const _PUNC:String = unescape("%22,./;'[]\`-=?><:|}{+_)(*&^%$#@!~‘’“”");
- private static const _SHIT_WORDS:Vector.<String> = new <String>["a", "an", "and", "but", "for", "is", "nor", "of", "or", "so", "the", "to", "yet"];
- public function MySearch()
- {
- // constructor code
- }
- public static function rank(searchIn:Vector.<String>, searchFor:String):Vector.<uint>
- {
- //makes formatted copy of searchIn
- var formated:Vector.<String>=new Vector.<String>(searchIn.length,true);
- for(var i:uint=0; i<searchIn.length; i++)
- {
- formated[i]=_formatForSearch(searchIn[i]);
- }
- //formats searchFor
- searchFor=_formatForSearch(searchFor);
- //creates array of individual words in searchFor
- var searchForSplit:Array=searchFor.split(" ");
- //creates mutlidimensional array to keep "score"
- var scores:Array=new Array();
- for(i=0; i<searchIn.length; i++)
- {
- scores.push(new Vector.<uint>());
- for(var j:uint=0; j<searchForSplit.length; j++)
- {
- scores[i][j]=0;
- }
- }
- //loops through individual and combinations of words from searchFor
- for(i=0; i<searchIn.length; i++)
- {
- for(j=1; j<=searchForSplit.length; j++)
- {
- for(var k:uint=0; k<=searchForSplit.length-j; k++)
- {
- scores[i][j-1]+=_numOfInstances(searchIn[i], searchForSplit.slice(k,k+j).join(" "));
- }
- }
- }
- //creates list of ranks
- var ranks:Vector.<uint>=new Vector.<uint>();
- for(i=0; i<searchForSplit.length; i++)
- {
- var values:Array=new Array();
- for(j=0; j<searchIn.length; j++)
- {
- values.push(scores[j][i]);
- }
- values.sort(Array.NUMERIC);
- values.reverse();
- for(j=0; j<searchIn.length; j++)
- {
- for(k=0; k<searchIn.length; k++)
- {
- if(scores[k][i]==values[j] && ranks.indexOf(k)==-1 && values[j]>0)
- {
- ranks.push(k);
- }
- }
- }
- }
- for(i=0; i<searchIn.length; i++)
- {
- if(ranks.indexOf(i)==-1)
- {
- ranks.push(i);
- }
- }
- return ranks;
- }
- private static function _numOfInstances(searchIn:String, searchFor:String):uint
- {
- var frequency:uint=0;
- for(var i:int=searchIn.length-1; i>=0; i--)
- {
- i=searchIn.lastIndexOf(searchFor,i);
- if(i>-1 && _SHIT_WORDS.indexOf(searchFor)==-1)
- frequency++;
- }
- return frequency;
- }
- private static function _formatForSearch(str:String):String
- {
- for(var i:uint=0; i<_PUNC.length; i++)
- {
- str=str.split(_PUNC.slice(i,i+1)).join("");
- }
- str=str.toLowerCase();
- while(str.indexOf(" ")>-1)
- {
- str=str.split(" ").join(" ");
- }
- if(str.slice(0,1)==" ")
- str=str.slice(1,str.length);
- if(str.slice(str.length-1,str.length)==" ")
- str=str.slice(0,str.length-1)
- return str;
- }
- }
- }