PHP 中将数组的值与字符串相匹配

发布于 2024-10-25 19:20:02 字数 553 浏览 1 评论 0原文

我正在做一个小项目,我需要一些帮助。我有一个包含 150,000 行的 CSV 文件(每行有 10 列数据)。我使用 fscvread 来读取文件,在循环期间我想将每行的一列(称为 stringx)与 10,000 个单词的数组进行匹配。如果 stringx 中存在 10,000 个单词中的任何一个,则使用 preg_replace 将其删除。

现在这一切都很好。我一切正常,但问题是,它太慢了。

我尝试了两种方法来匹配数组。 1)我使用explode(" ", $stringx)将stringx转换为数组,然后使用array_diff($array_stringx, $array_10000); 2) 在 $array_10000 上使用 foreach 并在 $stringx 上使用 preg_replace

方法 1 需要大约 60 秒来遍历 200 行数据,方法 2 可以在 60 秒内循环 500 行。

有更好的方法吗?

再次,我正在寻找一种有效的方法(基本上) array_diff 一次将 10,000 个单词的数组与 150,000 个字符串进行比较。

非常感谢您的帮助。

I am working on a small project and I need some help. I have a CSV file with 150,000 rows (each row has 10 cols of data). I am using the fscvread to read the file and during the loop I want to match one of the columns (call it stringx) of each row against an array of 10,000 words. If any of the 10,000 words exist in stringx, it is removed using preg_replace.

Now all of this is fine. I have everything working fine, but the problem is, its too slow.

I have tried 2 methods to match the array.
1) I convert stringx to an array using explode(" ", $stringx) and then use the array_diff($array_stringx, $array_10000);
2) use foreach on $array_10000 and preg_replace on $stringx

Method 1 takes about 60 secs to go through 200 rows of data and method 2 can loop 500 rows in 60 secs.

Is there a better way to do this?

Once again, I am looking for an efficient way to (basically) array_diff an array of 10,000 words against 150,000 strings one at a time.

Help is much appreciated.

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

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

发布评论

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

评论(6

峩卟喜欢 2024-11-01 19:20:02

以下只是一个替代方案。它可能会也可能不会满足您的要求。

在我的笔记本电脑上,它每秒执行 84 次操作,包含 10k 单词字典和 15k 字符串。

缺点是它不会删除单词周围的空格。

$wordlist 只是每行一个单词,可以是一个文件。

$dict = array_flip(preg_split('/\n/',$wordlist));

function filter($str,$dict) {
  $words = preg_split('/\s/',$str);
  sort($words);
  $words = array_unique($words);

  foreach ($words as $word) {
    if (key_exists($word,$dict)) {
        $removeWords[] = '/\b' . $word . '\b/';
    }
  }
  return preg_replace($removeWords, '', $str);
}

另一个执行速度更快的示例(107ops/s,15kb 字符串和 10k 单词字典)

function filter2($str,$dict) {
  $words = preg_split('/\b/',$str);
  foreach ($words as $k => $word) {
    if (key_exists($word,$dict)) {
        unset($words[$k]);
    }
  }
  return implode('', $words);
}

The following is just an alternative. It may or may not fulfil your requirements.

It performs 84 ops/second with 10k words dictionary and 15k string on my laptop.

Downside is that it does not remove the spaces around the words.

$wordlist is just rows with one word each, could be a file.

$dict = array_flip(preg_split('/\n/',$wordlist));

function filter($str,$dict) {
  $words = preg_split('/\s/',$str);
  sort($words);
  $words = array_unique($words);

  foreach ($words as $word) {
    if (key_exists($word,$dict)) {
        $removeWords[] = '/\b' . $word . '\b/';
    }
  }
  return preg_replace($removeWords, '', $str);
}

Another example that performs a bit faster (107ops/s with 15kb string and 10k words dictionary)

function filter2($str,$dict) {
  $words = preg_split('/\b/',$str);
  foreach ($words as $k => $word) {
    if (key_exists($word,$dict)) {
        unset($words[$k]);
    }
  }
  return implode('', $words);
}
甜味拾荒者 2024-11-01 19:20:02

不爆炸 stringx,并对每个单词执行 stripos() 怎么样?在 $array_10000 中?

像这样:

foreach ($array_10000 as $word)
{
    if (stripos($stringx, $word) !== false)
    {
        // do your stuff
    }
}

How about not exploding stringx, and doing an stripos() for each word in $array_10000?

like this:

foreach ($array_10000 as $word)
{
    if (stripos($stringx, $word) !== false)
    {
        // do your stuff
    }
}
沙与沫 2024-11-01 19:20:02

你的 10000 字数组排序了吗?如果没有,请先尝试排序。

编辑:好的,因为它已经排序了,我猜也许 PHP 的 array_search 不执行二进制搜索,所以我会寻找二进制搜索实现并使用它。如果确实只是线性搜索,那么您将获得一个数量级的速度提升。

Is your 10000 word array sorted? If not, try sorting it first.

Edit: ok since it's sorted i'm guessing maybe PHP's array_search doesn't do a binary search, so i'd look for a binary search implementation and use that. If indeed it's just a linear search then you'll get an order of magnitude speed increase that way.

苦笑流年记忆 2024-11-01 19:20:02

PHP 不是追求速度的语言,但我想您知道这一点。我必须在我正在编写的项目中执行类似的操作,并且我正在使用 PHP 编写一个文件,然后使用独立的 Matlab 来读取该文件,对其进行处理并将其输出到另一个文件。

您可以执行相同的操作,并用 C 编写一个小程序,其功能与 array_diff() 相同。我认为会有很大的差异,尽管我没有做过任何测试。

PHP isn't the language for speed, but I guess you know that. I have to do something like that in a project I'm writing and I'm writing a file with PHP, then using a Matlab standalone to read that file, process it and output it an other one.

You could do the same and write a small program in C that does the same as array_diff(). I think there will be a huge difference, although I haven't done any testing.

故人的歌 2024-11-01 19:20:02

我还没有测试过这一点,但我突然想到:

您可以尝试使用正则表达式预先解析文件以获得要过滤的 150,000 个单词(基于列分隔符),然后您可以进行文本替换,选择基于我在谷歌上搜索的这篇文章的最佳功能。

我希望它有帮助!干杯!

I haven't tested this, but it just occured to me:

You could try pre-parsing the file with a regex to obtain the 150,000 words to filter (based on the column separator) and then you could do the text replacement, picking the best function based on this article I googled.

I hope it helps! Cheers!

甜心 2024-11-01 19:20:02

您可以只执行 foreachimplode

$words = array("one","two", "three");
$number = 0;
foreach ($words as $false_array)
{
$number += 1;
$array[$number] = $false_array;
echo "Added ". $false_array . ". ";
}
foreach ($words as $false_array)
{
echo "Array Contains " . $false_array . ". ";
}

如果你在 php 中执行这个,你会得到:

Added one. Added two. Added three. Array Contains one. Array Contains two. Array Contains three.

You can just do the foreach and also the implode.

$words = array("one","two", "three");
$number = 0;
foreach ($words as $false_array)
{
$number += 1;
$array[$number] = $false_array;
echo "Added ". $false_array . ". ";
}
foreach ($words as $false_array)
{
echo "Array Contains " . $false_array . ". ";
}

If you were to execute this in php, you would get:

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