中文词典转换程序

将中文文本转换成hashmap对象,中文词一个一行.

package test;

import java.io.Serializable;

public class Item implements Serializable {
private String content;
private int count;
private boolean isWord;
private boolean haveSuccessor;

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}

public boolean isWord() {
return isWord;
}

public void setWord(boolean isWord) {
this.isWord = isWord;
}

public boolean isHaveSuccessor() {
return haveSuccessor;
}

public void setHaveSuccessor(boolean haveSuccessor) {
this.haveSuccessor = haveSuccessor;
}
}

package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;

public class Main {

/**
* @param args
* @throws IOException
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException,
IOException {
// TODO Auto-generated method stub

String file = null;
String dir = null;
HashMap map = null;

file = "f:\\test\\results.obj";
dir = "f:\\word";

File filer = new File(file);
if (!filer.exists()) {
map = new HashMap();
Main.save(file, map);
} else {
map = Main.load(file);
}

String[] results = null;

results = Main.list(dir);
if (results != null) {
int s = results.length;
for (int i = 0; i < s; i++) {
try {
Main.read(dir + "\\" + results[i], map);
} catch(Exception e) {
//
e.printStackTrace();
}
}
}

Main.save(file, map);

}

public static void read(String file, HashMap map) throws IOException {
InputStreamReader reader = new InputStreamReader(new FileInputStream(file), "UTF-8");
BufferedReader br = new BufferedReader(reader);
String s1 = null;
while ((s1 = Main.read(br)) != null) {
System.out.println(s1);
Main.scan(s1, map);
}
br.close();
reader.close();
}

public static String read(BufferedReader reader) {
String result = null;

try {
result = reader.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return result;
}

public static String[] list(String path) {
File file = new File(path);
String[] result = null;

if (file.exists()) {
result = file.list();
}
return result;
}

public static HashMap load(String file) throws FileNotFoundException,
IOException {
HashMap map = null;

ObjectInputStream in = null;
try {
in = new ObjectInputStream(new FileInputStream(new File(file)));
map = (HashMap) in.readObject();
// out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
try {
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
in = null;
}
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
in = null;
}
}
}

return map;
}

public static void save(String file, HashMap map) {
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(new FileOutputStream(new File(file)));
out.writeObject(map);
// out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
try {
out.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
out = null;
}
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
out = null;
}
}
}
}

public static void scan(String content, HashMap map) {
String temp = null;
Item item = null;
int s = content.length();
for (int i = 0; i < s; i++) {
if(s == 1) {
item = new Item();
item.setContent(content);
item.setCount(1);
item.setWord(true);
item.setHaveSuccessor(false);

} else if(i < s-1){
temp = content.substring(0, i);
item = (Item)map.get(temp.toString());
if(item != null) {
item.setCount(item.getCount() + 1);
item.setHaveSuccessor(true);
} else {
item = new Item();
item.setContent(temp);
item.setCount(1);
item.setWord(false);
item.setHaveSuccessor(true);
}
} else if(i == s-1){
temp = content;
item = (Item)map.get(temp.toString());
if(item != null) {
item.setCount(item.getCount() + 1);
item.setWord(true);
} else {
item = new Item();
item.setContent(temp);
item.setCount(1);
item.setWord(true);
item.setHaveSuccessor(false);
}
}
map.put(temp, item);
}
}
}

package test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.HashMap;

public class Test {

/**
* @param args
* @throws IOException
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
// TODO Auto-generated method stub

HashMap map = null;

map = Test.load("f:\\test\\results.obj");

for(Object obj : map.keySet().toArray()) {
String str = (String)obj;
Item item = (Item)map.get(str);
System.out.println(str + " : " + item.getCount() + " , " + item.isWord() + " , " + item.isHaveSuccessor());
}

System.out.println(map.size());
}

public static HashMap load(String file) throws FileNotFoundException,
IOException {
HashMap map = null;

ObjectInputStream in = null;
try {
in = new ObjectInputStream(new FileInputStream(new File(file)));
map = (HashMap) in.readObject();
// out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
try {
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
in = null;
}
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
in = null;
}
}
}

return map;
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值