git init
This commit is contained in:
30
app/Services/CsvReader.php
Normal file
30
app/Services/CsvReader.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
class CsvReader {
|
||||
private $file;
|
||||
private array $headers;
|
||||
|
||||
public array $lines = [];
|
||||
|
||||
public function __construct(
|
||||
public string $filename,
|
||||
)
|
||||
{
|
||||
$this->file = fopen(public_path($filename), "r");
|
||||
if (!$this->file) {
|
||||
throw new \RuntimeException("Failed to open file: " . $filename);
|
||||
}
|
||||
|
||||
$this->headers = $this->readNextLine();
|
||||
}
|
||||
|
||||
public function readNextLine(): ?array
|
||||
{
|
||||
if (($data = fgetcsv($this->file, 1000, ",")) !== FALSE) {
|
||||
return $data;
|
||||
}
|
||||
return null; // End of file or error
|
||||
}
|
||||
}
|
||||
49
app/Services/DataSetReader.php
Normal file
49
app/Services/DataSetReader.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
class DataSetReader {
|
||||
public array $lines = [];
|
||||
private array $currentLines = [];
|
||||
|
||||
private int $lastReadLineIndex = -1;
|
||||
|
||||
public function __construct(
|
||||
public string $filename,
|
||||
) {
|
||||
// For now, we only support CSV files, so we can delegate to CsvReader
|
||||
$csvReader = new CsvReader($filename);
|
||||
$this->readEntireFile($csvReader);
|
||||
$this->reset();
|
||||
}
|
||||
|
||||
private function readEntireFile(CsvReader $reader): void
|
||||
{
|
||||
while ($line = $reader->readNextLine()) {
|
||||
$this->lines[] = $line;
|
||||
}
|
||||
}
|
||||
|
||||
public function getRandomLine(): array | null
|
||||
{
|
||||
if (empty($this->currentLines)) {
|
||||
return null; // No more lines to read
|
||||
}
|
||||
$randomNumber = array_rand($this->currentLines);
|
||||
$randomLine = $this->currentLines[$randomNumber];
|
||||
// Remove the line from the current lines to avoid repetition
|
||||
unset($this->currentLines[$randomNumber]);
|
||||
$this->lastReadLineIndex = array_search($randomLine, $this->lines, true);
|
||||
return $randomLine;
|
||||
}
|
||||
|
||||
public function reset(): void
|
||||
{
|
||||
$this->currentLines = $this->lines;
|
||||
}
|
||||
|
||||
public function getLastReadLineIndex(): int
|
||||
{
|
||||
return $this->lastReadLineIndex;
|
||||
}
|
||||
}
|
||||
7
app/Services/ISynapticWeights.php
Normal file
7
app/Services/ISynapticWeights.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
interface ISynapticWeights {
|
||||
public function generate(int $input_size): array;
|
||||
}
|
||||
14
app/Services/RandomSynapticWeights.php
Normal file
14
app/Services/RandomSynapticWeights.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
class RandomSynapticWeights implements ISynapticWeights {
|
||||
public function generate(int $input_size): array
|
||||
{
|
||||
$weights = [];
|
||||
for ($i = 0; $i < $input_size + 1; $i++) { // +1 for bias weight
|
||||
$weights[] = rand(-100, 100) / 100; // Random weights between -1 and 1
|
||||
}
|
||||
return $weights;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user