删除行包含特定数量的单词

时间:2015-07-28 20:31:19

标签: php

我的问题可以通过下面给出的例子来理解:

假设这是文本文件,其中包含以下行:

  

你好,这是我的word文件,这是第1行   你好,这是第二行,这是一些文字
  你好,这是第三行,还有一些文字
  jhasg djgha sdgasjhgdjasgh jdkh
  sdhgfkjg sdjhgf sjkdghf sdhf
  s hdg fjhsgd fjhgsdj gfj ksdgh

  • 我想让每一行都变成一个变量
  • 然后将该行的所有单词都放入数组中
  • 然后将包含该行的单词的数组与下一行的所有单词进行比较
  • 如果单词的匹配数超过3,则删除该行

所以在上面的例子中输出应该是:

  

你好,这是我的word文件,这是第1行   jhasg djgha sdgasjhgdjasgh jdkh
  sdhgfkjg sdjhgf sjkdghf sdhf
  s hdg fjhsgd fjhgsdj gfj ksdgh

由于hello this is line超过3个字,因此删除包含这些字的行。请注意,第一行不会被删除,因为它是唯一的....

我尝试编写自己的代码并创建了一个混乱,创建了200mb文本文件,其中包含无限数量的第一行文本。无论如何这里是代码,不执行它,否则你最终可能会硬盘已满。

<?php

$fileA = fopen("names.txt", "r");
$fileB = fopen("anothernames.txt", "r");
$fileC = fopen("uniquenames.txt", "w");
while(!feof($fileA))
{
    $line = fgets($fileA);
    $words = explode(" ", $line);
    $size = count($words);

    while(!feof($fileA))
    {
        $line1 = fgets($fileB);
        $words1 = explode(" ", $line1);
        $size1 = count($words1);

        $c=0;

        for($i=0; $i<$size; $i++)
        {
                for($j=0; $j<$size1; $j++)
            {
                    if($words[$i]==$words1[$j])
                        $c++;
            }
        }
        if($c<3)
            fwrite($fileC, $line);
    }
}

fclose($fileA);
fclose($fileB);
fclose($fileC);

?>

由于

3 个答案:

答案 0 :(得分:1)

一种简单的方法如下:

  • 使用file()
  • 阅读所有行
  • 创建一个包含每个单词索引的句子的数组。
  • 最后建立一个出现在任何数组中的每个句子的黑名单,计算任何单词的3个以上的条目。
  • 然后打印除黑名单之外的每一行:

示例:

    <?php
$lines = array("hello this is my word file and this is line number 1",
  "hello this is second line and this is some text",
  "hello this is third line and again some text",
  "jhasg djgha sdgasjhgdjasgh jdkh",
  "sdhgfkjg sdjhgf sjkdghf sdhf",
  "s hdg fjhsgd fjhgsdj gfj ksdgh");

//$lines = file("path/to/file");

$result = array();
//build "count-per-word" array
foreach ($lines AS $line){
   $words = explode(" ", $line);
   foreach ($words AS $word){
       $word = strtolower($word);
       if (isset($result[$word]))
           $result[$word][] = $line;
       else
           $result[$word] = array($line);  
   }
}

//Blacklist each sentence, containing a word appearing in 3 sentences.
$blacklist = array();
foreach ($result AS $word => $entries){
   if (count($entries) >= 3){
     foreach($entries AS $entry){
       $blacklist[] = $entry;
     }
   }
}

//list all not blacklisted. 
foreach ($lines AS $line){
  if (!in_array($line, $blacklist))
      echo $line."<br />";
}

?>

输出:

jhasg djgha sdgasjhgdjasgh jdkh
sdhgfkjg sdjhgf sjkdghf sdhf
s hdg fjhsgd fjhgsdj gfj ksdgh

请注意,这也会将包含3倍相同单词的单个句子列入黑名单,例如&#34; Foo Foo Foo bar&#34;。

要进行此操作,请检查该行是否已经已知&#34;已知&#34;在将它推送到数组之前的某个单词:

foreach ($words AS $word){
   if (isset($result[$word])){
       if (!in_array($line, $result[$word])){
          $result[$word][] = $line;
       }
   }else
       $result[$word] = array($line);  
}

答案 1 :(得分:0)

#second 
while(!feof($fileA))
#should be
while(!feof($fileB))

if($c<3)
        fwrite($fileC, $line);
#should
if($c<3){
   fwrite($fileC, $line);
   continue 2;
}

  

然后将包含该行的单词的数组与下一行的所有单词进行比较

只有在将文件与自身进行比较时才会发生!

编辑:我的帖子根本没有任何意义,请阅读上一篇文章的说明!

答案 2 :(得分:0)

为什么不只是array_intersect

php > $l1 = 'hello this is my word file and this is line number 1';
php > $l2 = 'hello this is second line and this is some text';
php > $a1 = explode(" ", $l1);
php > $a2 = explode(" ", $l2);
php > var_dump(array_intersect($a1, $a2));
array(7) {
  [0]=>
  string(5) "hello"
  [1]=>
  string(4) "this"
  [2]=>
  string(2) "is"
  [6]=>
  string(3) "and"
  [7]=>
  string(4) "this"
  [8]=>
  string(2) "is"
  [9]=>
  string(4) "line"
}


if (count of intersection >= 3) {
  skip line
}

或者我正在阅读你的#34;匹配&#34;太宽松了?

相关问题