merging arrays and word frequency
So I'm cycling through a document with 41 paragraphs. For each paragraph I'm trying to [1] first break the string into an array, and then get the word frequency of the paragraph. I then want to combine the data from all paragraphs and get the word frequency of the whole document.
I'm able to get array that gives me the "word" and its "frequency" for a given pargraph but I'm having trouble merging the results from each paragraph so as to get the "word frequency of the whole document. Here is what I have:
function sectionWordFrequency($sectionFS)
{
$section_frequency = array();
$filename = $sectionFS . ".xml";
$xmldoc = simplexml_load_file('../../editedtranscriptions/' . $filename);
$xmldoc->registerXPathNamespace("tei", "http://www.tei-c.org/ns/1.0");
$paraArray = $xmldoc->xpath("//tei:p");
foreach ($paraArray as $p)
{
$para_frequency = (array_count_values(str_word_count(strtolower($p), 1)));
$section_frequency[] = $para_frequency;
}
return array_merge($section_frequency);
}
/// now I call the function, sort it, and try to display it
$section_frequency = sectionWordFrequency($fs);
ksort($section_frequency);
foreach ($section_frequency as $word=>$frequency)
{
echo $word . ": " . $frequency . "开发者_如何学运维</br>";
}
Right now the result I get is:
1: Array 2: Array 3: Array 4: Array
Any help is greatly appreciated.
Try to replace this line
$section_frequency[] = $para_frequency;
with this
$section_frequency = array_merge($section_frequency, $para_frequency);
and then
return $section_frequency
精彩评论