From b826dbf26467f00c7f196789258c3232dc40af31 Mon Sep 17 00:00:00 2001 From: stephenmk Date: Mon, 30 Jan 2023 13:26:26 -0600 Subject: [PATCH] Add verification logic for date entry in JMdict Very old versions of JMdict and unofficial versions are unlikely to have the publication date entry at the end of the file. --- jmdict.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/jmdict.go b/jmdict.go index ceb5835..5abbcbf 100644 --- a/jmdict.go +++ b/jmdict.go @@ -63,10 +63,20 @@ func doDisplaySenseNumberTag(headword headword, entry jmdict.JmdictEntry, meta j } func jmdictPublicationDate(dictionary jmdict.Jmdict) string { + if len(dictionary.Entries) == 0 { + return "unknown" + } dateEntry := dictionary.Entries[len(dictionary.Entries)-1] + if len(dateEntry.Sense) == 0 || len(dateEntry.Sense[0].Glossary) == 0 { + return "unknown" + } r := regexp.MustCompile(`\d{4}-\d{2}-\d{2}`) jmdictDate := r.FindString(dateEntry.Sense[0].Glossary[0].Content) - return jmdictDate + if jmdictDate != "" { + return jmdictDate + } else { + return "unknown" + } } func createFormsTerm(headword headword, entry jmdict.JmdictEntry, meta jmdictMetadata) (dbTerm, bool) {