JAVA版StarDict星际译王简单实现(四)

2014-11-24 11:20:01 · 作者: · 浏览: 12
输入获取待查询的单词,控制台下为GBK字符,字典索引中的英文单词字母也是如此
* @param max_len
* @param count
* @return
*/
public static String get_input(int max_len, int[] count)
{
byte input_buf[] = new byte[max_len];
count[0] = 0;
String tempString[] = new String[1];
try
{
count[0] = System.in.read(input_buf) - 2;// 返回实际读取到的字符数,减去2个控制字符
byte temp_buf[] = new byte[count[0]];
for (int i = 0; i < count[0]; i++)
{
temp_buf[i] = input_buf[i];
}
tempString[0] = new String(temp_buf);
}
catch (Exception e)
{
System.out.println("Input error!");
}
System.out.println("Your input is:" + tempString[0]);
return tempString[0];
}
/**
* 从标准输入获取待查询的单词,控制台下为GBK字符,字典索引中的英文单词字母也是如此
* @param input_buf
* @param count
* @return
*/
public static byte[] get_input(byte[] input_buf, int[] count)
{
try
{
count[0] = System.in.read(input_buf) - 2;// 返回实际读取到的字符数,减去2个控制字符
}
catch (Exception e)
{
input_buf = null;
System.out.println("Input error!");
}
return input_buf;
}
/**
* 缓存KEYS在idx中的偏移信息,以便加快search_word的搜索速度
* @param idx_cache 保存每个单字母单词对应的起始位置
* @return
*/
public static void cache_idx(long[] idx_cache)
{
int i;
long[] p = idx_cache;
int unused1[] = new int[1];
int unused2[] = new int[1];
try
{
// 将文件内部的位置指针重新指向一个流(数据流/文件)的开头返回FILE指针当前位置,
// 然后重新遍历整个文件搜寻下一个字母开头的单词
isidx.reset();
STREAM_LOCAL = 0;
for (i = 0; i < MAX_KEYS; i++)
{
// System.out.println("Start search_word:" + KEY[i]);
if (search_word(KEY[i], unused1, unused2))// 从idx文件中搜索由word指定的单词,并保存相应的偏移和长度信息
{
p[i] = STREAM_LOCAL; // 返回当前文件位置
// String tempString = Long.toString(STREAM_LOCAL);
// System.out.println(KEY[i] + "'s local is:" + tempString);
System.out.println(KEY[i] + "'s local is:" + STREAM_LOCAL
+ " offset:" + unused1[0] + "length:" + unused2[0]);
} else
p[i] = 0;
}
// isidx.reset();
}
catch (Exception e)
{
// TODO: handle exception
}
}
/**
* 定位由word指定的单词在idx文件中的大概偏移位置
* @param word
* @param idx_cache
* @return
*/
public static long locate_idx(String word, long[] idx_cache)
{
int i = 0;
int pre = 0;
String tempString = word.toLowerCase();
while (i < MAX_KEYS && KEY[i].charAt(0) < tempString.charAt(0))
{
pre = i;
++i;
}
if (tempString.charAt(0) == '-')
{
pre = 0;
}
System.out.println("Now word's locate is:" + idx_cache[pre]);
return idx_cache[pre];
}
/**
* 主要查询函数
*/
public static void consult()
{
byte data[] = null;// 释义数据,UTF-8数据
long idx[] = new long[MAX_KEYS];// 26个字母孤立单词+"-"开头的后缀对应的索引缓冲
int offset[] = new int[1];
int length[] = new int[1];
System.out.println("Start cache_idx....!");
try
{
System.out.println("Open files....!");
// 读取字典索引文件
isidx = new BufferedInputStream(new FileInputStream(
idxfileString));
isidx.mark(isidx.available() + 1);
if (!isidx.markSupported())
{
System.out.println("This stream do not support mark....!");
}
}
catch (Exception e)
{
System.out.println("Open files error!");
e.printStackTrace();
}
cache_idx(idx);// 缓存KEYS在idx中的偏移信息,以便加快search_word的搜索速度
try
{
isdict = new BufferedInputStream(new FileInputStream(
dictfileString));
isdict.mark(isdict.available() + 1);
if (!isdict.mark