function print_hash_table(&$hash, $title = "") { $total = get_total($hash); $unique = get_unique($hash); $hapax = get_hapax($hash); // start the csv $csv = "{$title},{$total},{$unique},{$hapax}\n"; $csv = "{$csv}RANK,WORD,COUNT,FREQUENCY\n"; // print table header ?> <div class="results"> <table class="resultstable"> <tr> <th>Rank</th> <th>Word</th> <th>Count</th> <th>Frequency</th> </tr> <?php $color = 1; $rank = 0; $allrank = 1; $prevcount; // iterate through the list of word printing them out foreach ($hash as $word => $count) { $prop = round($count / $total, 10); if ($count != $prevcount) { $rank = $allrank; } $prevcount = $count; $allrank++; $rowclass = $color % 2 ? "oddrow" : "evenrow"; $color++; ?> <tr class="<?php echo $rowclass; ?> "> <td><?php echo $rank; ?> </td> <td class="intext"><?php echo $word; ?> </td> <td><?php echo $count; ?> </td> <td><?php echo $prop; ?> </td> </tr> <?php // update csv $csv = "{$csv}{$rank},{$word},{$count},{$prop}\n"; } ?> </table> </div> <?php // return the csv return $csv; }
function get_counts(&$hash) { $counts['unique'] = get_unique($hash); $counts['total'] = get_total($hash); $counts['hapax'] = get_hapax($hash); return $counts; }
private function write_csv(&$hash, $end, $style = null) { $DELIM = "\t"; //$file = $this->folder . "/$style/csv/" . $this->id . "_$end.csv"; $file = $this->folder . "/tsv/" . $this->id . "_{$end}.tsv"; $total = get_total($hash); $unique = get_unique($hash); $hapax = get_hapax($hash); hash_sort($hash, 'c'); $csvarr = array(); // start the csv $csvarr[] = array($this->id . "_{$end}.txt", $total, $unique, $hapax); $csvarr[] = array("RANK", "WORD", "COUNT", "RELATIVE FREQUENCY"); $rank = 0; $allrank = 1; $prevcount = null; // iterate through the list of word printing them out foreach ($hash as $word => $count) { $prop = round($count / $total, 10); if ($count != $prevcount) { $rank = $allrank; } $prevcount = $count; $allrank++; // update csv $csvarr[] = array($rank, utf8_decode($word), $count, $prop); } // write out file $FH = fopen($file, 'w'); if (!$FH) { return "Could not open file '{$file}' in mode 'c+'."; } foreach ($csvarr as $line) { if (!fputcsv($FH, $line, "{$DELIM}")) { return "Could not write " . implode(",", $line) . " to file '{$file}'."; } } fclose($FH); return null; }