Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/services/dictionary_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:archive/archive_io.dart' as archive;
import 'package:sagase/datamodels/user_backup.dart';
import 'package:sagase/services/isar_service.dart';
import 'package:sagase/utils/constants.dart' as constants;
import 'package:sagase/utils/enum_utils.dart';
import 'package:sagase_dictionary/sagase_dictionary.dart';
import 'package:path/path.dart' as path;

Expand Down Expand Up @@ -188,7 +189,7 @@ class DictionaryService {

if (list.length == 1) {
return list;
} else if (list.length > 1) {
} else if (list.isNotEmpty) {
results = list;
}
}
Expand Down
74 changes: 69 additions & 5 deletions lib/services/mecab_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,24 +133,39 @@ class MecabService {
PartOfSpeech? pos;
if (tokens[i].features[0] == '助詞') {
pos = PartOfSpeech.particle;
} else if (tokens[i].features[4] == 'サ変・スル') {
pos = PartOfSpeech.verbSuruIncluded;
} else if (tokens[i].features[0] == '動詞') {
pos = _identifyVerb(tokens[i].features);
} else if (tokens[i].features[1] == '固有名詞' &&
tokens[i].features[2] == '人名') {
pos = PartOfSpeech.nounProper;
}

// Handle corner cases where the base form (index 6) is different than the real base form
// Handle corner cases where the base or base reading form is different than the real form
String base = tokens[i].features[6];
if (tokens[i].surface == 'なら' && tokens[i].features[6] == 'だ') {
base = 'なら';
}
String baseReading = tokens[i].features[7];
if (baseReading.endsWith('ッ') &&
tokens[i].features[5] == '連用タ接続' &&
tokens.length > i + 1) {
switch (tokens[i + 1].features[6]) {
case 'て':
baseReading =
'${baseReading.substring(0, baseReading.length - 1)}ク';
break;
case 'で':
baseReading =
'${baseReading.substring(0, baseReading.length - 1)}グ';
break;
}
}

// Created token to add to list or trailing of previous token
final current = JapaneseTextToken(
original: tokens[i].surface,
base: base,
baseReading: tokens[i].features[7],
baseReading: baseReading,
rubyTextPairs: createRubyTextPairs(
tokens[i].surface,
tokens[i].features[7],
Expand All @@ -159,7 +174,12 @@ class MecabService {
);

// Check if the current token should be trailing of previous token
if (list.isNotEmpty && list.last.pos != PartOfSpeech.particle) {
if (list.isNotEmpty) {
if (tokens[i - 1].features[5] == '連用タ接続') {
list.last.trailing ??= [];
list.last.trailing!.add(current);
continue;
} else if (list.last.pos != PartOfSpeech.particle) {
if (tokens[i].features[1] == '接続助詞' && tokens[i].features[6] == 'て') {
list.last.trailing ??= [];
list.last.trailing!.add(current);
Expand All @@ -180,6 +200,7 @@ class MecabService {
list.last.trailing ??= [];
list.last.trailing!.add(current);
continue;
}
}
}

Expand All @@ -189,6 +210,49 @@ class MecabService {
return list;
}

PartOfSpeech? _identifyVerb(List<String> features) {
switch (features[4]) {
case 'サ変・スル':
return PartOfSpeech.verbSuruIncluded;
case '一段':
return PartOfSpeech.verbIchidan;
case '一段・クレル':
return PartOfSpeech.verbIchidanS;
// case '':
// return PartOfSpeech.verbGodanAru;
case '五段・ラ行':
if (features[6] == 'ある' || features[6] == '有る' || features[6] == 'アル') {
return PartOfSpeech.verbGodanRI;
} else {
return PartOfSpeech.verbGodanR;
}
case '五段・バ行':
return PartOfSpeech.verbGodanB;
case '五段・ガ行':
return PartOfSpeech.verbGodanG;
case '五段・カ行促音便':
return PartOfSpeech.verbGodanKS;
case '五段・カ行イ音便':
return PartOfSpeech.verbGodanK;
case '五段・マ行':
return PartOfSpeech.verbGodanM;
case '五段・ナ行':
return PartOfSpeech.verbGodanN;
case '五段・サ行':
return PartOfSpeech.verbGodanS;
case '五段・タ行':
return PartOfSpeech.verbGodanT;
case '五段・ワ行促音便':
return PartOfSpeech.verbGodanU;
// case '':
// return PartOfSpeech.verbGodanUS;
// case '':
// return PartOfSpeech.verbGodanUru;
default:
return null;
}
}

List<RubyTextPair> createRubyTextPairs(String writing, String reading) {
// First check if only kana
if (_kanaKit.isKana(writing)) return [RubyTextPair(writing: writing)];
Expand Down