2016-07-16 21:29:40 +00:00
|
|
|
<?php
|
2016-07-16 21:56:52 +00:00
|
|
|
|
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)) {
|
2018-03-03 15:03:53 +00:00
|
|
|
throw new DatasetException(sprintf('Dataset root folder "%s" missing.', $rootPath));
|
2016-07-16 21:29:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->scanRootPath($rootPath);
|
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
private function scanRootPath(string $rootPath): void
|
2016-07-16 21:29:40 +00:00
|
|
|
{
|
2016-07-16 21:56:52 +00:00
|
|
|
foreach (glob($rootPath.DIRECTORY_SEPARATOR.'*', GLOB_ONLYDIR) as $dir) {
|
2016-07-16 21:29:40 +00:00
|
|
|
$this->scanDir($dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
private function scanDir(string $dir): void
|
2016-07-16 21:29:40 +00:00
|
|
|
{
|
|
|
|
$target = basename($dir);
|
|
|
|
|
2016-07-16 21:56:52 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|