如何找出文本块中最常用的 2 个单词组合?

发布于 2024-10-16 23:11:30 字数 145 浏览 5 评论 0原文

如何从一段文本中找出我最常用的两个单词?换句话说,是否有一个在线或离线(或代码)工具,我可以在其中复制和粘贴文本,并输出我最常用的两个词频,例如:

从最常用到最少:

“猫”2.9% “她说” 1.8% “去了” 1.2%

谢谢

How can I find out what the most common two words that I used right after each other are from a block of text? In other words is there a tool online or offline (or code) where I can copy and paste text and it outputs my most used two word frequency like:

From most used to least:

"the cat" 2.9%
"she said" 1.8%
"went to" 1.2%

Thanks

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

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

发布评论

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

评论(2

等往事风中吹 2024-10-23 23:11:30
  1. 将文本分成两个单词对(使用 substrstrpos 来帮助您)

    • 使用 strpos 查找空格的第二个索引,然后使用开头和第二个空格索引之间的子字符串来获取两个单词对。
  2. 将每一对添加到地图或集合中(该对将是键)并设置值(如果地图中已存在,则增加值)
  3. 解析全文后,根据地图的大小计算百分比/set 和每对的值。
  1. Chunk up the text into two word pairs (use substr and strpos to help you)

    • find the second index of a space, using strpos, and then substring between the beginning and the second space index to get the two word pair.
  2. Add each pair to a map or set (the pair will be the key) and set the value (if it already exists in the map, increment the value
  3. Once you have parsed the full text, calculate the percentages based on the size of the map/set and the value for each pair.
生生不灭 2024-10-23 23:11:30

这很有趣,但我做了一些尝试,这应该让你开始,而不应该是你的答案。

这基本上是将单词分组为 2,将它们索引到数组中并递增找到的时间,最后转换为百分比:)

$data = 'In the first centuries of typesetting, quotations were distinguished merely by indicating the speaker, and this can still be seen in some editions of the Bible. During the Renaissance, quotations were distinguished by setting in a typeface contrasting with the main body text (often Italic type with roman, or the other way round). Block quotations were set this way at full size and full measure.
Quotation marks were first cut in type during the middle of the sixteenth century, and were used copiously by some printers by the seventeenth. In Baroque and Romantic-period books, they could be repeated at the beginning of every line of a long quotation. When this practice was abandoned, the empty margin remained, leaving an indented block quotation';

//Clean The Data from un required chars!
$data = preg_replace("/[^\w]/"," ",$data);

$segments = explode(" ",$data);
$indexes = array();

for($i=0;$i<count($segments);$i++)
{
   if($i == 0)
   {
      continue;
   }

   if(trim($segments[$i - 1]) != "" && trim($segments[$i]) != "")
   {
      $key = trim($segments[$i - 1]) . " " . trim($segments[$i]);
      if(array_key_exists($key,$indexes))
      {
          $indexes[$key]["count"]++;
      }else
      {
          $indexes[$key] = array(
              'count' => 1,
              'words' => $key
          );
      }
   }
}

//Change to the percentage:
$total_double_words = count($segments);
foreach($indexes as $id => $set)
{
    $indexes[$id]['percentage'] = number_format((($set['count']/ $total_double_words) * 100),2) . "%";
}

var_dump($indexes);

您可以在这里看到它:

This was fun, but i had a little go at it, this should get you started and should not be your answer.

This is basically grouping the words into 2's, indexing them into an array and incrementing the times there found, and finally converting into a percentage :)

$data = 'In the first centuries of typesetting, quotations were distinguished merely by indicating the speaker, and this can still be seen in some editions of the Bible. During the Renaissance, quotations were distinguished by setting in a typeface contrasting with the main body text (often Italic type with roman, or the other way round). Block quotations were set this way at full size and full measure.
Quotation marks were first cut in type during the middle of the sixteenth century, and were used copiously by some printers by the seventeenth. In Baroque and Romantic-period books, they could be repeated at the beginning of every line of a long quotation. When this practice was abandoned, the empty margin remained, leaving an indented block quotation';

//Clean The Data from un required chars!
$data = preg_replace("/[^\w]/"," ",$data);

$segments = explode(" ",$data);
$indexes = array();

for($i=0;$i<count($segments);$i++)
{
   if($i == 0)
   {
      continue;
   }

   if(trim($segments[$i - 1]) != "" && trim($segments[$i]) != "")
   {
      $key = trim($segments[$i - 1]) . " " . trim($segments[$i]);
      if(array_key_exists($key,$indexes))
      {
          $indexes[$key]["count"]++;
      }else
      {
          $indexes[$key] = array(
              'count' => 1,
              'words' => $key
          );
      }
   }
}

//Change to the percentage:
$total_double_words = count($segments);
foreach($indexes as $id => $set)
{
    $indexes[$id]['percentage'] = number_format((($set['count']/ $total_double_words) * 100),2) . "%";
}

var_dump($indexes);

You can see it live here: http://codepad.org/rcwpddW8

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