1

ははは、まだキーワードの問題がありますが、これは私が作成しているコードです。

貧弱なコードですが、私の作成です:

<?php
$url = 'http://es.wikipedia.org/wiki/Animalia';
Keys($url);
function Keys($url) { 
$listanegra = array("a", "ante", "bajo", "con", "contra", "de", "desde", "mediante", "durante", "hasta", "hacia", "para", "por", "que", "qué", "cuán", "cuan", "los", "las", "una", "unos", "unas", "donde", "dónde", "como", "cómo", "cuando", "porque", "por", "para", "según", "sin", "tras", "con", "mas", "más", "pero", "del"); 

    $doc = new DOMDocument();
    libxml_use_internal_errors(true);
    $doc->loadHTMLFile($url);
    $webhtml = $doc->getElementsByTagName('p');
    $webhtml = $webhtml ->item(0)->nodeValue;

    $webhtml = strip_tags($webhtml);
    $webhtml = explode(" ", $webhtml);

    foreach($listanegra as $key=> $ln) {
    $webhtml = str_replace($ln, " ", $webhtml);
    }
    $palabras = str_word_count ("$webhtml", 1 ); 
    $frq = array_count_values ($palabras); 
    $frq = asort($frq);
    $ffrq = count($frq);
$i=1;
while ($i < $ffrq) {
    print $frqq[$i];
    print '<br />';
    $i++;
}
}
?>

ウェブサイトのキーワードを抽出しようとするコード。Webの最初の段落を抽出し、変数「$listanegra」の単語を削除します。次に、繰り返し単語を数え、すべての単語を「配列」に保存します。配列を呼び出した後、これで単語が表示されます。

問題は...機能しないコード=(。

コードを使用すると、空白が表示されます。

コードを完成させるのを手伝ってもらえますか?「tf-idf」の使用を勧めていましたが、後で使用します。

4

2 に答える 2

1

私はこれがあなたがやろうとしていたことだと信じています:

$url = 'http://es.wikipedia.org/wiki/Animalia';

$words = Keys($url);

/// do your database stuff with $words


function Keys($url)
{
    $listanegra = array('a', 'ante', 'bajo', 'con', 'contra', 'de', 'desde', 'mediante', 'durante', 'hasta', 'hacia', 'para', 'por', 'que', 'qué', 'cuán', 'cuan', 'los', 'las', 'una', 'unos', 'unas', 'donde', 'dónde', 'como', 'cómo', 'cuando', 'porque', 'por', 'para', 'según', 'sin', 'tras', 'con', 'mas', 'más', 'pero', 'del');

    $doc = new DOMDocument();
    libxml_use_internal_errors(true);
    $doc->loadHTMLFile($url);
    $webhtml = $doc->getElementsByTagName('p');
    $webhtml = $webhtml->item(0)->nodeValue;
    $webhtml = strip_tags($webhtml);
    $webhtml = explode(' ', $webhtml);

    $palabras = array();
    foreach($webhtml as $word)
    {
        $word = strtolower(trim($word, ' .,!?()')); // remove trailing special chars and spaces
        if (!in_array($word, $listanegra))
        {
            $palabras[] = $word;
        }
    }
    $frq = array_count_values($palabras);
    asort($frq);
    return implode(' ', array_keys($frq));
}
于 2012-12-02T17:41:30.053 に答える
0

テストしている場合、サーバーはエラーを表示するはずです:後にこれを追加してください

ini_set('display_errors', 1); 
ini_set('log_errors', 1); 
ini_set('error_log', dirname(__FILE__) . '/error_log.txt'); 
error_reporting(E_ALL);

そうすると、次のエラーが表示されます。24行目(新しい5行を入力しない場合は19行目)の配列から文字列への変換

ここにいくつかのエラーがあります。str_replace、str_word_count、asort、array_count_valuesの4つの関数が使用されていないことがわかりました。

str_replaceの使用は少し注意が必要です。を見つけて削除しようとすると、「動物」でもテキスト内のすべての「a」が削除されます。(str_replace( "a"、 "animal")=> nmal)このリンクは便利なはずですl:link

asortはtrueまたはfalseを返すので、次のようにします。

asort($frq);

値をアルファベット順に並べ替えます。$ frqは、 array_count_values- > $ frq = array($ word1 => word1_count、...)の結果を返します。ここでの値は、単語が使用された回数です。

 print $**frq**[$i]; // you have  print $frqq[$i]; in your code

この配列のインデックスは単語であり、値は単語がテキストに表示される回数であるため、結果は空になります。

また、str_word_countを使用する場合は、ヒスパニック系のテキストを読んでいるため、注意が必要です。テキストには、これを使用する必要のある数字を含めることができます。

str_word_count($string,1,'áéíóúüñ1234567890');

私が提案するコード:

<?php
header('Content-Type: text/html; charset=UTF-8');
ini_set('display_errors', 1); 
ini_set('log_errors', 1); 
ini_set('error_log', dirname(__FILE__) . '/error_log.txt'); 
error_reporting(E_ALL);



$url = 'http://es.wikipedia.org/wiki/Animalia';
Keys($url);
function Keys($url) { 
$listanegra = array("a", "ante", "bajo", "con", "contra", "de", "desde", "mediante", "durante", "hasta", "hacia", "para", "por", "que", "qué", "cuán", "cuan", "los", "las", "una", "unos", "unas", "donde", "dónde", "como", "cómo", "cuando", "porque", "por", "para", "según", "sin", "tras", "con", "mas", "más", "pero", "del"); 

$html=file_get_contents($url);

    $doc = new DOMDocument('1.0', 'UTF-8');
    $html = mb_convert_encoding($html, "HTML-ENTITIES", "UTF-8"); 
    libxml_use_internal_errors(true);
     $doc->loadHTML($html);
    $webhtml = $doc->getElementsByTagName('p');


    $webhtml = $webhtml ->item(0)->nodeValue;

    $webhtml = strip_tags($webhtml);
    print_r ($webhtml);
    $webhtml = explode(" ", $webhtml);


   // $webhtml = str_replace($listanegra, " ", $webhtml); str_replace() accepts array

    foreach($listanegra as $key=> $ln) {

    $webhtml = preg_replace('/\b'.$ln.'\b/u', ' ', $webhtml);
    }

    $palabras = str_word_count(implode(" ",$webhtml), 1, 'áéíóúüñ1234567890');

    sort($palabras);

    $frq = array_count_values ($palabras);


foreach($frq as $index=>$value) {
    print "the word <strong>$index</strong>  was used <strong>$value</strong> times";
    print '<br />';

}
}
?>

特別な文字の問題を理解しようとして本当に苦痛でした

于 2012-12-02T19:51:50.050 に答える