$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); return explode(' ', $line); } private static function parseTargetColumn(string $column): float { if (!is_numeric($column)) { throw new DatasetException(sprintf('Invalid target "%s".', $column)); } return (float) $column; } private static function parseFeatureColumn(string $column): array { $feature = explode(':', $column, 2); if (count($feature) !== 2) { throw new DatasetException(sprintf('Invalid value "%s".', $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 new DatasetException(sprintf('Invalid index "%s".', $index)); } if ((int) $index < 1) { throw new DatasetException(sprintf('Invalid index "%s".', $index)); } return (int) $index - 1; } private static function parseFeatureValue(string $value): float { if (!is_numeric($value)) { throw new DatasetException(sprintf('Invalid value "%s".', $value)); } return (float) $value; } }