Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. str0Highlights  = '';
  2. str1Highlights  = '';
  3.  
  4. function compare(s0, s1)
  5. {
  6.     str0 = s0.split(' ');
  7.     str1 = s1.split(' ');
  8.    
  9.     diff(str0, str1);
  10. }
  11.  
  12. function diff(s0, s1)
  13. {
  14.     str0Highlights  = '<span class="highlight">';
  15.     str1Highlights  = '<span class="highlight">';
  16.    
  17.     if((s0 == null)     ||
  18.        (s1 == null)     ||
  19.        (s0.length == 0) ||
  20.        (s1.length == 0))
  21.     {
  22.         str0Highlights  = str0Highlights + 'Error comparing documents: one or both documents not set';
  23.         str1Highlights  = str1Highlights + 'Error comparing documents: one or both documents not set';
  24.     }
  25.     else
  26.     {
  27.         if(((s0.length == 1) && (s0[0] == '')) ||
  28.            ((s1.length == 1) && (s1[0] == '')))
  29.         {
  30.             str0Highlights  = str0Highlights + 'Error comparing documents: one or both documents were BLANK';
  31.             str1Highlights  = str1Highlights + 'Error comparing documents: one or both documents were BLANK';
  32.         }
  33.         else
  34.         {
  35.             table       = [];
  36.             nullCell    = new Cell();
  37.            
  38.             //prepopulate the first row and first column with blank strings
  39.             for(col = 0; col <= s0.length; col++)
  40.             {
  41.                 table[col]  = [nullCell]
  42.             }
  43.            
  44.             for(row = 1; row <= s1.length; row++)
  45.             {
  46.                 table[0][row]   = nullCell;
  47.             }
  48.            
  49.            
  50.             //start finding the LCS
  51.             for(row = 1; row <= s1.length; row++)
  52.             {  
  53.                 for(col = 1; col <= s0.length; col++)
  54.                 {  
  55.                     //get the words
  56.                     w0  = s0[col - 1];
  57.                     w1  = s1[row - 1];
  58.                     c   = new Cell();
  59.                    
  60.                     if(w0 == w1)
  61.                     {  
  62.                         node    = new LCSNode((col - 1), (row - 1));
  63.                         upLeft  = table[col - 1][row - 1];  //cell
  64.                        
  65.                         if(upLeft.lcsList.length == 0)
  66.                         {
  67.                             //[col][row] should be a new array, with one lcs in it
  68.                             c.lcsList[0] = [node];
  69.                         }
  70.                         else
  71.                         {
  72.                             //copy the list of LCSs from [col-1][row-1], and add 'node' to it
  73.                             c.lcsList = upLeft.lcsList;
  74.                            
  75.                             for(lcs = 0; lcs < c.lcsList.length; lcs++)
  76.                             {
  77.                                 c.lcsList[lcs] = c.lcsList[lcs].concat([node]);
  78.                             }
  79.                         }
  80.                     }
  81.                     else
  82.                     {
  83.                         up  = table[col][row - 1];  //cell
  84.                         left    = table[col - 1][row];  //cell
  85.                        
  86.                         if((up.lcsList.length == 0) || (left.lcsList.length == 0))
  87.                         {
  88.                             if(up.lcsList.length != 0)
  89.                             {
  90.                                 c.lcsList = up.lcsList.concat([]);
  91.                             }
  92.                             else if(left.lcsList.length != 0)
  93.                             {
  94.                                 c.lcsList = left.lcsList.concat([]);
  95.                             }
  96.                         }
  97.                         else
  98.                         {
  99.                             if(up.lcsList[0].length > left.lcsList[0].length)
  100.                             {
  101.                                 c.lcsList = up.lcsList.concat([]);
  102.                             }
  103.                             else if(up.lcsList[0].length < left.lcsList[0].length)
  104.                             {
  105.                                 c.lcsList = left.lcsList.concat([]);
  106.                             }
  107.                             else
  108.                             {
  109.                                 c.lcsList = up.lcsList.concat(left.lcsList);
  110.                             }
  111.                         }
  112.                     }
  113.                    
  114.                     table[col][row] = c;
  115.                 }
  116.             }
  117.            
  118.             lastCell    = table[s0.length][s1.length];
  119.             chosenLCS   = null;
  120.            
  121.             if(lastCell.lcsList.length > 0)
  122.             {              
  123.                 minClusters = lastCell.lcsList[0].length;
  124.                
  125.                 for(lcs = 0; lcs < lastCell.lcsList.length; lcs++)
  126.                 {
  127.                     numClusters = 0;
  128.                    
  129.                     list = lastCell.lcsList[lcs];
  130.                    
  131.                     for(i = 1; i < list.length; i++)
  132.                     {
  133.                         if(list[i].str0Index != list[i - 1].str0Index + 1)
  134.                         {
  135.                             numClusters++;
  136.                         }
  137.                        
  138.                         if(list[i].str1Index != list[i - 1].str1Index + 1)
  139.                         {
  140.                             numClusters++;
  141.                         }
  142.                     }
  143.                    
  144.                     if(numClusters < minClusters)
  145.                     {
  146.                         chosenLCS = list;
  147.                     }
  148.                 }
  149.             }
  150.            
  151.             if(chosenLCS != null)
  152.             {
  153.                 for(node = 0; node < chosenLCS.length; node++)
  154.                 {
  155.                     //document.write('str0Index: ' + chosenLCS[node].str0Index);
  156.                     s0[chosenLCS[node].str0Index] = '</span>' + s0[chosenLCS[node].str0Index] + '<span class="highlight">';
  157.                     s1[chosenLCS[node].str1Index] = '</span>' + s1[chosenLCS[node].str1Index] + '<span class="highlight">';
  158.                 }
  159.                
  160.                 for(word = 0; word < s0.length; word++)
  161.                 {
  162.                     str0Highlights = str0Highlights + '</span> <span class="highlight">' + s0[word];
  163.                 }
  164.                
  165.                 for(word = 0; word < s1.length; word++)
  166.                 {
  167.                     str1Highlights = str1Highlights + '</span> <span class="highlight">' + s1[word];
  168.                 }
  169.             }
  170.             else
  171.             {
  172.                 str0Highlights = str0Highlights + 'Error comparing documents: error in main comparison';
  173.                 str1Highlights = str1Highlights + 'Error comparing documents: error in main comparison';
  174.             }
  175.         }
  176.     }
  177.    
  178.     str0Highlights = str0Highlights + '</span>';
  179.     str1Highlights = str1Highlights + '</span>';
  180. }
  181.  
  182.  
  183. function Cell()
  184. {
  185.     this.lcsList = [];
  186. }
  187.  
  188.  
  189. function LCSNode(s0i, s1i)
  190. {
  191.     this.str0Index = s0i;
  192.     this.str1Index = s1i;
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement