简体   繁体   English

需要有关逻辑的建议,以编写此php练习

[英]need advice with logic for coding out this php exercise

I've got a list in a text file with the top 1000 words used in the english language. 我在文本文件中有一个列表,列出了英语中使用的前1000个单词。 Each line has a list of up to 50 words, like this: 每行最多包含50个单词,如下所示:

the,stuff,is,thing,hi,bye,hello,a,stuffs 该,东西,是,东西,喜,再见,你好,一个,东西
cool,free,awesome,the,pray,is,crime 酷,免费,很棒,祈祷,犯罪
etc. 等等

I need to write code using that file as input, to make an output file with the a list of pairs of words which appear together in at least fifty different lists. 我需要使用该文件作为输入来编写代码,以使输出文件的单词对列表至少出现在五十个不同的列表中。 For example, in the above example, THE & IS appear together twice, but every other pair appears only once. 例如,在上面的示例中,THE&IS一起出现两次,而其他每对仅出现一次。

I can't store all possible pairs of words, so no brute force. 我无法存储所有可能的单词对,因此没有蛮力。

I'm trying to learn the language and I'm stuck on this exercise of the book. 我正在尝试学习该语言,并且坚持使用本书。 Please help. 请帮忙。 Any logic, guidance or code for this would help me. 为此的任何逻辑,指导或代码都会对我有帮助。

This is what I have so far. 到目前为止,这就是我所拥有的。 It doesn't do what's intended but I'm stuck: 它没有达到预期的目的,但是我被卡住了:

Code: 码:

//open the file
$handle = fopen("list.txt", 'r');

$count = 0;
$is = 0;
while(!feof($handle)) {

    $line = fgets($handle); 

    $words = explode(',', $line);

    echo $count . "<br /><br />";
    print_r($words);
    foreach ($words as $word) {
        if ($word == "is") {
            $is++;
        }
    }
    echo "<br /><br />";

$count++;
}

echo "Is count: $is";

//close the file
fclose($handle);

$fp = fopen('output.txt', 'w');
fwrite($fp, "is count: " . $is);
fclose($fp);

This is what I came up with but I think it's too bloated: 这是我想出的,但我认为它太肿了:

plan: 计划:
check the first value of the $words array 检查$words数组的第一个值
store the value into $cur_word 将值存储到$cur_word
store $cur_word as a key in an array ( $compare ) and $cur_word作为键存储在数组中( $compare ),
store the counter (line number) as the value of that key 将计数器(行号)存储为该键的值
it'll be 1 at this point 此时将为1
see if $cur_word is on each line and if it is then 看看$cur_word是否在每一行上,如果是,那么
put the value into $compare with the key as $cur_word if array has at least 50 values then continue 如果数组具有至少50个值,则将值放入$compare且键为$cur_word ,然后继续
else go to the next value of the $words array 否则转到$words数组的下一个值
if it has 50 values then 如果它有50个值,则
go to the next value and do the same thing 转到下一个值并执行相同的操作
compare both lists to see how many values match 比较两个列表以查看有多少个值匹配
if it's at least 50 then append 如果至少是50,则附加
the words to the output file 单词到输出文件

repeat this process with every word 每个字都重复此过程

There are probably 100's of solutions to this problem. 该问题可能有100多种解决方案。 Here is one 这是一个

$contents = file_get_contents("list.txt");

//assuming all words are separated by a , and converting new lines to word separators as well
$all_words = explode(",", str_replace("\n", ",", $contents)); 
$unique_words = array();    
foreach ($all_words as $word) {
    $unique_words[$word] = $word;
}

this will give you all the unique words in the file in an array. 这将为您提供数组中文件中的所有唯一单词。

You can also use the same technique to count the words 您也可以使用相同的技术来计算单词

$word_counts = array();
foreach ($all_words as $word) {
    if (array_key_exists($word, $word_counts)) {
        $word_counts[$word]++;
    } else {
        $word_counts[$word] = 1;
    }
}

then you can loop through and save the results 然后您可以循环浏览并保存结果

$fp = fopen("output.txt", "w");
foreach ($word_counts as $word => $count) {
    fwrite($fp, $word . " occured " . $count . " times" . PHP_EOL);
}
fclose($fp);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM