在 PHP 中逐行解码页面?

发布于 2024-11-01 16:10:20 字数 103 浏览 4 评论 0原文

我想检查文本文件中的每个单词是否存在于另一个大型词典文本文件的任何“行”中。

我尝试过的每一种方法都失败了,或者只是短暂地起作用。

没有一百万个嵌套循环怎么办?

I would like to check if every word in a text file exists in any "LINES" of another large dictionary text file.

Every way I have tried this has failed, or worked only briefly.

How can I do without a million nested loops?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

装纯掩盖桑 2024-11-08 16:10:25

我经常这样回答。但正则表达式可以避免大部分循环。

// get words
preg_match_all(':\p{L}{2,}:u', $text_file, $words);
$words = end($words);

// make a search regex  "abc|foobar|xyz|text|.."
$rx_words = implode("|", $words);

// find all words that exist on a line
preg_match_all(':^($rx_words)$:', file_get_contents("LINES"), $cmp);

// everything found if:
$found_all = !array_diff($cmp[1], $words);

通过一些额外的编码可以避免读取整个 LINES 文件。但我想在这里保持简单。

I'm answering this way too often. But a regex would avoid much of the looping.

// get words
preg_match_all(':\p{L}{2,}:u', $text_file, $words);
$words = end($words);

// make a search regex  "abc|foobar|xyz|text|.."
$rx_words = implode("|", $words);

// find all words that exist on a line
preg_match_all(':^($rx_words)$:', file_get_contents("LINES"), $cmp);

// everything found if:
$found_all = !array_diff($cmp[1], $words);

Reading in the whole LINES file can be avoided with some extra coding. But I wanted to keep it simple here.

素手挽清风 2024-11-08 16:10:25

伪代码 如果您有足够的内存:

for each line in text file:
   break line into words
   for each word in line:
       $wordMap[lowercase($word)] = 1;

for each line:
   break line into words
   for each word:
       if $wordMap[lowercase($word)] == 1:
          line has word $word

如果您没有足够的内存用于 $wordMap,则将 $wordMap 设为某种数据库。您也可以尝试布隆过滤器(http://code.google.com/p/php-bloom-filter/,http://en.wikipedia.org/wiki/Bloom_filter)。

Psuedocode If you have enough memory:

for each line in text file:
   break line into words
   for each word in line:
       $wordMap[lowercase($word)] = 1;

for each line:
   break line into words
   for each word:
       if $wordMap[lowercase($word)] == 1:
          line has word $word

If you don't have enough memory for $wordMap, then make $wordMap some sort of database. You might also try a bloom filter (http://code.google.com/p/php-bloom-filter/, http://en.wikipedia.org/wiki/Bloom_filter).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文