实现代码:
<?php
class MapReduce
{
public function map($str)
{
preg_match_all('/([a-zA-Z]+)/i', $str, $arr);
return $arr[0];
}
public function reduce($arr)
{
$ret_arr = [];
foreach ($arr as $val) {
if(empty($ret_arr[$val])){
$ret_arr[$val]=1;
}
else{
$ret_arr[$val]++;
}
}
return $ret_arr;
}
}
$mr = new MapReduce();
$str = <<<CONTENT
The book lists Greeven Cuomo under a minor misspelling of her birth name, Cristina Greeven. Her entry refers to Manhattan File, a New York society magazine she published after her father purchased it in 1994. The same entry includes an office address in the New York City neighborhood of SoHo, an office phone number, and a home phone number. In the '90s, Greeven Cuomo was known for hanging out with socialites like Alex von Furstenberg and the former CNN producer Pamela Gross. The latter appears in both "little black books" and is now a close friend of Melania Trump.
It's unclear why Greeven Cuomo is listed in the 1997 address book. She serves as the editor in chief of The Purist, a wellness website she founded in 2017. A message left with The Purist seeking comment from Greeven Cuomo was returned almost immediately by Chris Cuomo, who declined to comment for the record or make his spouse available.
Greeven Cuomo is one of 221 people who appear in Epstein's 1997 address book but not his previously published address book, which dates to 2004.
CONTENT;
$arr = $mr->map($str);
$arr = $mr->reduce($arr);
print_r($arr);
演示输出:
Array
(
[The] => 5
[book] => 4
[lists] => 1
[Greeven] => 6
[Cuomo] => 6
[under] => 1
[a] => 5
[minor] => 1
[misspelling] => 1
[of] => 5
[her] => 2
[birth] => 1
[name] => 1
[Cristina] => 1
[Her] => 1
[entry] => 2
[refers] => 1
[to] => 3
[Manhattan] => 1
[File] => 1
[New] => 2
[York] => 2
[society] => 1
[magazine] => 1
[she] => 2
[published] => 2
[after] => 1
[father] => 1
[purchased] => 1
[it] => 1
[in] => 7
[same] => 1
[includes] => 1
[an] => 2
[office] => 2
[address] => 4
[the] => 6
[City] => 1
[neighborhood] => 1
[SoHo] => 1
[phone] => 2
[number] => 2
[and] => 3
[home] => 1
[In] => 1
[s] => 3
[was] => 2
[known] => 1
[for] => 2
[hanging] => 1
[out] => 1
[with] => 2
[socialites] => 1
[like] => 1
[Alex] => 1
[von] => 1
[Furstenberg] => 1
[former] => 1
[CNN] => 1
[producer] => 1
[Pamela] => 1
[Gross] => 1
[latter] => 1
[appears] => 1
[both] => 1
[little] => 1
[black] => 1
[books] => 1
[is] => 3
[now] => 1
[close] => 1
[friend] => 1
[Melania] => 1
[Trump] => 1
[It] => 1
[unclear] => 1
[why] => 1
[listed] => 1
[She] => 1
[serves] => 1
[as] => 1
[editor] => 1
[chief] => 1
[Purist] => 2
[wellness] => 1
[website] => 1
[founded] => 1
[A] => 1
[message] => 1
[left] => 1
[seeking] => 1
[comment] => 2
[from] => 1
[returned] => 1
[almost] => 1
[immediately] => 1
[by] => 1
[Chris] => 1
[who] => 2
[declined] => 1
[record] => 1
[or] => 1
[make] => 1
[his] => 2
[spouse] => 1
[available] => 1
[one] => 1
[people] => 1
[appear] => 1
[Epstein] => 1
[but] => 1
[not] => 1
[previously] => 1
[which] => 1
[dates] => 1
)