Skip to content

Commit 8af5134

Browse files
jtejidoangeloskath
authored andcommitted
Optimize FreqDist::getHapaxes()
1 parent b02175a commit 8af5134

File tree

1 file changed

+6
-21
lines changed

1 file changed

+6
-21
lines changed

src/NlpTools/Analysis/FreqDist.php

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -106,28 +106,13 @@ public function getKeyValues()
106106
*/
107107
public function getHapaxes()
108108
{
109-
$hapaxes = array();
110-
111-
//get the head key
112-
$head = key($this->keyValues);
113-
114-
//get the tail value,. set the internal pointer to the tail
115-
$tail = end($this->keyValues);
116-
// no hapaxes available
117-
if ($tail > 1) {
118-
return array();
109+
$samples = array();
110+
foreach ($this->getKeyValues() as $sample => $count) {
111+
if ($count == 1) {
112+
$samples[] = $sample;
119113
}
120-
121-
do {
122-
$hapaxes[] = key($this->keyValues);
123-
prev($this->keyValues);
124-
125-
} while (current($this->keyValues) == 1 && key($this->keyValues) !== $head);
126-
127-
//reset the internal pointer in the array
128-
reset($this->keyValues);
129-
130-
return $hapaxes;
114+
}
115+
return $samples;
131116
}
132117

133118
}

0 commit comments

Comments
 (0)