存档

2009年11月 的存档

HTML解析模块完成

2009年11月15日

1.根据DTD文件构造HTML树
2.遍历出网页中所有肉眼能看到的东西
3.找出DIV块文本、找出TABLE块文本
4.根据DTD中的实体定义,将HTML转义符转成原有的字符

下一步计划,完成分词模块

大杂烩

HtmlReader模块完成

2009年11月4日

通过2组引擎数组,完成对html网页的粗糙预处理

  1. final String[] blankFilter = {" \t", " \t", "\n", " \t\r\n", "<", " \t"};

意思:如果已经扫描到空格或者tab,那么后面不要出现空格或tab了
“\n”, ” \t\r\n” 同义
“<", " \t" 同义

  1. final String[] tagFilters = {"<!–", "–>", "<script", "</script>", "<style", "</style>", "<!DOCTYPE", ">"};

意思,删除script开头,script结尾的所有内容

  1. private Reader reader;
  2.     public void setReader(Reader reader){this.reader = new BufferedReader(reader);}
  3.  
  4.     char[] buf = null;
  5.     int bufEnd = 0;
  6.    
  7.     /* 
  8.      *  将reader中的内容存入500K的缓存中,如果不够采取加倍策略
  9.      *  read all content from reader into buf and update the bufEnd,
  10.      *  finally will close the reader */
  11.     boolean readyData() throws IOException, NotSetReaderException
  12.     {
  13.         if(reader == null)
  14.         {
  15.             throw new NotSetReaderException();
  16.         }
  17.        
  18.         /* for most page 500K is enough */
  19.         int MOST_PAGE_SIZE = 500 * 1024;
  20.        
  21.         /* do not bigger than 5M */
  22.         int MAX_PAGE_SIZE = 5 * 1024 * 1024;
  23.        
  24.         buf = new char[MOST_PAGE_SIZE];
  25.        
  26.         try {
  27.             int actualRead;
  28.             while((actualRead = reader.read(buf, bufEnd, MOST_PAGE_SIZE-bufEnd)) != -1)
  29.             {
  30.                 /* oh, fill all space, so there are something remain in stream */
  31.                 if(actualRead == MOST_PAGE_SIZEbufEnd)
  32.                 {
  33.                     /* update the bufEnd */
  34.                     bufEnd += actualRead;
  35.                    
  36.                     /* it’s enough */
  37.                     if(bufEnd >= MAX_PAGE_SIZE)
  38.                         break;
  39.                    
  40.                     /* assign bigger array */
  41.                     char[] tempBuf = new char[MOST_PAGE_SIZE * 2];
  42.                     MOST_PAGE_SIZE = MOST_PAGE_SIZE * 2;
  43.                     System.arraycopy(buf, 0, tempBuf, 0, bufEnd);
  44.                     buf = tempBuf;
  45.                 }
  46.                 /* nothing to read */
  47.                 else
  48.                 {
  49.                     /* update the bufEnd */
  50.                     bufEnd += actualRead;
  51.                 }
  52.             }
  53.  
  54.         } catch (IOException e) {
  55.             System.out.println("God said: something wrong when read the page in initialize");
  56.             throw e;
  57.         } finally {
  58.             reader.close();
  59.         }
  60.        
  61.         /* test for the buff
  62.         FileWriter writer = new FileWriter("11");
  63.         writer.write(buf,0, bufEnd);
  64.         writer.close();
  65.         */
  66.        
  67.         return true;
  68.     }
  69.    
  70.    
  71.     void removeUselessContent()
  72.     {
  73.         int saved = 0, reading = 0;
  74.        
  75.         /* 前者为条件(空格tab),后者为删除对象(空格tab) */
  76.         final String[] blankFilter = {" \t", " \t", "\n", " \t\r\n", "<", " \t"};
  77.        
  78.         /* 准备删除的无用标签,提供首末信息 */
  79.         final String[] tagFilters = {"<!–", "–>", "<script", "</script>", "<style", "</style>", "<!DOCTYPE", ">"};
  80.        
  81.         /* 无回溯高效扫描整个文档 */
  82.         while(true)
  83.         {
  84.             if(reading >= bufEnd)
  85.                 break;
  86.  
  87.             /* 一直扫描到没有过滤为止 */
  88.             while(true)
  89.             {
  90.                 /* 监控是否有符合的过滤,只有当没有什么可以过滤了,
  91.                  * 才退出循环,标志为reading是否有读 */
  92.                 int checkFilter = reading;
  93.                
  94.                 /* 过滤篇首的所有空格 */
  95.                 while(saved == 0 && reading < bufEnd &&
  96.                         (buf[reading] ==   || buf[reading] == \t ||
  97.                                 buf[reading] == \r || buf[reading] == \n))
  98.                 {
  99.                     reading++;
  100.                 }
  101.  
  102.                 reading = removeUselessBlank(blankFilter, reading, saved);           
  103.                
  104.                 reading = removeHtmlTag(tagFilters, reading);
  105.                
  106.                 /* 如果有reading有遇到过滤的条件,让其继续扫描过滤 */
  107.                 if(checkFilter == reading)
  108.                     break;
  109.             }
  110.            
  111.             if(reading < bufEnd)
  112.                 buf[saved++] = buf[reading++];
  113.  
  114.         }
  115.        
  116.         bufEnd = saved;
  117.        
  118.         ///* test for the buff
  119.         try{
  120.         FileWriter writer = new FileWriter("11");
  121.         writer.write(buf,0, bufEnd);
  122.         writer.close();
  123.         //*/
  124.         }catch(IOException e){}
  125.     }
  126.  
  127.  
  128.     private int removeUselessBlank(String[] filterString, int reading, int saved) 
  129.     {
  130.         char[] condition, forbidance;
  131.         for(int i = 0; i < filterString.length;)
  132.         {
  133.             condition = filterString[i++].toCharArray();
  134.             forbidance = filterString[i++].toCharArray();
  135.             while (saved1 >= 0 && equalAny(condition, buf[saved - 1])
  136.                     && equalAny(forbidance, buf[reading])) {
  137.                 reading++;
  138.             }
  139.         }
  140.         return reading;
  141.     }
  142.  
  143.     private boolean equalAny(char[] condition, char aim) {
  144.         boolean bool = false;
  145.         for (char c : condition) {
  146.             bool = bool || (c == aim);
  147.         }
  148.         return bool;
  149.     }
  150.  
  151.       private int removeHtmlTag(String[] filterString, int reading) {
  152.  
  153.         char[] styleBegin, styleEnd;
  154.         for (int i = 0; i < filterString.length;) {
  155.             styleBegin = filterString[i++].toCharArray();
  156.             styleEnd = filterString[i++].toCharArray();
  157.  
  158.             while (reading + styleBegin.length < bufEnd    && compareCharArray(buf, reading, styleBegin)) 
  159.             {
  160.                 reading = reading + styleBegin.length;
  161.                 while (reading + styleEnd.length < bufEnd && !compareCharArray(buf, reading, styleEnd)) 
  162.                 {
  163.                     reading++;
  164.                 }
  165.                 reading = reading + styleEnd.length;
  166.             }
  167.         }
  168.  
  169.         return reading;
  170.     }
  171.  
  172.     private boolean compareCharArray(char[] src, int from, char[] des) {
  173.         int i = 0;
  174.         while (i < des.length
  175.                 && (src[from] == des[i] || (src[from] + 32) == des[i])) {
  176.             from++;
  177.             i++;
  178.         }
  179.         return i == des.length;
  180.     }

大杂烩