Skip to content

Commit

Permalink
Update queryWord methods
Browse files Browse the repository at this point in the history
According database schema changing, words table unique(word_spell)
changed to unique(word_spell, word_source).
  • Loading branch information
Kunkgg committed Nov 16, 2020
1 parent 147af8a commit a07ab1a
Show file tree
Hide file tree
Showing 23 changed files with 355 additions and 81 deletions.
Binary file modified bin/main/kunDict/App.class
Binary file not shown.
Binary file modified bin/main/kunDict/CollinsOnlineDict.class
Binary file not shown.
Binary file modified bin/main/kunDict/DefaultLocalDict.class
Binary file not shown.
Binary file modified bin/main/kunDict/Dict.class
Binary file not shown.
Binary file modified bin/main/kunDict/DictType$1.class
Binary file not shown.
Binary file modified bin/main/kunDict/DictType$2.class
Binary file not shown.
Binary file modified bin/main/kunDict/DictType.class
Binary file not shown.
Binary file modified bin/main/kunDict/Extractor.class
Binary file not shown.
Binary file modified bin/main/kunDict/LocalDict.class
Binary file not shown.
Binary file added bin/main/kunDict/LongmanOnlineDict.class
Binary file not shown.
Binary file modified bin/main/kunDict/MITDict.class
Binary file not shown.
Binary file modified bin/main/kunDict/OnlineDict.class
Binary file not shown.
Binary file modified bin/main/kunDict/SQLStr.class
Binary file not shown.
Binary file modified bin/test/kunDict/AppTest.class
Binary file not shown.
127 changes: 92 additions & 35 deletions src/main/java/kunDict/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public App() throws IOException, SQLException {

// load configs {{{ //

private void loadConfigs() throws FileNotFoundException,
IOException, InvalidPropertiesFormatException {
private void loadConfigs() throws FileNotFoundException, IOException,
InvalidPropertiesFormatException {
App.configs = new Properties();
FileInputStream fis = new FileInputStream(this.configFileName);
App.configs.load(fis);
Expand Down Expand Up @@ -96,22 +96,22 @@ public void setConfigFileName(String configFileName) {

public ArrayList<Dict> getRegisteredDicts() {
ArrayList<Dict> dicts = new ArrayList<>();
for(LocalDict localDict : this.registeredLocalDicts) {
if(localDict instanceof Dict) {
for (LocalDict localDict : this.registeredLocalDicts) {
if (localDict instanceof Dict) {
dicts.add((Dict) localDict);
}
}

for(OnlineDict onlineDict : this.registeredOnlineDicts) {
if(onlineDict instanceof Dict) {
for (OnlineDict onlineDict : this.registeredOnlineDicts) {
if (onlineDict instanceof Dict) {
dicts.add((Dict) onlineDict);
}
}

return dicts;
}

public ArrayList<LocalDict> getRegisteredLocalDicts(){
public ArrayList<LocalDict> getRegisteredLocalDicts() {
return this.registeredLocalDicts;
}

Expand Down Expand Up @@ -195,11 +195,13 @@ public void insertValuesIntoDictTypes() throws SQLException {
public void registerDicts() throws IOException, SQLException {
DefaultLocalDict defaultDict = new DefaultLocalDict();
CollinsOnlineDict collinsDict = new CollinsOnlineDict();
LongmanOnlineDict longmanDict = new LongmanOnlineDict();

this.clearRegisteredDicts();

this.registerDict(defaultDict);
this.registerDict(collinsDict);
this.registerDict(longmanDict);
}

public Word queryWordByFirst(String wordSpell) throws SQLException {
Expand All @@ -212,35 +214,70 @@ public Word queryWordByFirst(String wordSpell) throws SQLException {
+ " registered dictionarys");
Utils.debug("RegisteredDicts: " + registeredDicts);

for(Dict dict : registeredDicts) {
for (Dict dict : registeredDicts) {
Utils.info(String.format("Searching (%s) in dictionary {%s}",
wordSpell, dict.getName()));
word = dict.queryWord(wordSpell);
if (word != null && !word.isEmypty()) {
hitedDict = dict.getName();

Utils.info("==> Get result from " + hitedDict);
if (!hitedDict.equals(defaultDict.getName())) {
defaultDict.addWord(word);
wordSpell, dict.getName()));
ArrayList<Word> words = dict.queryWordBySpell(wordSpell);
for (Word w : words) {
if (w != null && !w.isEmypty()) {
word = w;
hitedDict = dict.getName();

Utils.info("==> Get result from " + hitedDict);
if (!hitedDict.equals(defaultDict.getName())) {
defaultDict.addWord(word);
}
break;
}
}
if (word != null && !word.isEmypty()) {
break;
}
}

return word;
}

public ArrayList<Word> queryWordByAll(String wordSpell)
throws SQLException {
ArrayList<Word> words = new ArrayList<>();
Word word = null;
String hitedDict = null;
LocalDict defaultDict = this.getRegisteredLocalDicts().get(0);

ArrayList<Dict> registeredDicts = this.getRegisteredDicts();
Utils.info("There are " + registeredDicts.size()
+ " registered dictionarys");
Utils.debug("RegisteredDicts: " + registeredDicts);

for (Dict dict : registeredDicts) {
Utils.info(String.format("Searching (%s) in dictionary {%s}",
wordSpell, dict.getName()));
words.addAll(dict.queryWordBySpell(wordSpell));
for (Word w : words) {
if (w != null && !w.isEmypty()) {
word = w;
hitedDict = dict.getName();

Utils.info("==> Get result from " + hitedDict);
if (!hitedDict.equals(defaultDict.getName())) {
defaultDict.addWord(word);
}
}
}
}

return words;
}

// register dict {{{ //
/**
* registerDict
* insert the name, shortName, dict_type_id, dict_size,
* dict_mtime, dict_atime of a register dict into table(dicts)
* If the size of dict is less than 0, express that dict is DictType.Online
* This method is implemented by transaction.
* It's not elegant. The better way is database native trigger.
* registerDict insert the name, shortName, dict_type_id, dict_size, dict_mtime,
* dict_atime of a register dict into table(dicts) If the size of dict is less
* than 0, express that dict is DictType.Online This method is implemented by
* transaction. It's not elegant. The better way is database native trigger.
*/
// TODO: Instaed registerDict() of database native trigger <27-10-20, gk07> //
// TODO: Instaed registerDict() of database native trigger <27-10-20, gk07> //
public void registerDict(Dict dict) throws SQLException {
LocalDict localDict = null;
String name = dict.getName();
Expand All @@ -257,12 +294,12 @@ public void registerDict(Dict dict) throws SQLException {
if (type.equals(DictType.Local)) {
localDict = (LocalDict) dict;
size = localDict.size();
if(!this.registeredLocalDicts.contains(localDict)){
if (!this.registeredLocalDicts.contains(localDict)) {
this.registeredLocalDicts.add(localDict);
}
} else {
OnlineDict onlineDict = (OnlineDict) dict;
if(!this.registeredOnlineDicts.contains(onlineDict)){
if (!this.registeredOnlineDicts.contains(onlineDict)) {
this.registeredOnlineDicts.add(onlineDict);
}
}
Expand Down Expand Up @@ -316,7 +353,7 @@ public void registerDict(Dict dict) throws SQLException {
public void clearRegisteredDicts() throws SQLException {
Connection con = db.getCurrentConUseDbName();

try(Statement stmt = con.createStatement();) {
try (Statement stmt = con.createStatement();) {
int affectedRow = stmt.executeUpdate(SQLStr.clearDicts());
if (affectedRow > 0) {
Utils.info("Cleared dicts table");
Expand All @@ -337,21 +374,41 @@ public void clearRegisteredDicts() throws SQLException {

public static void main(String... args) throws IOException, SQLException {
App app = new App();
Word word = null;
// Word word = null;
String wordSpell = null;

// if (args != null && args.length > 0) {
// wordSpell = Dict.preProcessWordSpell(String.join(" ", args));
// word = app.queryWordByFirst(wordSpell);

// if (word != null && !word.isEmypty()) {
// Formatter fmt = new Formatter(word);
// fmt.printColorText();
// } else {
// Utils.warning("Can't find anything");
// }
// } else {
// Utils.warning("Nothing is inputed");
// }

if (args != null && args.length > 0) {
wordSpell = Dict.preProcessWordSpell(String.join(" ", args));
word = app.queryWordByFirst(wordSpell);
ArrayList<Word> words = app.queryWordByAll(wordSpell);

if (words.size() > 0) {
for (Word word : words) {
if (word != null && !word.isEmypty()) {
Formatter fmt = new Formatter(word);
fmt.printColorText();
}
}
} else {
Utils.warning("Can't find anything");
}

if (word != null && !word.isEmypty()) {
Formatter fmt = new Formatter(word);
fmt.printColorText();
} else {
Utils.warning("Can't find anything");
}
} else {
Utils.warning("Nothing is inputed");
}

}
}
}
9 changes: 7 additions & 2 deletions src/main/java/kunDict/CollinsOnlineDict.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kunDict;

import java.net.URISyntaxException;
import java.util.ArrayList;

public class CollinsOnlineDict extends OnlineDict {

Expand All @@ -15,21 +16,25 @@ public CollinsOnlineDict(){
}

@Override
public Word queryWord(String wordSpell) {
public ArrayList<Word> queryWordBySpell(String wordSpell) {
String url = queryUrlBase + preProcessWordSpell(wordSpell);
Utils.debug("URL: " + url);
Word word = null;
ArrayList<Word> words = new ArrayList<>();

try {
Request req = new Request(url);
String html = req.get().body();
Extractor extractor = new Extractor(html);
word = extractor.collinsOnline();
if (word != null && ! word.isEmypty()) {
words.add(word);
}
} catch (URISyntaxException e) {
Utils.warning("Syntax error. Please check the spell of word.");
}

return word;
return words;
};

}
3 changes: 2 additions & 1 deletion src/main/java/kunDict/Dict.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kunDict;

import java.sql.SQLException;
import java.util.ArrayList;

abstract class Dict {
private String name;
Expand Down Expand Up @@ -64,7 +65,7 @@ public static String preProcessWordSpell(String wordSpell) {
return wordSpell.strip();
}

abstract Word queryWord(String wordSpell) throws SQLException;
abstract ArrayList<Word> queryWordBySpell(String wordSpell) throws SQLException;
}

enum DictType {
Expand Down
58 changes: 57 additions & 1 deletion src/main/java/kunDict/Extractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,61 @@ public Word collinsOnline() {
word = new Word(spell, pronounce, fre, forms, senseEntryList, source);
}
return word;
}
}

public Word longmanOnline() {
// extract a word from longman website through Jsoup {{{ //
Word word = null;
Document doc = Jsoup.parse(this.input);

Elements dicts = doc.select("span.dictentry");
Element dict = dicts.first();
Utils.debug("dicts size: " + dicts.size());
if (dicts.size() > 0) {

String source = "Longman Online English Dictionary";
String spell = dict.select("h1.pagetitle").text();
Pronounce pronounce = new Pronounce();
pronounce.setSoundmark(dict.select("span.PRON").text());
pronounce.setSound(
dict.select("span.speaker.brefile")
.attr("data-src-mp3"));
Frequency fre = new Frequency();
fre.setBand(dict.select("span.FREQ").first().text());
fre.setDescription(dict.select("span.FREQ").first().attr("title"));
Elements formsEleCrossRef = dict.select("span.crossRef");
Elements formsEleW = dict.select("span.w");
ArrayList<String> forms = new ArrayList<>();
for(Element formEle : formsEleCrossRef) {
String form = formEle.text();
if (!forms.contains(form)) forms.add(form);
}
for(Element formEle : formsEleW) {
String form = formEle.text();
if (!forms.contains(form)) forms.add(form);
}

Elements entrys = dict.select("span.Sense");
ArrayList<SenseEntry> senseEntryList = new ArrayList<>();

for (Element entry : entrys) {
String wordClass = entry.select("span.SIGNPOST").text();
if (! wordClass.equals("")) {

SenseEntry senseEntry = new SenseEntry();
senseEntry.setWordClass(wordClass);
senseEntry.setSense(entry.select("span.DEF").text());
for (Element example : entry.select("span.EXAMPLE")) {
senseEntry.addExample(example.text());
}
senseEntryList.add(senseEntry);
}
}
// }}} extract a word from longman website through Jsoup //

word = new Word(spell, pronounce, fre, forms, senseEntryList, source);
}
return word;
}

}
Loading

0 comments on commit a07ab1a

Please sign in to comment.