2012-08-02 17 views
5

Ho tb_sentence tavolo:non può memorizzare i document_id

========================================================================= 
| 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.    | 
========================================================================= 

Voglio tokenize il sentence_content, in modo che i tb_tokens tabelle conterranno:

========================================================================== 
| 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... 

ecco il mio codice:

$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); 
} 

con la funzione di tokenizing è:

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')"); 
      } 
     } 
    } 
} 

il problema è Il document_id non può essere letto e non può essere inserito nella tabella tb + token. Come chiamare quelli document_id? grazie :)

QUESTIONE MODIFICATA: ogni parola (il risultato della tokenizzazione) ha document_id e sentence_id. il mio problema è impossibile chiamare lo document_id. come chiamare sia sentence_id e document_id in ogni parole?

+0

Un buon lavoro che presenta la domanda. TRANNE ... "il problema è Il documento_id non può essere letto e non può essere inserito nella tabella tb + token" - puoi essere più preciso? Cosa non va? – Smandoli

+0

@Smandoli scusa se il mio inglese è cattivo. Ogni 'frase_conten' ha il' document_id'. Ho bisogno di inserire anche le parole token con document_id, ma non riesco a leggere il documento_id – bruine

+0

Non c'è '$ row ['document_id']' perché non hai incluso 'document_id' nell'elenco di selezione della seconda query. –

risposta

1

penso che non hai bisogno di questi codice:

$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']; 
} 

gamma di $ doc_id non è mai stato utilizzato

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

$ Kal è un array e non c'è bisogno di essere tagliato

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

perché si sta andando a registrare il sentence_id, dovrebbe essere $ row2 [ 'sentence_id'] non []

(ovviamente è necessario assicurarsi, non ci sarà lo stesso sentence_id nella stessa document_id o altro si dovrebbe concat esso)

questo è un paio di correzioni da parte mia:

$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')"); 
      } 
     } 
    } 
} 

ho analizzare il ID_documento alla funzione

+0

oh, sciocco me .. sì, hai ragione ..! grande! grazie mille @ivantedja. imparo molte cose da te :) – bruine

Problemi correlati