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.

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

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名