失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 中文词典转换程序

中文词典转换程序

时间:2020-05-12 03:39:54

相关推荐

中文词典转换程序

将中文文本转换成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;

}

}

如果觉得《中文词典转换程序》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。