Skip to content
This repository has been archived by the owner on Jul 16, 2022. It is now read-only.

Commit

Permalink
Pdic: use pdic4j library@0.3.2
Browse files Browse the repository at this point in the history
Signed-off-by: Hiroshi Miura <miurahr@linux.com>
  • Loading branch information
miurahr committed Jan 9, 2022
1 parent f3cdd6e commit e5c9844
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 1,243 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ dependencies {
implementation("tokyo.northside:url-protocol-handler:0.1.4")

// for pdic
implementation("com.ibm.icu:icu4j-charset:70.1")
implementation("io.github.eb4j:pdic4j:0.3.2")

// for stardict
implementation("io.github.dictzip:dictzip:0.9.5")
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/github/eb4j/ebview/dictionary/PDic.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.eb4j.ebview.dictionary;

import io.github.eb4j.ebview.data.IDictionary;
import io.github.eb4j.ebview.dictionary.pdic.PdicDictionary;
import io.github.eb4j.ebview.dictionary.pdic.PdicDict;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -35,7 +35,7 @@ public boolean isSupportedFile(final File file) {
public Set<IDictionary> loadDict(final File file) {
Set<IDictionary> result = new HashSet<>();
try {
IDictionary dictionary = new PdicDictionary(file);
IDictionary dictionary = new PdicDict(file);
result.add(dictionary);
} catch (IOException e) {
e.printStackTrace();
Expand Down
95 changes: 95 additions & 0 deletions src/main/java/io/github/eb4j/ebview/dictionary/pdic/PdicDict.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package io.github.eb4j.ebview.dictionary.pdic;

import io.github.eb4j.ebview.data.DictionaryEntry;
import io.github.eb4j.ebview.data.IDictionary;
import io.github.eb4j.pdic.PdicDictionary;
import io.github.eb4j.pdic.PdicElement;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
* @author wak (Apache-2.0)
* @author Hiroshi Miura
*/
public class PdicDict implements IDictionary {

private final PdicDictionary dict;
private final Locale sourceLocale;
private final String dictionaryName;

/**
* Construct with .dic file.
* It create index cache file with name .dic.idx.
*
* @param file PDIC .dic file.
* @throws IOException when access error occurred.
*/
public PdicDict(final File file) throws IOException {
File cache = new File(file.getPath() + ".idx");
sourceLocale = Locale.ROOT;
this.dict = PdicDictionary.loadDictionary(file, cache);
dictionaryName = file.getName();
}

@Override
public String getDictionaryName() {
return dictionaryName;
}

/**
* Read article's text.
*
* @param word The word to look up in the dictionary
* @return List of entries. May be empty, but cannot be null.
*/
@Override
public List<DictionaryEntry> readArticles(final String word) throws IOException {
return makeDictionaryEntries(dict.getEntries(word.toLowerCase(sourceLocale)));
}

/**
* Read article's text. Matching is predictive, so e.g. supplying "term"
* will return articles for "term", "terminology", "termite", etc.
*
* @param word The word to look up in the dictionary
* @return List of entries. May be empty, but cannot be null.
*/
@Override
public List<DictionaryEntry> readArticlesPredictive(final String word) throws IOException {
return makeDictionaryEntries(dict.getEntriesPredictive(word.toLowerCase(sourceLocale)));
}

/**
* Dispose IDictionary. Default is no action.
*/
@Override
public void close() {
}

private List<DictionaryEntry> makeDictionaryEntries(final List<PdicElement> results) {
List<DictionaryEntry> lists = new ArrayList<>();
for (PdicElement result : results) {
String word = result.getHeadWord();
if (word.equals("")) {
word = result.getIndexWord();
}
StringBuilder articleBuilder = new StringBuilder();
String pronunciation = result.getPronunciation();
if (pronunciation != null) {
articleBuilder.append(pronunciation).append(" / ");
}
articleBuilder.append(result.getTranslation()).append("<br/>");
String example = result.getExample();
if (example != null) {
articleBuilder.append(example);
}
lists.add(new DictionaryEntry(word, articleBuilder.toString(), dictionaryName));
}
return lists;
}

}

This file was deleted.

This file was deleted.

111 changes: 0 additions & 111 deletions src/main/java/io/github/eb4j/ebview/dictionary/pdic/PdicHeader.java

This file was deleted.

Loading

0 comments on commit e5c9844

Please sign in to comment.