2011-01-18 5 views
0

私は疑問に思っていました...私はウェブから記事をクロールするウェブページを持っているとしましょう。私が得るのはタイトルと平文の記事です。それらの間で記事を関連付けることができるPHPスクリプトまたはWebサービスがありますか?または...パラグラフからキーワードを生成できるPHPスクリプトはありますか?PHPの段落比較

...おかげで私は働くJAVAでスクリプトをテストしましたが、多分助けることができるPHPclassのどこかがあります!

+0

Javaコードに関する情報を入手すると便利です。また、どのような関係を探しているのかを明確にすることができますか?または、あなたは単にキーワードの密度を探していますか? – Gordon

+1

私はJAVAでコードを持っていない、私はちょうど一度それを長い前に使用したことを覚えていると、関係について私は同様にどのように別のテキストです数値のような意味。この場合、キーワード密度が本当に便利です。 –

答えて

1

this answerの機能を使用して、テキストから単語を抽出し、それらを互いに比較することができます。大まかな例:

// For better results grab the texts manually and paste them here. 
$nyt = file_get_contents('http://www.nytimes.com/2011/01/19/technology/19apple.html?pagewanted=print'); 
$sfc = file_get_contents('http://www.sfgate.com/cgi-bin/article.cgi?f=/c/a/2011/01/19/BUAK1HARUL.DTL&type=business'); 

$nyt = strip_tags($nyt); 
$sfc = strip_tags($sfc); 

// stopwords from english snowball porter stemmer 
$stopwordsFile = dirname(__FILE__).'/includes/stopwords_en.txt'; 
if (file_exists($stopwordsFile)) { 
    $stopwords = file($stopwordsFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); 
} else { 
    $stopwords = array(); 
} 

$nytWords = extractWords($nyt, 3, $stopwords); 
$sfcWords = extractWords($sfc, 3, $stopwords); 

$nyt2sfcCount = countKeywords($nytWords, $sfcWords, 4); 
$sfc2nytCount = countKeywords($sfcWords, $nytWords, 4); 

// absolute 
print_r($nyt2sfcCount); 
print_r($sfc2nytCount); 

$nyt2sfcFactor = strlen($sfc)/strlen($nyt); 
$sfc2nytFactor = strlen($nyt)/strlen($sfc); 

print($nyt2sfcFactor . PHP_EOL); 
print($sfc2nytFactor . PHP_EOL); 

foreach ($nyt2sfcCount as $word => $count) { 
    $nyt2sfcCountRel[$word] = $count * $nyt2sfcFactor; 
} 

foreach ($sfc2nytCount as $word => $count) { 
    $sfc2nytCountRel[$word] = $count * $sfc2nytFactor; 
} 

// relative 
print_r($nyt2sfcCountRel); 
print_r($sfc2nytCount); 
print_r($nyt2sfcCount); 
print_r($sfc2nytCountRel); 

// reduce 
$nyt2sfcCountRed = array_intersect_key($nyt2sfcCount, $sfc2nytCount); 
$sfc2nytCountRed = array_intersect_key($sfc2nytCount, $nyt2sfcCount); 

// reduced absolute 
print_r($nyt2sfcCountRed); 
print_r($sfc2nytCountRed); 

foreach ($nyt2sfcCountRed as $word => $count) { 
    $nyt2sfcCountRedRel[$word] = $count * $nyt2sfcFactor; 
} 

foreach ($sfc2nytCountRed as $word => $count) { 
    $sfc2nytCountRedRel[$word] = $count * $sfc2nytFactor; 
} 

// reduced relative 
print_r($nyt2sfcCountRedRel); 
print_r($sfc2nytCountRed); 
print_r($nyt2sfcCountRed); 
print_r($sfc2nytCountRedRel); 
+0

ありがとう、あなたの例から本当に便利です。 "the"、 "and"、 "for"などのような一般的な言葉を切るために辞書を使わなければならないとにかく、それはクールなスタートです。 –

+0

http://pastebin.com/raw.php?i=9AB2thTpにincludes/stopwords_en.txtを入れました。これはhttp://snowball.tartarus.org/algorithms/english/stemmer.htmlのコンパイル版です。他の言語のストップワードもあります。 – rik