计算子字符串出现次数和返回得分

时间:2014-05-07 22:27:21

标签: php mysql

我正在开发这个项目,我在这个MySQL数据库中有一个名为“speechesLCMcoded”的表,其中包含数千行解析文本。我有四列“关键”,“解析”,“重量”和“计数”。

“已解析”列中的文字如下所示:

{N}table{/N}{V}play{/V}{A}big{/A}

我有另一个名为“具体性”的表,它有两列,在“word”列中我有一个单词列表,在“score”栏中我有每个单词的具体分数。例如,它看起来像这样

Word    Score 
table   5 
play    3 
big     2

我正在制作一个php脚本,它将为我的表格的每一行“speechesLCMcoded”计算每个单词的总累积分数和得分单词的总数。

在我的示例中,结果如下所示:

Key    parsed                            weight    count
1     {N}table{/N}{V}play{/V}{A}big{/A}  10        3

我已经写了一部分脚本,但由于我对php的经验有限而陷入困境。在我的脚本

中有两个“while”循环的事实让我感到困惑

你怎么建议我这样做?

<?php
//Include functions
        include "functions.php";
        ini_set('max_execution_time', 90000);
        echo 'Time Limit = ' . ini_get('max_execution_time');

//Conecting the database
        if (!$conn) {
         die('Not connected : ' . mysql_error());}

// make LCM the current db
        mysql_select_db('senate');
        $data = mysql_query("SELECT `key`, `tagged` FROM speecheLCMcoded") 
        or die(mysql_error()); 

// puts the "data" info into the $info array 
        while($info = mysql_fetch_array( $data) ){
        $key=$info['key'];
        $tagged=$info['tagged'];

// Print out the contents of the entry 
        Print "<b>Key:</b> ".$info['key'] .  " <br>";  

// Select Word List and their weights
        $dataweights = mysql_query("SELECT `word`, `weight` FROM concreteness") 
        or die(mysql_error()); 

// puts the "data" info into the $infoweights array 
        while($infoweights = mysql_fetch_array( $infoweights) ){
        $word=$info['word'];
        $score=$info['score'];      

// Calulate number of adjectives
        ob_start(); //Start output buffer
        print substr_count($tagged,"{"); 
        $adjectives = ob_get_contents(); //Grab output
        ob_end_clean(); //Discard output buffer

// Calulate number of coded items


//Saving number of coded words
        $sql = "UPDATE speechestest SET adjectives='$adjectives', verbs='$verbs', nouns='$nouns' WHERE `key`='$key';" ;
        $retval = mysql_query( $sql, $conn );
        if(! $retval )
        {die('Could not update data: ' . mysql_error());}
        echo "Updated data successfully\n";
}
 ?> 

1 个答案:

答案 0 :(得分:1)

您想要做的是以下内容:

o  Query the database to get your sentences

o  For each sentence (while loop) {

  o  Get a list of all the words in your sentence (by breaking up the sentence into words--c.f. explode and preg_match_all)

  o  For each word you found (while loop) {

    o  Query the database to get information about the word.

    o  Do whatever you need to do with the word data.
  }

}

我没有提供代码,因为这是你的工作:)另外,尝试在人类可读的句子中写出你想做的事情,然后用代码替换这些句子。它应该让你更清楚自己在做什么以及为什么要这样做。