Setup LexcialEntry import

This commit is contained in:
Leonard Smith 2020-10-06 22:03:59 -05:00
parent 24b4407fe1
commit b5f067e185
4 changed files with 153 additions and 29 deletions

View File

@ -0,0 +1,47 @@
<?php
namespace App\Console\Commands;
use App\Handlers\LexiconEntryImportHandler;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class ImportLexicalEntries extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'gwt:import-lexical-entries';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Import Lexical Entries from the Greek Words for Translators repo';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
DB::table('lexical_entries')->truncate();
$importHandler = new LexiconEntryImportHandler();
$importHandler->run();
}
}

View File

@ -0,0 +1,65 @@
<?php
namespace App\Handlers;
use App\LexicalEntry;
/**
* LexiconEntryImportHandler.php
*
* @author: Leonard Smith <leonard@acornwebconsultants.com>
* Date: 10/6/20
* Time: 8:28 PM
*/
class LexiconEntryImportHandler
{
use LexiconEntryTrait;
const REPO_FOLDER = '/gwt/';
public function run() : void
{
// Iterate over each folder in the repo
$handle = opendir($this->getFolder());
while (($dir = readdir($handle)) !== false) {
if (is_dir($this->getFolder() . $dir)) {
$this->scanLexiconDirectory($this->getFolder() . $dir);
}
// Silently ignoring files at this level
}
closedir($handle);
}
protected function scanLexiconDirectory(string $dir) : void
{
$handle = opendir($dir);
while (($item = readdir($handle)) !== false) {
if ((substr($item, 0, 1) === 'g') && is_file($dir . '/' . $item)) {
$parts = explode('.', substr($item, 1));
$this->importEntryFile($dir . '/' . $item, $parts[0]);
}
}
closedir($handle);
}
protected function importEntryFile(string $file, string $stongsNumber) : LexicalEntry
{
$contents = file_get_contents($file);
list($lemma, $commentary) = $this->parseEntry($contents);
$entry = LexicalEntry::create([
'id' => $stongsNumber,
'lemma' => $lemma,
'commentary' => $commentary,
]);
return $entry;
}
protected function getFolder() : string
{
return storage_path() . self::REPO_FOLDER;
}
}

View File

@ -0,0 +1,38 @@
<?php
namespace App\Handlers;
/**
* LexiconEntryTrait.php
*
* @author: Leonard Smith <leonard@acornwebconsultants.com>
* Date: 10/6/20
* Time: 8:34 PM
*/
trait LexiconEntryTrait
{
/**
* Extract the lexeme and the commentary portion into separate variables
*
* @param $entry
* @return array
*/
public function parseEntry($entry)
{
$lexeme = '';
$commentary = '';
$n = 0;
foreach(preg_split('~[\r\n]+~', $entry) as $line){
if(empty($line) or ctype_space($line)) continue; // skip only spaces
if ($n === 0) {
$lexeme = ltrim($line, '#');
} else {
$commentary .= $line . "\n";
}
$n++;
}
return [$lexeme, $commentary];
}
}

View File

@ -11,7 +11,7 @@ use Illuminate\Support\Facades\URL;
*/
class LexiconHandler
{
use BookXmlFilesTrait;
use BookXmlFilesTrait, LexiconEntryTrait;
public function getEntriesJson($book, $chapter, $verse)
{
@ -33,14 +33,14 @@ class LexiconHandler
public function formatForJson($id, $entry)
{
list($lexeme, $commentary) = $this->parseEntry($entry);
list($lemma, $commentary) = $this->parseEntry($entry);
return [
'type' => 'lexical-entries',
'id' => $id,
'attributes' => [
'strongs-number' => $id,
'lexeme' => $lexeme,
'lemma' => $lemma,
'commentary' => $commentary,
],
'links' => [
@ -49,32 +49,6 @@ class LexiconHandler
];
}
/**
* Extract the lexeme and the commentary portion into separate variables
*
* @param $entry
* @return array
*/
public function parseEntry($entry)
{
$lexeme = '';
$commentary = '';
$n = 0;
foreach(preg_split('~[\r\n]+~', $entry) as $line){
if(empty($line) or ctype_space($line)) continue; // skip only spaces
if ($n === 0) {
$lexeme = ltrim($line, '#');
} else {
$commentary .= $line . "\n";
}
$n++;
}
return [$lexeme, $commentary];
}
public function getEntriesByVerse($book, $chapter, $verse)
{
$xmlFile = $this->getBookXmlFile('/ulb/', $book);