5

私はtb_sentenceテーブルを持っています:

=========================================================================
| id_row | document_id | sentence_id |          sentence_content        |
=========================================================================
|   1    |     1       |    0        |  Introduction to Data Mining.    |
|   2    |     1       |    1        |  Describe how data mining.       |
|   3    |     2       |    0        |  The boss is right.              |
=========================================================================

私は、文の内容をトークン化したいので、tb_tokensテーブルには以下が含まれます:

==========================================================================
| tokens_id | tokens_word  | tokens_freq | sentence_id  | document_id    |
==========================================================================
|     1     | Introduction |        1    |       0      |       1        |
|     2     | to           |        1    |       0      |       1        |
|     3     | Data         |        1    |       0      |       1        |
|     4     | Mining       |        1    |       0      |       1        |
|     5     | Describe     |        1    |       1      |       1        |
etc...

ここに私のコードがあります:

$sentence_clean = array();
$q1 = mysql_query("SELECT document_id FROM tb_sentence ORDER BY document_id ") or die(mysql_error());
while ($row1 = mysql_fetch_array($q1)) {
    $doc_id[] = $row1['document_id'];
}
$q2 = mysql_query('SELECT sentence_content, sentence_id, document_id FROM tb_sentence ') or die(mysql_error());
while ($row2 = mysql_fetch_array($q2)) {
    $sentence_clean[$row2['document_id']][] = $row2['sentence_content'];
}
foreach ($sentence_clean as $kal) {
    if (trim($kal) === '')
        continue;
    tokenizing($kal);
}

トークン化の機能は次のとおりです。

function tokenizing($sentence) {
    foreach ($sentence as $sentence_id => $sentences) {
        $symbol = array(".", ",", "\\", "-", "\"", "(", ")", "<", ">", "?", ";", ":", "+", "%", "\r", "\t", "\0", "\x0B");
        $spasi = array("\n", "/", "\r");
        $replace = str_replace($spasi, " ", $sentences);
        $cleanSymbol = str_replace($symbol, "", $replace);
        $quote = str_replace("'", "\'", $cleanSymbol);
        $element = explode(" ", trim($quote));
        $elementNCount = array_count_values($element);

        foreach ($elementNCount as $word => $freq) {
            if (ereg("([a-z,A-Z])", $word)) {
                $query = mysql_query(" INSERT INTO tb_tokens VALUES ('','$word','$freq','$sentence_id', '$doc_id')");
            }
        }
    }
}

問題は、document_id読み取りができず、tb+tokens テーブルに挿入できないことです。それらを呼び出す方法はdocument_id?ありがとうございました :)

編集された質問: すべての単語 (トークン化の結果) にはdocument_idとがありsentence_idます。私の問題は、を呼び出すことができないことdocument_idです。sentence_id両方とdocument_idすべての単語を呼び出す方法は?

4

1 に答える 1

1

これらのコードは必要ないと思います:

$q1 = mysql_query("SELECT document_id FROM tb_sentence ORDER BY document_id ") or die(mysql_error());
while ($row1 = mysql_fetch_array($q1)) {
    $doc_id[] = $row1['document_id'];
}

$doc_id の配列は使用されていません

if (trim($kal) === '')
        continue;

$kal は配列であり、トリミングする必要はありません

$sentence_clean[$row2['document_id']][] = $row2['sentence_content'];

文の ID を記録するので、[] ではなく $row2['sentence_id'] にする必要があります。

(もちろん、同じ document_id に同じ statement_id がないことを確認する必要があります。そうでない場合は、それを連結する必要があります)

これは私からのいくつかの修正です:

$sentence_clean = array();
$q2 = mysql_query('SELECT sentence_content, sentence_id, document_id FROM tb_sentence ') or die(mysql_error());
while ($row2 = mysql_fetch_array($q2)) {
    $sentence_clean[$row2['document_id']][$row2['sentence_id']] = $row2['sentence_content'];
}

foreach ($sentence_clean as $doc_id => $kal) {
    tokenizing($kal, $doc_id);
}

function tokenizing($sentence, $doc_id) {
    foreach ($sentence as $sentence_id => $sentences) {
        $symbol = array(".", ",", "\\", "-", "\"", "(", ")", "<", ">", "?", ";", ":", "+", "%", "\r", "\t", "\0", "\x0B");
        $spasi = array("\n", "/", "\r");
        $replace = str_replace($spasi, " ", $sentences);
        $cleanSymbol = str_replace($symbol, "", $replace);
        $quote = str_replace("'", "\'", $cleanSymbol);
        $element = explode(" ", trim($quote));
        $elementNCount = array_count_values($element);

        foreach ($elementNCount as $word => $freq) {
            if (ereg("([a-z,A-Z])", $word)) {
                $query = mysql_query(" INSERT INTO tb_tokens VALUES ('','$word','$freq','$sentence_id', '$doc_id')");
            }
        }
    }
}

document_id を関数に解析します

于 2012-08-02T13:58:09.547 に答える