读取PHP中对应的值并添加到运行总和中

发布于 2024-12-24 17:07:09 字数 750 浏览 1 评论 0原文

我希望在文件中交叉引用字符串中的每个单词。

因此,如果给我字符串:Jumping jacks唤醒我在早上。

  1. 我使用一些正则表达式来去掉句点。此外,整个字符串都变为小写。
  2. 然后,我继续使用 PHP 漂亮的 explode() 函数将单词分成一个数组。
  3. 现在,我剩下的是一个数组,其中包含字符串中使用的单词。

从那里我需要查找数组中的每个值并获取它的值并将其添加到运行总和中。它是 for() 循环。好吧,这就是我陷入困境的地方......

列表($wordlist)的结构如下:

wake#4 waking#3 0.125

morning#2 - 0.125

单词和数字之间有\t。每个值可以有多个单词。

我现在需要 PHP 做的是查找数组中每个单词的数字,然后拉回相应的数字以将其添加到运行总和中。对我来说最好的方法是什么?

答案应该很简单,只需在单词列表中找到字符串的位置,然后找到选项卡并从那里读取 int...我只需要一些指导。

提前致谢。

编辑:澄清一下——我不想要单词列表的值的总和,相反,我想查找我的个人值,因为它们对应于句子中的单词,然后在列表中查找它们并添加这些值;不是全部。

I would like to have each word in a string cross-referenced in a file.

So, if I was given the string: Jumping jacks wake me up in the morning.

  1. I use some regex to strip out the period. Also, the entire string is made lowercase.
  2. I then go on to have the words separated into an array by using PHP's nifty explode() function.
  3. Now, what I'm left with, is an array with the words used in the string.

From there I need to look up each value in the array and get a value for it and add it to a running sum. for() loop it is. Okay, this is where I get stuck...

The list ($wordlist) is structured like so:

wake#4 waking#3 0.125

morning#2 -0.125

There are \ts in between the word and the number. There can be more than one word per value.

What I need the PHP to do now is look up the number to each word in the array then pull that corresponding number back to add it to a running sum. What's the best way for me to go about this?

The answer should be easy enough, just finding the location of the string in the wordlist and then finding the tab and from there reading the int... I just need some guidance.

Thanks in advance.

EDIT: to clarify -- I don't want the sum of the values of the wordlist, rather, I'd like to look up my individual values as they correspond to the words in the sentence and THEN look them up in the list and add just those values; not all of them.

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

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

发布评论

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

评论(2

羁客 2024-12-31 17:07:09

根据您的评论和问题编辑编辑答案。运行总和存储在名为 $sum 的数组中,其中“word”的键值将存储其运行总和的值。例如 $sum['wake'] 将存储单词wake 的运行总和等。

$sum = array();
foreach($wordlist as $word) //Loop through each word in wordlist
{
    // Getting the value for the word by matching pattern.
    //The number value for each word is stored in an array $word_values, where the key is the word and value is the value for that word.
    // The word is got by matching upto '#'. The first parenthesis matches the word - (\w+)
    //The word is followed by #, single digit(\d), multiple spaces(\s+), then the number value(\S+ matches the rest of the non-space characters)
    //The second parenthesis matches the number value for the word

    preg_match('/(\w+)#\d\s+(\S+)/', $word, $match);  
    $word_ref = $match[1];
    $word_ref_number = $match[2];
    $word_values["$word_ref"] = $word_ref_number;

}

//Assuming $sentence_array to store the array of words used in your string example {"Jumping", "jacks", "wake", "me", "up", "in", "the", "morning"}

foreach ($sentence_array as $word)
{
    if (!array_key_exists("$word", $sum)) $sum["$word"] = 0;
    $sum["$word"] += $word_values["$word"]; 
}

我假设您会注意区分大小写,因为您提到您将整个字符串设为小写,所以我不将其包含在此处。

Edited answer based on your comment and question edit. The running sum is stored in an array called $sum where the key value of the "word" will store the value of its running sum. e.g $sum['wake'] will store the running sum for the word wake and so on.

$sum = array();
foreach($wordlist as $word) //Loop through each word in wordlist
{
    // Getting the value for the word by matching pattern.
    //The number value for each word is stored in an array $word_values, where the key is the word and value is the value for that word.
    // The word is got by matching upto '#'. The first parenthesis matches the word - (\w+)
    //The word is followed by #, single digit(\d), multiple spaces(\s+), then the number value(\S+ matches the rest of the non-space characters)
    //The second parenthesis matches the number value for the word

    preg_match('/(\w+)#\d\s+(\S+)/', $word, $match);  
    $word_ref = $match[1];
    $word_ref_number = $match[2];
    $word_values["$word_ref"] = $word_ref_number;

}

//Assuming $sentence_array to store the array of words used in your string example {"Jumping", "jacks", "wake", "me", "up", "in", "the", "morning"}

foreach ($sentence_array as $word)
{
    if (!array_key_exists("$word", $sum)) $sum["$word"] = 0;
    $sum["$word"] += $word_values["$word"]; 
}

Am assuming you would take care of case sensitivities, since you mentioned that you make the entire string lowercase, so am not including that here.

暗恋未遂 2024-12-31 17:07:09
$sentence = 'Jumping jacks wake me up in the morning';

$words=array();

foreach( explode(' ',$sentence) as $w ){

  if( !array_key_exists($w,$words) ){

   $words[$w]++;

  } else {
    $words[$w]=1;
  }

}

explodeby space,检查该单词是否作为key存在于words数组中;如果是这样,则增加计数(val);如果不是,请将其 val 设置为 1。为每个句子循环此操作,而无需重新声明 $words=array()

$sentence = 'Jumping jacks wake me up in the morning';

$words=array();

foreach( explode(' ',$sentence) as $w ){

  if( !array_key_exists($w,$words) ){

   $words[$w]++;

  } else {
    $words[$w]=1;
  }

}

explodeby space, check if that word is in the words array as key; if so increment it's count(val); if not, set it's val as 1. Loop this for each of your sentences without redeclaring the $words=array()

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