4

tb_content(左)和tb_word(右):

=====================================    ================================
|id|sentence |sentence_id|content_id|    |id|word|sentence_id|content_id|
=====================================    ================================
| 1|sentence1|    0      |    1     |    | 1|  a |     0     |    1     |
| 2|sentence2|    1      |    1     |    | 2|  b |     0     |    1     |
| 3|sentence5|    0      |    2     |    | 3|  c |     1     |    1     |
| 4|sentence6|    1      |    2     |    | 4|  a |     1     |    1     |
| 5|sentence7|    2      |    2     |    | 5|  e |     1     |    1     |
=====================================    | 6|  f |     0     |    2     |
                                         | 7|  g |     1     |    2     |
                                         | 8|  h |     1     |    2     |
                                         | 9|  i |     1     |    2     |
                                         |10|  f |     2     |    2     |
                                         |11|  h |     2     |    2     |
                                         |12|  f |     2     |    2     |
                                         ================================

我需要检查每个句子是否包含每个句子中其他句子所拥有的单词content_id

例如 :

检查content_id=1他们是sentence1sentence2。从tb_word,我们可以看出sentence1sentence2由同一个词组成a。如果a两个句子中的个数是>=2,那么a就是结果。因此,如果我打印结果,它必须是: 00Array ( [0] => a [1] => b) 01Array ( [3] => a ) 10Array ( [3] => a )11Array ( [0] => c [1] => a [2] => e)where00表示sentence_id=0sentence_id=0

首先,我计算每个拥有 functionTotal多少:sentencecontent_id

$total = array();
$sql = mysql_query('select content_id, count(*) as RowAmount 
       from tb_content Group By contente_id') or die(mysql_error());
while ($row = mysql_fetch_array($sql)) {
    $total[] = $row['RowAmount']; 
}
return $total;

从那个函数我得到 和 的值$total,我需要检查一些单词(from tb_word)在 2 的所有可能性之间的相似性sentence

foreach ($total as $content_id => $totals){
for ($x=0; $x <= ($totals-1); $x++) {
    for ($y=0; $y <= ($totals-1); $y++) {
      $shared = getShared($x, $y);
    }
}

的功能getShared是:

function getShared ($x, $y){
    $token = array();
    $shared = array();
    $i = 0;
    if ($x == $y) {
        $query = mysql_query("SELECT word FROM `tb_word`
                             WHERE sentence_id ='$x' ");
        while ($row = mysql_fetch_array($query)) {
            $shared[$i] = $row['word'];
            $i++;
        }

    } else {
        $query = mysql_query("SELECT word, count(word) as jml 
                             FROM `tb_word` WHERE sentence_id ='$x' 
                             OR sentence_id ='$y' 
                             GROUP BY word ");
        while ($row = mysql_fetch_array($query)) {
            $jml = $row['jml'];
            $token[$i] = $row['word'];
            if ($jml >= 2) {
                $shared[$i] = $token[$i];
            }
            $i++;
        }

但是我得到的结果仍然是错误的。结果还是混在了不同的地方content_id。结果也必须分组content_id。对不起我糟糕的英语和糟糕的解释。cmiiw,请帮助我..谢谢:)

4

2 回答 2

1

这一项实际上可以由 DBMS 自己完成,一次查询两步。首先,为了在相同的内容中准备句子组合,您进行了自连接:

SELECT a.content_id,
       a.sentence_id AS sentence_id_1,
       b.sentence_id AS sentence_id_2
FROM   tb_content AS a
       JOIN tb_content AS b
         ON ( a.content_id = b.content_id
              AND a.sentence_id <= b.sentence_id )

“<=”将保持相同的句子连接,如“1-1”或“2-2”,但避免双向重复,如“1-2”和“2-1”。接下来,您可以将上述结果与单词结合并计算出现次数。像那样:

SELECT s.content_id,
       s.sentence_id_1,
       s.sentence_id_2,
       c.word,
       Count(*) AS jml
FROM   (SELECT a.content_id,
               a.sentence_id AS sentence_id_1,
               b.sentence_id AS sentence_id_2
        FROM   tb_content AS a
               JOIN tb_content AS b
                 ON ( a.content_id = b.content_id
                      AND a.sentence_id <= b.sentence_id )) AS s
       JOIN tb_word AS c
         ON ( s.content_id = c.content_id
              AND ( c.sentence_id = s.sentence_id_1
                     OR c.sentence_id = s.sentence_id_2 ) )
GROUP  BY s.content_id,
          s.sentence_id_1,
          s.sentence_id_2,
          c.word
HAVING Count(*) >= 2; 

上述查询的结果将为您提供容器、句子 1 和 2、单词以及出现次数(2 次或更多)。您现在需要的只是将结果收集到数组中,正如我看到的那样,您已经知道该怎么做。

如果我误解了你的目标,请告诉我。

于 2012-09-14T22:34:55.823 回答
1

简单的怎么样SELECT content_id, word, COUNT(*) as num_appearing FROM tb_word GROUP BY content_id, word

编辑:我现在看到了复杂性:您的主要问题是该getShared()函数有两个句子 ID 传递给它,但不content_id知道正在分析哪些内容。您还假设content_idandsentence_id数字是连续的并且从零开始。我的代码不假设,而是直接从数据库中提取这些 ID。

<?php
$rs = mysql_query("SELECT * FROM tb_content");
$content = array();
while ($row = mysql_fetch_assoc($rs)) {
    if (!isset($content[$row['content_id']])) $content[$row['content_id']] = array();
    $content[$row['content_id']][] = $row['sentence_id'];
}
foreach($content as $content_id => $sentences) {
  foreach($sentences as $sentence_id) {
    foreach($sentences as $compare) {
      $shared = getShared($content_id, $sentence_id, $compare);
    }
  }
}
function getShared($cid, $s1, $s2) {
  $rs = mysql_query("SELECT `word`, COUNT(*) AS 'num' FROM `tb_word` WHERE `content_id`={$cid} AND `sentence_id` IN ({$s1}, {$s2}) GROUP BY `word`");
  $out = array();
  while ($row = mysql_fetch_assoc($rs)) {
    if ($rs['num'] >= 2) $out[$rs['word']] = $rs['num'];
  }
  return $out;
}
于 2012-09-14T05:02:33.573 回答