php-ml/src/Dataset/FilesDataset.php

48 lines
1.1 KiB
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
{
2019-05-14 20:43:08 +00:00
$dirs = glob($rootPath.DIRECTORY_SEPARATOR.'*', GLOB_ONLYDIR);
if ($dirs === false) {
throw new DatasetException(sprintf('An error occurred during directory "%s" scan', $rootPath));
}
foreach ($dirs 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);
2019-05-14 20:43:08 +00:00
$files = glob($dir.DIRECTORY_SEPARATOR.'*');
if ($files === false) {
return;
}
foreach (array_filter($files, 'is_file') as $file) {
$this->samples[] = file_get_contents($file);
2016-07-16 21:29:40 +00:00
$this->targets[] = $target;
}
}
}