读取PHP中对应的值并添加到运行总和中
我希望在文件中交叉引用字符串中的每个单词。
因此,如果给我字符串:Jumping jacks唤醒我在早上。
- 我使用一些正则表达式来去掉句点。此外,整个字符串都变为小写。
- 然后,我继续使用 PHP 漂亮的
explode()
函数将单词分成一个数组。 - 现在,我剩下的是一个数组,其中包含字符串中使用的单词。
从那里我需要查找数组中的每个值并获取它的值并将其添加到运行总和中。它是 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.
- I use some regex to strip out the period. Also, the entire string is made lowercase.
- I then go on to have the words separated into an array by using PHP's nifty
explode()
function. - 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 \t
s 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您的评论和问题编辑编辑答案。运行总和存储在名为 $sum 的数组中,其中“word”的键值将存储其运行总和的值。例如 $sum['wake'] 将存储单词wake 的运行总和等。
我假设您会注意区分大小写,因为您提到您将整个字符串设为小写,所以我不将其包含在此处。
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.
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.
explodeby space,检查该单词是否作为key存在于words数组中;如果是这样,则增加计数(val);如果不是,请将其 val 设置为 1。为每个句子循环此操作,而无需重新声明 $words=array()
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()