public Pair<Long, Long> findWordMemoryOffsets(int idx) { if (idx < 0 || idx >= idxFile.getLongWordCount()) { return null; } WordEntry tempEntry = idxFile.getEntryList().get((int) idx); return new Pair<Long, Long>(tempEntry.getLongOffset(), tempEntry.getLongSize()); }
/** * Constructor to load dictionary with given path. * * @param url Path of one of stardict file or Path of folder contains stardict files */ public StarDict(String url) { File file = new File(url); if (!file.isDirectory()) { strURL = getFileNameWithoutExtension(url); Log.d(LOG_TAG, "strURL=" + strURL); ifoFile = new IfoFile(strURL + ".ifo"); idxFile = new IdxFile(strURL + ".idx", ifoFile.getLongWordCount(), ifoFile.getLongIdxFileSize()); dictFile = new DictFile(strURL + ".dict"); Log.d(LOG_TAG, "done " + ifoFile.isBoolIsLoaded() + " " + idxFile.isLoaded() + " "); } else { String[] list = file.list(); strURL = url; Log.d(LOG_TAG, "strURL=" + strURL); for (int i = list.length - 1; i >= 0; i--) { String extension = getExtension(list[i]); String path = url + File.separator + list[i]; if (extension.equals("ifo")) { ifoFile = new IfoFile(path); } else if (extension.equals("idx")) { idxFile = new IdxFile(path, ifoFile.getLongWordCount(), ifoFile.getLongIdxFileSize()); } else if (extension.equals("dict")) { dictFile = new DictFile(path); } } } if (ifoFile.isBoolIsLoaded() && idxFile.isLoaded()) { boolAvailable = true; } }
/** * lookup a word by its index. * * @param idx index of a word * @return word data */ public String lookupWord(int idx) { if (idx < 0 || idx >= idxFile.getLongWordCount()) { return null; } WordEntry tempEntry = idxFile.getEntryList().get((int) idx); return dictFile.getWordData(tempEntry.getLongOffset(), tempEntry.getLongSize()); }
/** load index file and info file. */ public void reLoad() { boolAvailable = false; ifoFile.reload(); idxFile.reload(); if (ifoFile.isBoolIsLoaded() && idxFile.isLoaded()) { boolAvailable = true; } }
/** * Add list of word to idx, dict file, modify size .ifo file. * * @param pWord word that is added * @param pMean word mean * @return true if success */ public boolean addListOfWords(String[] pWord, String[] pMean) { if (pWord.length != pMean.length || pWord.length == 0) { return false; } try { for (int i = 0; i < pWord.length; i++) { String strLwrWord = pWord[i].toLowerCase(); int pos = (int) idxFile.findIndexForWord(strLwrWord); boolean bExist = false; if (pos < (int) idxFile.getLongWordCount()) { if (strLwrWord.compareTo(((WordEntry) idxFile.getEntryList().get(pos)).getStrLwrWord()) == 0) { bExist = true; } } long nextOffset = dictFile.addData(pMean[i]); if (nextOffset >= 0) { if (!bExist) { idxFile.addEntry(pWord[i], nextOffset, pMean[i].length(), pos); } else { WordEntry tempEntry = idxFile.getEntryList().get(pos); tempEntry.setLongOffset(nextOffset); tempEntry.setLongSize(pMean[i].length()); } } } idxFile.write(); ifoFile.setLongIdxFileSize(idxFile.getLongIdxFileSize()); ifoFile.setLongWordCount(idxFile.getLongWordCount()); ifoFile.write(); } catch (Exception ex) { return false; } return true; }
/** * check if a word is in dictionary. * * @param word that is looked up in database * @return true if exists, false otherwise */ public boolean existWord(String word) { int wordIndex = (int) idxFile.findIndexForWord(word); if (wordIndex >= idxFile.getLongWordCount()) { return false; } String lwrWord = word.toLowerCase(); if (lwrWord.equals(idxFile.getEntryList().get(wordIndex).getStrLwrWord())) { return true; } return false; }
/** * lookup a word. * * @param word that is looked up in database. * @return word data */ public String lookupWord(String word) { if (!boolAvailable) { return "the dictionary is not available"; } int idx = (int) idxFile.findIndexForWord(word); return lookupWord(idx); }
/** * get the nearest of the chosen word. * * @param word that is looked up in database * @return a list of nearest word. */ public List<Word> getNearestWords(String word) { if (boolAvailable) { int idx = (int) idxFile.findIndexForWord(word); int nMax = nearest + idx; if (nMax > idxFile.getLongWordCount()) { nMax = (int) idxFile.getLongWordCount(); } List<Word> wordList = new ArrayList<Word>(); for (int i = idx; i < nMax; i++) { if (i != 0) { Word tempWord = new Word(); tempWord.setStrWord(idxFile.getEntryList().get(i).getStrWord()); tempWord.setIndex(i); wordList.add(tempWord); } } return wordList; } return null; }
/** * get a list of word entry. * * @return list of word entry */ public List<WordEntry> getWordEntry() { return idxFile.getEntryList(); }