Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 3rd, 2012  |  syntax: None  |  size: 2.88 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package com.danehansen
  2. {
  3.         public class Search
  4.         {
  5.                 private static const _PUNC:String = unescape("%22,./;'[]\`-=?><:|}{+_)(*&^%$#@!~‘’“”");
  6.                 private static const _SHIT_WORDS:Vector.<String> = new <String>["a", "an", "and", "but", "for", "is", "nor", "of", "or", "so", "the", "to", "yet"];
  7.                
  8.                 public function MySearch()
  9.                 {
  10.                         // constructor code
  11.                 }
  12.                
  13.                 public static function rank(searchIn:Vector.<String>, searchFor:String):Vector.<uint>
  14.                 {
  15.                         //makes formatted copy of searchIn
  16.                         var formated:Vector.<String>=new Vector.<String>(searchIn.length,true);
  17.                         for(var i:uint=0; i<searchIn.length; i++)
  18.                         {
  19.                                 formated[i]=_formatForSearch(searchIn[i]);
  20.                         }
  21.                        
  22.                         //formats searchFor
  23.                         searchFor=_formatForSearch(searchFor);
  24.                        
  25.                         //creates array of individual words in searchFor
  26.                         var searchForSplit:Array=searchFor.split(" ");
  27.                        
  28.                         //creates mutlidimensional array to keep "score"
  29.                         var scores:Array=new Array();
  30.                         for(i=0; i<searchIn.length; i++)
  31.                         {
  32.                                 scores.push(new Vector.<uint>());
  33.                                 for(var j:uint=0; j<searchForSplit.length; j++)
  34.                                 {
  35.                                         scores[i][j]=0;
  36.                                 }
  37.                         }
  38.                        
  39.                         //loops through individual and combinations of words from searchFor
  40.                         for(i=0; i<searchIn.length; i++)
  41.                         {
  42.                                 for(j=1; j<=searchForSplit.length; j++)
  43.                                 {
  44.                                         for(var k:uint=0; k<=searchForSplit.length-j; k++)
  45.                                         {
  46.                                                 scores[i][j-1]+=_numOfInstances(searchIn[i], searchForSplit.slice(k,k+j).join(" "));
  47.                                         }
  48.                                 }
  49.                         }
  50.  
  51.                         //creates list of ranks
  52.                         var ranks:Vector.<uint>=new Vector.<uint>();
  53.                         for(i=0; i<searchForSplit.length; i++)
  54.                         {
  55.                                 var values:Array=new Array();
  56.                                 for(j=0; j<searchIn.length; j++)
  57.                                 {
  58.                                         values.push(scores[j][i]);
  59.                                 }
  60.                                 values.sort(Array.NUMERIC);
  61.                                 values.reverse();
  62.                                 for(j=0; j<searchIn.length; j++)
  63.                                 {
  64.                                         for(k=0; k<searchIn.length; k++)
  65.                                         {
  66.                                                 if(scores[k][i]==values[j] && ranks.indexOf(k)==-1 && values[j]>0)
  67.                                                 {
  68.                                                         ranks.push(k);
  69.                                                 }
  70.                                         }
  71.                                 }
  72.                         }
  73.                         for(i=0; i<searchIn.length; i++)
  74.                         {
  75.                                 if(ranks.indexOf(i)==-1)
  76.                                 {
  77.                                         ranks.push(i);
  78.                                 }
  79.                         }
  80.                        
  81.                         return ranks;
  82.                 }
  83.                
  84.                 private static function _numOfInstances(searchIn:String, searchFor:String):uint
  85.                 {
  86.                         var frequency:uint=0;
  87.                         for(var i:int=searchIn.length-1; i>=0; i--)
  88.                         {
  89.                                 i=searchIn.lastIndexOf(searchFor,i);
  90.                                 if(i>-1 && _SHIT_WORDS.indexOf(searchFor)==-1)
  91.                                         frequency++;
  92.                         }
  93.                        
  94.                         return frequency;
  95.                 }
  96.                
  97.                 private static function _formatForSearch(str:String):String
  98.                 {
  99.                         for(var i:uint=0; i<_PUNC.length; i++)
  100.                         {
  101.                                 str=str.split(_PUNC.slice(i,i+1)).join("");
  102.                         }
  103.                         str=str.toLowerCase();
  104.                         while(str.indexOf("  ")>-1)
  105.                         {
  106.                                 str=str.split("  ").join(" ");
  107.                         }
  108.                         if(str.slice(0,1)==" ")
  109.                                 str=str.slice(1,str.length);
  110.                         if(str.slice(str.length-1,str.length)==" ")
  111.                                 str=str.slice(0,str.length-1)
  112.                        
  113.                         return str;
  114.                 }
  115.         }
  116. }