Skip to content

Commit

Permalink
Replaced jmdict files with a single .zip file
Browse files Browse the repository at this point in the history
  • Loading branch information
simjanos-dev committed May 24, 2024
1 parent 55036c9 commit a8faf48
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 16 deletions.
49 changes: 36 additions & 13 deletions app/Services/DictionaryImportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,29 @@
use App\Models\VocabularyJmdictReading;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;
use League\Csv\Reader;

class DictionaryImportService {

public function __construct() {
}

private function deleteTempDictionaryFiles() {
$tempDictionaryFiles = Storage::allFiles('temp/dictionaries');
Storage::delete($tempDictionaryFiles);
}

/*
Scans the /storage/app/dictionaries folder,
and returns a list of importable dictionaries.
*/
public function getImportableDictionaryList($supportedSourceLanguages, $dictCcLanguageCodes, $databaseLanguageCodes) {
$dictionariesFound = [];
$files = Storage::files('dictionaries');

// jmdict dictionary
if (Storage::exists('dictionaries/jmdict_processed.txt') &&
Storage::exists('dictionaries/kanjidic2.xml') &&
Storage::exists('dictionaries/radical-strokes.txt') &&
Storage::exists('dictionaries/radicals.txt')) {

if (Storage::exists('dictionaries/jmdict.zip')) {
$dictionary = new \stdClass();
$dictionary->name = 'JMDict';
$dictionary->databaseName = 'dict_jp_jmdict';
Expand All @@ -44,7 +46,7 @@ public function getImportableDictionaryList($supportedSourceLanguages, $dictCcLa
$dictionary->expectedRecordCount = 207690;
$dictionary->firstUpdateInterval = 25000;
$dictionary->updateInterval = 10000;
$dictionary->fileName = 'multiple files';
$dictionary->fileName = 'jmdict.zip';
$dictionary->imported = false;

// check if jmdict is imported
Expand Down Expand Up @@ -675,7 +677,7 @@ public function importWiktionary($name, $language, $fileName, $databaseName) {
public function kanjiRadicalImport() {
// init
DB::beginTransaction();
$file = fopen(base_path() . '/storage/app/dictionaries/radicals.txt', 'r');
$file = fopen(base_path() . '/storage/app/temp/dictionaries/radicals.txt', 'r');
$index = 0;

// these kanjis has to be replaced with radicals
Expand Down Expand Up @@ -710,7 +712,7 @@ public function kanjiRadicalImport() {
DB::statement('DELETE FROM dict_jp_kanji_radicals');

// load radical stroke counts into an array
$radicalStrokesFiles = fopen(base_path() . '/storage/app/dictionaries/radical-strokes.txt', 'r');
$radicalStrokesFiles = fopen(base_path() . '/storage/app/temp/dictionaries/radical-strokes.txt', 'r');
$radicalStrokeCountsData = [];

while (($line = fgets($radicalStrokesFiles)) !== false) {
Expand Down Expand Up @@ -759,6 +761,9 @@ public function kanjiRadicalImport() {

// finish
DB::commit();

// delete temp files
$this->deleteTempDictionaryFiles();
}

/*
Expand All @@ -776,7 +781,7 @@ public function kanjiImport() {

$doc = new \DOMDocument();
$reader = new \XMLReader();
$reader->open(base_path() . '/storage/app/dictionaries/kanjidic2.xml');
$reader->open(base_path() . '/storage/app/temp/dictionaries/kanjidic2.xml');
$index = 0;

DB::beginTransaction();
Expand Down Expand Up @@ -867,11 +872,29 @@ public function kanjiImport() {
Imports jmdict dictionary file.
*/
public function jmdictImport() {
$file = fopen(base_path() . '/storage/app/dictionaries/jmdict_processed.txt', 'r');
DB::statement('DELETE FROM dict_jp_jmdict');
DB::statement('DELETE FROM dict_jp_jmdict_words');
DB::statement('DELETE FROM dict_jp_jmdict_readings');

// extract zip file
$filePath = Storage::path('dictionaries/jmdict.zip');
$extractPath = Storage::path('temp/dictionaries');

// delete all temp dictionary files
$this->deleteTempDictionaryFiles();

// extract jmdict.zip file
$zip = new \ZipArchive();
$zipFile = $zip->open($filePath);
if ($zipFile === TRUE) {
$zip->extractTo($extractPath);
$zip->close();
} else {
throw new \Exception('JMDict zip file could not be extracted.');
}

// import jmdict file
$file = fopen(base_path() . '/storage/app/temp/dictionaries/jmdict_processed.txt', 'r');
$index = 0;
DB::beginTransaction();
while (($line = fgets($file)) !== false) {
Expand Down Expand Up @@ -934,10 +957,10 @@ public function jmdictImport() {
Converts jmdict to text. Should be moved to python.
*/
public function jmdictXmlToText() {
$file = fopen(base_path() . '/storage/app/dictionaries/jmdict.txt', 'w');
$file = fopen(base_path() . '/storage/app/temp/dictionaries/jmdict.txt', 'w');
$doc = new \DOMDocument();
$reader = new \XMLReader();
$reader->open(base_path() . '/storage/app/dictionaries/JMdict_e.xml');
$reader->open(base_path() . '/storage/app/temp/dictionaries/JMdict_e.xml');
$index = 0;


Expand Down
2 changes: 1 addition & 1 deletion entrypoint-dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ folder_paths="
./storage/app/fonts
./storage/app/images/book_images
./storage/app/public
./storage/app/temp
./storage/app/temp/dictionaries
./storage/framework/cache/data
./storage/framework/sessions
./storage/framework/testing
Expand Down
2 changes: 1 addition & 1 deletion entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ folder_paths="
./storage/app/fonts
./storage/app/images/book_images
./storage/app/public
./storage/app/temp
./storage/app/temp/dictionaries
./storage/framework/cache/data
./storage/framework/sessions
./storage/framework/testing
Expand Down
1 change: 1 addition & 0 deletions resources/js/components/Admin/AdminDictionarySettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
:items="dictionaries"
:loading="loading"
:search="dictionaryTableFilter"
:items-per-page="9"
>

<!-- Source language -->
Expand Down
5 changes: 4 additions & 1 deletion resources/js/components/Home/PatchNotes.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<b>Bug fixes:</b>
<ul>
<li>
Review page displayed no text after the user finished reviewing, and it only had one card in total.
Review page displayed no text after the user finished reviewing if it only had one card in total.
</li>
<li>
Fixed and enabled Thai import options.
Expand All @@ -39,6 +39,9 @@
<li>
Hover vocabulary's dictionary search results have been limited to 9 instead of 15. I'll add a setting for this in the future.
</li>
<li>
JMDict dictionary files have been replaced with a single .zip file that contains them.
</li>
</ul>
</v-card-text>
</v-card>
Expand Down

0 comments on commit a8faf48

Please sign in to comment.