douniwan_0025 2014-05-07 22:27
浏览 109
已采纳

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

I am working on this project where I have this MySQL database with a table named "speechesLCMcoded" with thousands of lines of parsed text. I have four columns that are "key", "parsed", "weight" and "count".

The text in the column "parsed" looks like this:

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

I have another table that is named "concreteness" that has two columns, In the column "word" I have a list of words and in the column "score" I have each word's concreteness score. For example it would look like this

Word    Score 
table   5 
play    3 
big     2

I am working on a php script that will calculate for each line of my table "speechesLCMcoded" the total cumulated score of each word and the total number of scored words.

In my example, the result would look like this:

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

I have written a part of the script but I am stuck because of my limited experience with php. I am confused by the fact that there are two "while" loops in my script

How would you advise me to do?

<?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
";
}
 ?> 
  • 写回答

1条回答 默认 最新

  • drduh44480 2014-05-07 22:43
    关注

    What you want to do is the following:

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

    I'm not providing code because that's your job :) Also, try writing out what you want to do in human-readable sentences, and then just replace those sentences with code. It should make it more clear what you're doing and why you're doing it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大