forked from jorgecasas/php-ml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FilesDataset.php
47 lines (39 loc) · 1005 Bytes
/
FilesDataset.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
declare(strict_types=1);
namespace Phpml\Dataset;
use Phpml\Exception\DatasetException;
class FilesDataset extends ArrayDataset
{
/**
* @param string $rootPath
*
* @throws DatasetException
*/
public function __construct(string $rootPath)
{
if (!is_dir($rootPath)) {
throw DatasetException::missingFolder($rootPath);
}
$this->scanRootPath($rootPath);
}
/**
* @param string $rootPath
*/
private function scanRootPath(string $rootPath)
{
foreach (glob($rootPath.DIRECTORY_SEPARATOR.'*', GLOB_ONLYDIR) as $dir) {
$this->scanDir($dir);
}
}
/**
* @param string $dir
*/
private function scanDir(string $dir)
{
$target = basename($dir);
foreach (array_filter(glob($dir.DIRECTORY_SEPARATOR.'*'), 'is_file') as $file) {
$this->samples[] = [file_get_contents($file)];
$this->targets[] = $target;
}
}
}