2016-10-21 3 views
3

웹 페이지의 내용에 대한 요약을 쓰려고합니다. 이를 위해서는 웹 페이지에서 관련없는 모든 텍스트와 데이터를 추출해야합니다.웹 페이지에서 주요 콘텐츠를 추출하는 방법은 무엇입니까?

보일러 파이프를 사용했지만 텍스트 추출이 좋지 않습니다. 결과는 관련없는 텍스트가 많이 나타나는 here입니다.

또한 머리말, 꼬리말, 외부 링크 등을 제거하여 관련없는 데이터를 스크랩하기 위해 JSoup을 사용해 보았습니다.하지만 다시 결과는 최대 점수가 아닙니다.

Document doc = Jsoup.connect("www.anyurl.com").get() 
    doc.head().remove(); 
    doc.getElementsByTag("header").remove(); 
    doc.getElementsByTag("footer").remove(); 
    doc.getElementsByTag("form").remove(); 
    doc.getElementsByTag("table").remove(); 
    doc.getElementsByTag("meta").remove(); 
    doc.getElementsByTag("img").remove(); 
    doc.getElementsByTag("a").remove(); 
    doc.getElementsByTag("br").remove(); 

    doc.getElementsByClass("tags").remove(); 
    doc.getElementsByClass("copyright").remove(); 
    doc.getElementsByClass("widget").remove(); 

    doc.select("div[class*=foot").remove(); 
    doc.select("div[class*=tag").remove(); 
    doc.select("div[class*=Loading").remove(); 
    doc.select("div[class*=Widget").remove(); 
    doc.select("div[class*=Head").remove(); 
    doc.select("div[class*=menu").remove(); 
    doc.select("p[class*=link").remove(); 

    Elements paragraphs = doc.select("p"); 
    Elements divs = doc.select("div"); 

    formattedOutput = paragraphs.text() + divs.text(); 

아무에게도이 작업을 수행하는 방법을 제안 할 수 있습니까? 보일러 파이프가 아닌 자바 라이브러리가 있습니까?

+0

에서 주요 내용을 추출 사용할 수 있습니다, 나는 "관련이없는 많은 텍스트를"표시되지 않습니다. 나는 관련성이 있거나 그렇지 않은 것이 의견의 문제 일 수 있기 때문에 더 관련없는 텍스트를 제거하는 방법을 묻는 것으로 어디서나 쉽게 얻을 수 있다고 생각하지 않습니다. 대신, 당신이 원하는 것을 구체적으로 알려주십시오. 아니면 단순히 더 구체적인 아이디어가 필요하다는 해결책 일 수도 있습니다. – ajb

+1

