$maxIndex) { $maxIndex = $index; $sample = array_pad($sample, $maxIndex + 1, 0); } $sample[$index] = $value; } return [$sample, $target, $maxIndex]; } private static function parseLine(string $line): array { $line = explode('#', $line, 2)[0]; $line = rtrim($line); $line = str_replace("\t", ' ', $line); $columns = explode(' ', $line); return $columns; } private static function parseTargetColumn(string $column): float { if (!is_numeric($column)) { throw DatasetException::invalidTarget($column); } return (float) $column; } private static function parseFeatureColumn(string $column): array { $feature = explode(':', $column, 2); if (count($feature) != 2) { throw DatasetException::invalidValue($column); } $index = self::parseFeatureIndex($feature[0]); $value = self::parseFeatureValue($feature[1]); return [$index, $value]; } private static function parseFeatureIndex(string $index): int { if (!is_numeric($index) || !ctype_digit($index)) { throw DatasetException::invalidIndex($index); } if ((int) $index < 1) { throw DatasetException::invalidIndex($index); } return (int) $index - 1; } private static function parseFeatureValue(string $value): float { if (!is_numeric($value)) { throw DatasetException::invalidValue($value); } return (float) $value; } }