(PHP) 在大型文本文档中随机插入10个单词的句子

发布于 2024-08-02 04:46:39 字数 239 浏览 4 评论 0原文

我有 140k 或更大的大型文本文件,其中充满了文本段落,并且仅当文件包含超过 200 个单词时,才需要以随机间隔向该文件插入一个句子。

我需要在较大的文档中随机插入的句子有 10 个单词长。

我可以完全控制运行 LAMP 站点的服务器,因此我可以使用 PHP 或 Linux 命令行应用程序(如果存在)为我执行此操作。

任何关于如何最好地解决这个问题的想法将不胜感激。

谢谢马克

I have large text files 140k or larger full of paragraphs of text and need to insert a sentence in to this file at random intervals only if the file contains more then 200 words.

The sentence I need to insert randomly throughout the larger document is 10 words long.

I have full control over the server running my LAMP site so I can use PHP or a linux command line application if one exists which would do this for me.

Any ideas of how best to tackle this would be greatly appreciated.

Thanks

Mark

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

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

发布评论

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

评论(2

我三岁 2024-08-09 04:46:39

您可以使用 str_word_count() 来获取字符串中的单词数。从那里确定是否要插入字符串。至于“随机”插入它,这可能是危险的。您的意思是建议您将其插入几个随机区域吗?如果是这样,请使用 file() 将文件内容作为数组加载,并在 $file[0]count($file );

You could use str_word_count() to get the number of words in the string. From there, determine if you want to insert the string or not. As for inserting it "at random," that could be dangerous. Do you mean to suggest you want to insert it in a couple random areas? If so, load the contents of the file in as an array with file() and insert your sentence anywhere between $file[0] and count($file);

千秋岁 2024-08-09 04:46:39

以下代码应该可以实现定位字符串并将其插入到随机位置的技巧。从那里您只需要重写该文件。这是一种非常粗略的方法,没有考虑标点符号或类似的东西,因此很可能需要进行一些微调。

$save = array();
$words = str_word_count(file_get_contents('somefile.txt'), 1);

if (count($words) <= 200)
  $save = $words;
else {
  foreach ($words as $word) {
    $save[] = $word;
    $rand = rand(0, 1000);
    if ($rand >= 100 && $rand <= 200)
      $save[] = 'some string';
  }
}

$save = implode(' ', $save);

这会生成一个随机数,并检查它是否在 100 到 200 之间(包含 100 和 200),如果是,则放入随机字符串。您可以更改随机数的范围和检查的范围以增加或减少添加的数量。您还可以实现一个计数器来执行某些操作,例如确保每个字符串之间至少有 x 个单词。

同样,这没有考虑标点符号或任何东西,只是假设所有单词都用空格分隔。因此,可能需要进行一些微调来完善它,但这应该是一个很好的起点。

The following code should do the trick to locate and insert strings into random locations. From there you would just need to re-write the file. This is a very crude way and does not take into account punctuation or anything like that, so some fine-tuning will most likely be necessary.

$save = array();
$words = str_word_count(file_get_contents('somefile.txt'), 1);

if (count($words) <= 200)
  $save = $words;
else {
  foreach ($words as $word) {
    $save[] = $word;
    $rand = rand(0, 1000);
    if ($rand >= 100 && $rand <= 200)
      $save[] = 'some string';
  }
}

$save = implode(' ', $save);

This generates a random number and checks if it's between 100 and 200 inclusive and, if so, puts in the random string. You can change the range of the random number and that of the check to increase or decrease how many are added. You could also implement a counter to do something like make sure there are at least x words between each string.

Again, this doesn't take into account punctuation or anything and just assumes all words are separated by spaces. So some fine tuning may be necessary to perfect it, but this should be a good starting point.

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