【问题标题】:Keywords erroneous, extracting content from a website. OOP关键字错误,从网站中提取内容。面向对象
【发布时间】:2012-12-01 19:49:18
【问题描述】:

我从网站(wiki 文章)中提取关键字时遇到问题,提取的关键字并不完全是关键字,是从 html 中提取的词,而不是从网站中提取的词。

我使用以下代码:

include("Extkeys.php");
[...]
if (empty($keywords)){
$ekeywords = new KeyPer;
$keywords = $ekeywords->Keys($webhtml);
}

而“Extkeys”的代码是:

<?php
class Extkeys {
function Keys($webhtml) { 
$webhtml = $this->clean($webhtml); 
$blacklist='de,la,los,las,el,ella,nosotros,yo,tu,el,te,mi,del,ellos'; 
$sticklist='test'; 
$minlength = 3; 
$count = 17; 

$webhtml = preg_replace('/[\.;:|\'|\"|\`|\,|\(|\)|\-]/', ' ', $webhtml); 
$webhtml = preg_replace('/¡/', '', $webhtml); 
$webhtml = preg_replace('/¿/', '', $webhtml);

$keysArray = explode(" ", $webhtml); 
$keysArray = array_count_values(array_map('strtolower', $keysArray)); 
$blackArray = explode(",", $blacklist); 

foreach($blackArray as $blackWord){ 
if(isset($keysArray[trim($blackWord)])) 
unset($keysArray[trim($blackWord)]); 
} 
arsort($keysArray); 
$i = 1; 
$keywords = ""; 
foreach($keysArray as $word => $instances){ 
if($i > $count) break; 
if(strlen(trim($word)) >= $minlength && is_string($word)) { 
$keywords .= $word . ", "; 
$i++; 
} 
} 

$keywords = rtrim($keywords, ", "); 

return $keywords=$sticklist.''.$keywords; 
} 

function clean($webhtml) { 

$regex = '/(([_A-Za-z0-9-]+)(\\.[_A-Za-z0-9-]+)*@([A-Za-z0-9-]+)(\\.[A-Za-z0-9-]+)*)/iex'; 
$desc = preg_replace($regex, '', $webhtml); 
$webhtml = preg_replace( "''si", '', $webhtml ); 
$webhtml = preg_replace( '/]*>([^<]+)<\/a>/is', '\2 (\1)', $webhtml ); 
$webhtml = preg_replace( '//', '', $webhtml ); 
$webhtml = preg_replace( '/{.+?}/', '', $webhtml ); 
$webhtml = preg_replace( '/ /', ' ', $webhtml ); 
$webhtml = preg_replace( '/&/', ' ', $webhtml ); 
$webhtml = preg_replace( '/"/', ' ', $webhtml ); 
$webhtml = strip_tags( $webhtml ); 
$webhtml = htmlspecialchars($webhtml); 
$webhtml = str_replace(array("\r\n", "\r", "\n", "\t"), " ", $webhtml); 

while (strchr($webhtml," ")) { 
$webhtml = str_replace(" ", "",$webhtml); 
} 

for ($cnt = 1; 
$cnt < strlen($webhtml)-1; $cnt++) {
if (($webhtml{$cnt} == '.') || ($webhtml{$cnt} == ',')) { 
if ($webhtml{$cnt+1} != ' ') { 
$webhtml = substr_replace($webhtml, ' ', $cnt + 1, 0); 
} 
} 
} 
return $webhtml; 
} 
}
?>

这是提取的关键字示例:

testfalse, lang, {mw, loader, window, function, true, vector, user, gadget, mediawiki, legacy, options, usebetatoolbar, implement, resourceloader, default

文章内容: http://en.wikipedia.org/wiki/Searchengine

代码“Extkeys”,它是教程中代码的副本,适合我使其发挥作用。

我怎样才能使代码可以提取网站的关键字,而不是 html?

最好的问候!

【问题讨论】:

    标签: php database oop keyword


    【解决方案1】:

    假设我理解您的问题,我认为只需执行以下操作就是您正在寻找的解决方案。

    这将从 URL(例如http://www.whatever.com/page.html)读取 HTML 并使用它来生成密钥,而不是将 HTML 作为参数。

    function Keys($url) { 
        $webhtml = file_get_contents($url);
    

    【讨论】:

    • 感谢您的回复。我测试了代码,并在第一个代码中用“$url”替换了“$html”。但是像以前一样提取。如果我不替换,则只给出“测试”。最好的问候!
    • 您必须将上面的调用更改为$keywords = $ekeywords-&gt;Keys('http://en.wikipedia.org/wiki/Searchengine');
    • 您好,使用您所说的代码,但仍然显示相同的结果。我也将代码用于许多页面,所以我使用 $url。
    【解决方案2】:

    您想先从页面中提取内容,然后搜索关键字。这意味着您想从页面中找到实际内容并将内容剥离为侧边栏、页脚等。 只是 google 用于 HTML 内容提取,有很多关于此的文章。

    我在 java 中做过一次,有一个名为 boilerpipe 的库我不确定是否有 PHP 端口/接口,快速谷歌搜索没有发现任何内容。但我确信 PHP 也有类似的库。

    摆脱 HTML 而不是专门搜索页面内容的最简单方法是使用正则表达式去除所有 html,例如 s/&lt;[^&gt;]+&gt;//g。但是,对于搜索引擎来说,这可能不是最好的方法,因为您最终会遇到很多可能会弄乱您的密钥提取的废话。

    编辑:这是一篇关于content extraction with PHP的文章。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-14
      • 1970-01-01
      • 2016-02-18
      • 1970-01-01
      • 1970-01-01
      • 2011-03-14
      相关资源
      最近更新 更多