** 1) ** 계속하기 전에 해당 [이용 약관] (http://www.medicalnewstoday.com/terms)을 살펴보십시오. '귀하는 귀하가 먼저 저희에게 연락하여 온라인 기사 전체를 게재 할 수 있다는 서면 허가를받지 않은 한, 귀하의 사이트에서 Medical News Today의 기사를 완성하십시오. ** 전체 이용 약관이 적용됩니다. ** 2) ** [newsfeeds] (http://www.medicalnewstoday.com/newsfeeds-rss)는 이미 찾고있는 정보를 제공 할 것입니다. – SubOptimal

답변

0

나는 자바에 대해하지 않습니다하지만 당신은 내가 링크에서 보이는 웹 페이지

<?php 

class ContentExtractor { 

    var $container_tags = array(
      'div', 'table', 'td', 'th', 'tr', 'tbody', 'thead', 'tfoot', 'col', 
      'colgroup', 'ul', 'ol', 'html', 'center', 'span' 
     ); 
    var $removed_tags = array(
      'script', 'noscript', 'style', 'form', 'meta', 'input', 'iframe', 'embed', 'hr', 'img', 
      '#comment', 'link', 'label' 
     ); 
    var $ignore_len_tags = array(
      'span' 
     ); 

    var $link_text_ratio = 0.04; 
    var $min_text_len = 20; 
    var $min_words = 0; 

    var $total_links = 0; 
    var $total_unlinked_words = 0; 
    var $total_unlinked_text=''; 
    var $text_blocks = 0; 

    var $tree = null; 
    var $unremoved=array(); 

    function sanitize_text($text){ 
     $text = str_ireplace('&nbsp;', ' ', $text); 
     $text = html_entity_decode($text, ENT_QUOTES); 

     $utf_spaces = array("\xC2\xA0", "\xE1\x9A\x80", "\xE2\x80\x83", 
      "\xE2\x80\x82", "\xE2\x80\x84", "\xE2\x80\xAF", "\xA0"); 
     $text = str_replace($utf_spaces, ' ', $text); 

     return trim($text); 
    } 

    function extract($text, $ratio = null, $min_len = null){ 
     $this->tree = new DOMDocument(); 

     $start = microtime(true); 
     if ([email protected]$this->tree->loadHTML($text)) return false; 

     $root = $this->tree->documentElement; 
     $start = microtime(true); 
     $this->HeuristicRemove($root, (($ratio == null) || ($min_len == null))); 

     if ($ratio == null) { 
      $this->total_unlinked_text = $this->sanitize_text($this->total_unlinked_text); 

      $words = preg_split('/[\s\r\n\t\|?!.,]+/', $this->total_unlinked_text); 
      $words = array_filter($words); 
      $this->total_unlinked_words = count($words); 
      unset($words); 
      if ($this->total_unlinked_words>0) { 
       $this->link_text_ratio = $this->total_links/$this->total_unlinked_words;// + 0.01; 
       $this->link_text_ratio *= 1.3; 
      } 

     } else { 
      $this->link_text_ratio = $ratio; 
     }; 

     if ($min_len == null) { 
      $this->min_text_len = strlen($this->total_unlinked_text)/$this->text_blocks; 
     } else { 
      $this->min_text_len = $min_len; 
     } 

     $start = microtime(true); 
     $this->ContainerRemove($root); 

     return $this->tree->saveHTML(); 
    } 

    function HeuristicRemove($node, $do_stats = false){ 
     if (in_array($node->nodeName, $this->removed_tags)){ 
      return true; 
     }; 

     if ($do_stats) { 
      if ($node->nodeName == 'a') { 
       $this->total_links++; 
      } 
      $found_text = false; 
     }; 

     $nodes_to_remove = array(); 

     if ($node->hasChildNodes()){ 
      foreach($node->childNodes as $child){ 
       if ($this->HeuristicRemove($child, $do_stats)) { 
        $nodes_to_remove[] = $child; 
       } else if ($do_stats && ($node->nodeName != 'a') && ($child->nodeName == '#text')) { 
        $this->total_unlinked_text .= $child->wholeText; 
        if (!$found_text){ 
         $this->text_blocks++; 
         $found_text=true; 
        } 
       }; 
      } 
      foreach ($nodes_to_remove as $child){ 
       $node->removeChild($child); 
      } 
     } 

     return false; 
    } 

    function ContainerRemove($node){ 
     if (is_null($node)) return 0; 
     $link_cnt = 0; 
     $word_cnt = 0; 
     $text_len = 0; 
     $delete = false; 
     $my_text = ''; 

     $ratio = 1; 

     $nodes_to_remove = array(); 
     if ($node->hasChildNodes()){ 
      foreach($node->childNodes as $child){ 
       $data = $this->ContainerRemove($child); 

       if ($data['delete']) { 
        $nodes_to_remove[]=$child; 
       } else { 
        $text_len += $data[2]; 
       } 

       $link_cnt += $data[0]; 

       if ($child->nodeName == 'a') { 
        $link_cnt++; 
       } else { 
        if ($child->nodeName == '#text') $my_text .= $child->wholeText; 
        $word_cnt += $data[1]; 
       } 
      } 

      foreach ($nodes_to_remove as $child){ 
       $node->removeChild($child); 
      } 

      $my_text = $this->sanitize_text($my_text); 

      $words = preg_split('/[\s\r\n\t\|?!.,\[\]]+/', $my_text); 
      $words = array_filter($words); 

      $word_cnt += count($words); 
      $text_len += strlen($my_text); 

     }; 

     if (in_array($node->nodeName, $this->container_tags)){ 
      if ($word_cnt>0) $ratio = $link_cnt/$word_cnt; 

      if ($ratio > $this->link_text_ratio){ 
        $delete = true; 
      } 

      if (!in_array($node->nodeName, $this->ignore_len_tags)) { 
       if (($text_len < $this->min_text_len) || ($word_cnt<$this->min_words)) { 
        $delete = true; 
       } 
      } 

     } 

     return array($link_cnt, $word_cnt, $text_len, 'delete' => $delete); 
    } 

} 

/**************************** 
    Simple usage example 
*****************************/ 

$html = file_get_contents('http://en.wikipedia.org/wiki/Shannon_index'); 

$extractor = new ContentExtractor(); 
$content = $extractor->extract($html); 
echo $content; 

?> 
관련 문제