Skip to content
This repository has been archived by the owner on Dec 23, 2024. It is now read-only.

Commit

Permalink
更新编译版本;开始重构代码,汉化
Browse files Browse the repository at this point in the history
  • Loading branch information
testacount1 committed Jul 16, 2017
1 parent 1615980 commit 76a1fcb
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 58 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/bin/
/dist/
/classes/
2 changes: 1 addition & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<fileset dir="${source.dir}" excludes="**/*.java"/>
</copy>

<javac srcdir="${source.dir}" destdir="${classes.dir}" fork="yes" debug="true" source="1.3" target="1.3"/>
<javac srcdir="${source.dir}" destdir="${classes.dir}" fork="yes" debug="true" source="1.8" target="1.8"/>

<delete dir="${dist.dir}"/>
<mkdir dir="${dist.dir}"/>
Expand Down
86 changes: 33 additions & 53 deletions src/com/spreada/utils/chinese/简繁转换类.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@
import java.util.Properties;
import java.util.Set;


public class 简繁转换类 {


private Properties 字符表 = new Properties();
private Set conflictingSets = new HashSet();

public static final int TRADITIONAL = 0;
public static final int SIMPLIFIED = 1;
private Set<String> 争议集 = new HashSet<>();

public enum 目标 {
繁体(0), 简体(1);

int 内部值;
目标(int ) {
内部值 = ;
}
}
private static final int TRADITIONAL = 0;
private static final int SIMPLIFIED = 1;
private static final int NUM_OF_CONVERTERS = 2;
private static final 简繁转换类[] converters = new 简繁转换类[NUM_OF_CONVERTERS];
private static final String[] propertyFiles = new String[2];
Expand All @@ -30,45 +36,33 @@ public class 简繁转换类 {
propertyFiles[SIMPLIFIED] = "zh2Hans.properties";
}



public static 简繁转换类 getInstance(目标 简繁) {
return getInstance(简繁.内部值);
}

/**
*
* @param converterType 0 for traditional and 1 for simplified
* @return
*/
public static 简繁转换类 getInstance(int converterType) {

if (converterType >= 0 && converterType < NUM_OF_CONVERTERS) {

if (converters[converterType] == null) {
synchronized (简繁转换类.class) {
if (converters[converterType] == null) {
converters[converterType] = new 简繁转换类(propertyFiles[converterType]);
}
}
private static 简繁转换类 getInstance(int converterType) {
if (converters[converterType] == null) {
synchronized (简繁转换类.class) {
converters[converterType] = new 简繁转换类(propertyFiles[converterType]);
}
return converters[converterType];

} else {
return null;
}
return converters[converterType];
}

public static String convert(String text, int converterType) {
简繁转换类 instance = getInstance(converterType);
return instance.convert(text);
public static String 转换(String 文本, 目标 简繁) {
简繁转换类 instance = getInstance(简繁);
return instance.转换(文本);
}


private 简繁转换类(String propertyFile) {
InputStream is = getClass().getResourceAsStream(propertyFile);

InputStream is = null;


is = getClass().getResourceAsStream(propertyFile);

// File propertyFile = new File("C:/Temp/testMDB/TestTranslator/abc.txt");
if (is != null) {
BufferedReader reader = null;
try {
Expand All @@ -92,46 +86,43 @@ public static String convert(String text, int converterType) {
}

private void initializeHelper() {
Map stringPossibilities = new HashMap();
Map<String, Integer> stringPossibilities = new HashMap<>();
Iterator iter = 字符表.keySet().iterator();
while (iter.hasNext()) {
String key = (String) iter.next();
if (key.length() >= 1) {

for (int i = 0; i < (key.length()); i++) {
String keySubstring = key.substring(0, i + 1);
if (stringPossibilities.containsKey(keySubstring)) {
Integer integer = (Integer) (stringPossibilities.get(keySubstring));
Integer integer = (stringPossibilities.get(keySubstring));
stringPossibilities.put(keySubstring, new Integer(integer.intValue() + 1));

} else {
stringPossibilities.put(keySubstring, new Integer(1));
}

}
}
}

iter = stringPossibilities.keySet().iterator();
while (iter.hasNext()) {
String key = (String) iter.next();
if (((Integer) (stringPossibilities.get(key))).intValue() > 1) {
conflictingSets.add(key);
if ((stringPossibilities.get(key)).intValue() > 1) {
争议集.add(key);
}
}
}

public String convert(String in) {
public String 转换(String in) {
StringBuilder outString = new StringBuilder();
StringBuilder stackString = new StringBuilder();

for (int i = 0; i < in.length(); i++) {

char c = in.charAt(i);
String key = "" + c;
stackString.append(key);

if (conflictingSets.contains(stackString.toString())) {
if (争议集.contains(stackString.toString())) {
// TODO: 不处理?
} else if (字符表.containsKey(stackString.toString())) {
outString.append(字符表.get(stackString.toString()));
stackString.setLength(0);
Expand All @@ -147,30 +138,19 @@ public String convert(String in) {
return outString.toString();
}


private void flushStack(StringBuilder outString, StringBuilder stackString) {
while (stackString.length() > 0) {
if (字符表.containsKey(stackString.toString())) {
outString.append(字符表.get(stackString.toString()));
stackString.setLength(0);

} else {
outString.append("" + stackString.charAt(0));
stackString.delete(0, 1);
}

}
}


String parseOneChar(String c) {

if (字符表.containsKey(c)) {
return (String) 字符表.get(c);

}
return c;
return 字符表.containsKey(c) ? (String) 字符表.get(c) : c;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@

import org.junit.Test;

import com.spreada.utils.chinese.简繁转换类;
import com.spreada.utils.chinese.简繁转换类.目标;

public class 简繁转换测试类 {

private final static 简繁转换类 繁体转换器 = 简繁转换类.getInstance(简繁转换类.TRADITIONAL);
private final static 简繁转换类 简体转换器 = 简繁转换类.getInstance(简繁转换类.SIMPLIFIED);
private final static 简繁转换类 繁体转换器 = 简繁转换类.getInstance(目标.繁体);
private final static 简繁转换类 简体转换器 = 简繁转换类.getInstance(目标.简体);

@Test
public void 基本测试() {
assertEquals("簡單", 繁体转换器.convert("简单"));
assertEquals("简单", 简体转换器.convert("簡單"));
assertEquals("簡單", 繁体转换器.转换("简单"));
assertEquals("简单", 简体转换器.转换("簡單"));
}

}

0 comments on commit 76a1fcb

Please sign in to comment.