php-ml/src/Dataset/FilesDataset.php

37 lines
877 B
PHP
Raw Normal View History

2016-07-16 21:29:40 +00:00
<?php
2016-11-20 21:53:17 +00:00
declare(strict_types=1);
2016-07-16 21:29:40 +00:00
namespace Phpml\Dataset;
use Phpml\Exception\DatasetException;
class FilesDataset extends ArrayDataset
{
public function __construct(string $rootPath)
{
if (!is_dir($rootPath)) {
throw new DatasetException(sprintf('Dataset root folder "%s" missing.', $rootPath));
2016-07-16 21:29:40 +00:00
}
$this->scanRootPath($rootPath);
}
private function scanRootPath(string $rootPath): void
2016-07-16 21:29:40 +00:00
{
foreach (glob($rootPath.DIRECTORY_SEPARATOR.'*', GLOB_ONLYDIR) as $dir) {
2016-07-16 21:29:40 +00:00
$this->scanDir($dir);
}
}
private function scanDir(string $dir): void
2016-07-16 21:29:40 +00:00
{
$target = basename($dir);
foreach (array_filter(glob($dir.DIRECTORY_SEPARATOR.'*'), 'is_file') as $file) {
2016-07-16 21:29:40 +00:00
$this->samples[] = [file_get_contents($file)];
$this->targets[] = $target;
}
}
}