Skip to content

Commit b6c0400

Browse files
committed
#6 #7 预加载 & 移除 唇 简繁转换
1 parent 6a8caad commit b6c0400

File tree

8 files changed

+268
-65
lines changed

8 files changed

+268
-65
lines changed

readme.md

+61-11
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ quick-chinese-transfer
1515
- [https://github.com/hankcs/HanLP](https://github.com/hankcs/HanLP)
1616
- [https://github.com/luhuiguo/chinese-utils](https://github.com/luhuiguo/chinese-utils)
1717

18+
词典更新维护来自:
19+
20+
- [https://github.com/hankcs/HanLP/tree/1.x/data/dictionary/tc](https://github.com/hankcs/HanLP/tree/1.x/data/dictionary/tc)
1821

1922
使用姿势
2023
---
@@ -24,20 +27,22 @@ quick-chinese-transfer
2427
中央仓库导入依赖
2528

2629
```xml
30+
<!-- https://mvnrepository.com/artifact/com.github.liuyueyi/quick-transfer-core -->
2731
<dependency>
2832
<groupId>com.github.liuyueyi</groupId>
2933
<artifactId>quick-transfer-core</artifactId>
30-
<version>0.2.4</version>
34+
<version>0.2.5</version>
3135
</dependency>
3236
```
3337

34-
gradle
38+
**中央仓库 gradle**
3539

3640
```gradle
37-
implementation 'com.github.liuyueyi:quick-transfer-core:0.2.4'
41+
// https://mvnrepository.com/artifact/com.github.liuyueyi/quick-transfer-core
42+
implementation 'com.github.liuyueyi:quick-transfer-core:0.2.5'
3843
```
3944

40-
使用jitpack导入依赖
45+
**使用jitpack导入依赖**
4146

4247
```xml
4348
<repositories>
@@ -50,7 +55,7 @@ implementation 'com.github.liuyueyi:quick-transfer-core:0.2.4'
5055
<dependency>
5156
<groupId>com.github.liuyueyi.quick-chinese-transfer</groupId>
5257
<artifactId>quick-transfer-core</artifactId>
53-
<version>0.2.4</version>
58+
<version>0.2.5</version>
5459
</dependency>
5560
```
5661

@@ -65,15 +70,14 @@ allprojects {
6570
}
6671
6772
dependencies {
68-
implementation 'com.github.liuyueyi.quick-chinese-transfer:quick-transfer-core:0.2.4'
73+
implementation 'com.github.liuyueyi.quick-chinese-transfer:quick-transfer-core:0.2.5'
6974
}
7075
```
7176

7277

73-
**测试case**
78+
**基本使用演示**
7479

7580
```java
76-
@Test
7781
public void testTrans() {
7882
String text = "这斜月三星洞…… 长寿面,孙悟空,猪八戒,唐僧,沙和尚,白龙马,李靖,托塔天王, 戏说西游,许多人都这样说,收拾一下,拾金不昧;纔=才";
7983
String out = ChineseUtils.s2t(text);
@@ -101,6 +105,47 @@ hk2s -->这斜月三星洞…… 长寿面,孙悟空,猪八戒,唐僧,
101105
tw2s -->这斜月三星洞…… 长寿面,孙悟空,猪八戒,唐僧,沙和尚,白龙马,李靖,托塔天王, 戏说西游,许多人都这样说,收拾一下,拾金不昧;才=才
102106
```
103107

108+
109+
高性能接入
110+
---
111+
112+
**词典预加载**
113+
114+
> 当系统对性能要求较高时,可以考虑提前异步加载词典、避免首次使用加载词典导致开销高
115+
116+
117+
```java
118+
// 预热加载所有的词典
119+
ChineseUtils.preLoad(true, TransType.values());
120+
121+
// 预加载简体转繁体词典
122+
ChineseUtils.preLoad(true, TransType.SIMPLE_TO_TRADITIONAL);
123+
124+
// 预加载简繁互转词典
125+
ChineseUtils.preLoad(true, TransType.SIMPLE_TO_TRADITIONAL, TransType.TRADITIONAL_TO_SIMPLE);
126+
```
127+
128+
**卸载**
129+
130+
> 当内存敏感时,提前卸载无用词典减少内存占用
131+
132+
```java
133+
// 卸载所有词典
134+
ChineseUtils.unLoad(TransType.values());
135+
136+
// 卸载简繁互转词典
137+
ChineseUtils.unLoad(TransType.SIMPLE_TO_TRADITIONAL, TransType.TRADITIONAL_TO_SIMPLE);
138+
```
139+
140+
**转换**
141+
142+
ChineseUtils封装了通用的字体转换接口,可以直接通过下面的方式实现转换
143+
144+
```java
145+
ChineseUtils.transfer("一灰灰blog", TransType.SIMPLE_TO_TRADITIONAL);
146+
```
147+
148+
104149
版本说明
105150
---
106151

@@ -112,10 +157,15 @@ tw2s -->这斜月三星洞…… 长寿面,孙悟空,猪八戒,唐僧,
112157
- 繁转简: fix 奔驰,奶油,黄油转换异常
113158
- 0.2.2
114159
- 繁转简
115-
- [#3 克拉转成克拉布](https://github.com/liuyueyi/quick-chinese-transfer/issues/3)
160+
- [#3 克拉转成克拉布](https://github.com/liuyueyi/quick-chinese-transfer/issues/3)
116161
- 0.2.3
117162
- 繁体转简体
118-
- [#4 乾 繁转简错误](https://github.com/liuyueyi/quick-chinese-transfer/issues/4)]
163+
- [#4 乾 繁转简错误](https://github.com/liuyueyi/quick-chinese-transfer/issues/4)]
119164
- 0.2.4
120165
- 繁体转简体
121-
- [#5 骼 繁转简错误](https://github.com/liuyueyi/quick-chinese-transfer/issues/5)]
166+
- [#5 骼 繁转简错误](https://github.com/liuyueyi/quick-chinese-transfer/issues/5)]
167+
- 0.2.5
168+
- 删除 `唇<=>脣` 简繁转换
169+
- [#6 修正簡繁轉換:唇<=>脣](https://github.com/liuyueyi/quick-chinese-transfer/issues/6)]
170+
- 支持同步/异步预加载词典、卸载词典
171+
- [#7 支持词典异步预加载、卸载](https://github.com/liuyueyi/quick-chinese-transfer/issues/7)

transfer-core/pom.xml

+1-10
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<version>0.1</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
11-
<version>0.2.4</version>
11+
<version>0.2.5</version>
1212

1313
<artifactId>quick-transfer-core</artifactId>
1414

@@ -25,13 +25,4 @@
2525
<url>http://blog.hhui.top</url>
2626
</developer>
2727
</developers>
28-
29-
<dependencies>
30-
<dependency>
31-
<groupId>junit</groupId>
32-
<artifactId>junit</artifactId>
33-
<version>4.13</version>
34-
<scope>test</scope>
35-
</dependency>
36-
</dependencies>
3728
</project>

transfer-core/src/main/java/com/github/liuyueyi/quick/transfer/ChineseUtils.java

+62-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
package com.github.liuyueyi.quick.transfer;
77

8+
import com.github.liuyueyi.quick.transfer.constants.TransType;
89
import com.github.liuyueyi.quick.transfer.dictionary.DictionaryContainer;
910

1011
/**
@@ -13,13 +14,68 @@
1314
* @author luhuiguo
1415
*/
1516
public class ChineseUtils {
17+
18+
/**
19+
* 预热,加载字体词典,支持后台线程加载,避免阻塞主线程(适用于先加载,后使用的场景)
20+
*
21+
* @param async 表示异步预热
22+
* @param types 需要预热的词典类型
23+
*/
24+
public static void preLoad(boolean async, TransType... types) {
25+
if (async) {
26+
Thread thread = new Thread(new Runnable() {
27+
@Override
28+
public void run() {
29+
preLoad(types);
30+
}
31+
});
32+
thread.setDaemon(true);
33+
thread.start();
34+
} else {
35+
preLoad(types);
36+
}
37+
}
38+
39+
/**
40+
* 预热,加载字体词典
41+
*
42+
* @param transType
43+
*/
44+
public static void preLoad(TransType... transType) {
45+
for (TransType type : transType) {
46+
transfer("", type);
47+
}
48+
}
49+
50+
/**
51+
* 下载不用的词典
52+
*
53+
* @param transTypes
54+
*/
55+
public static void unLoad(TransType... transTypes) {
56+
for (TransType type : transTypes) {
57+
DictionaryContainer.getInstance().unloadDictionary(type);
58+
}
59+
}
60+
61+
/**
62+
* 通用简繁简繁转换接口
63+
*
64+
* @param content
65+
* @param type
66+
* @return
67+
*/
68+
public static String transfer(String content, TransType type) {
69+
return DictionaryContainer.getInstance().getDictionary(type).convert(content);
70+
}
71+
1672
/**
1773
* 简体转繁体
1874
*
1975
* @return
2076
*/
2177
public static String s2t(String content) {
22-
return DictionaryContainer.getInstance().getDictionary("s2t").convert(content);
78+
return DictionaryContainer.getInstance().getDictionary(TransType.SIMPLE_TO_TRADITIONAL).convert(content);
2379
}
2480

2581
/**
@@ -29,7 +85,7 @@ public static String s2t(String content) {
2985
* @return
3086
*/
3187
public static String s2hk(String content) {
32-
return DictionaryContainer.getInstance().getDictionary("s2hk").convert(content);
88+
return DictionaryContainer.getInstance().getDictionary(TransType.SIMPLE_TO_HONGKONG).convert(content);
3389
}
3490

3591
/**
@@ -39,7 +95,7 @@ public static String s2hk(String content) {
3995
* @return
4096
*/
4197
public static String s2tw(String content) {
42-
return DictionaryContainer.getInstance().getDictionary("s2tw").convert(content);
98+
return DictionaryContainer.getInstance().getDictionary(TransType.SIMPLE_TO_TAIWAN).convert(content);
4399
}
44100

45101
/**
@@ -49,7 +105,7 @@ public static String s2tw(String content) {
49105
* @return
50106
*/
51107
public static String t2s(String content) {
52-
return DictionaryContainer.getInstance().getDictionary("t2s").convert(content);
108+
return DictionaryContainer.getInstance().getDictionary(TransType.TRADITIONAL_TO_SIMPLE).convert(content);
53109
}
54110

55111
/**
@@ -59,7 +115,7 @@ public static String t2s(String content) {
59115
* @return
60116
*/
61117
public static String hk2s(String content) {
62-
return DictionaryContainer.getInstance().getDictionary("hk2s").convert(content);
118+
return DictionaryContainer.getInstance().getDictionary(TransType.HONGKONG_TO_SIMPLE).convert(content);
63119
}
64120

65121
/**
@@ -69,6 +125,6 @@ public static String hk2s(String content) {
69125
* @return
70126
*/
71127
public static String tw2s(String content) {
72-
return DictionaryContainer.getInstance().getDictionary("tw2s").convert(content);
128+
return DictionaryContainer.getInstance().getDictionary(TransType.TAIWAN_TO_SIMPLE).convert(content);
73129
}
74130
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.github.liuyueyi.quick.transfer.constants;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* @author yihui
8+
* @date 2022/06/06
9+
*/
10+
public enum TransType {
11+
/**
12+
* 简体转繁体
13+
*/
14+
SIMPLE_TO_TRADITIONAL("s2t"),
15+
/**
16+
* 简体转香港繁体
17+
*/
18+
SIMPLE_TO_HONGKONG("s2hk"),
19+
/**
20+
* 简体转台湾繁体
21+
*/
22+
SIMPLE_TO_TAIWAN("s2tw"),
23+
/**
24+
* 繁体转简体
25+
*/
26+
TRADITIONAL_TO_SIMPLE("t2s"),
27+
/**
28+
* 香港转简体
29+
*/
30+
HONGKONG_TO_SIMPLE("hk2s"),
31+
32+
/**
33+
* 台湾转简体
34+
*/
35+
TAIWAN_TO_SIMPLE("tw2s");
36+
37+
private final String type;
38+
39+
TransType(String type) {
40+
this.type = type;
41+
}
42+
43+
public String getType() {
44+
return type;
45+
}
46+
47+
static Map<String, TransType> typeMap;
48+
49+
static {
50+
typeMap = new HashMap<>(8, 1);
51+
for (TransType transType : values()) {
52+
typeMap.put(transType.getType(), transType);
53+
}
54+
}
55+
56+
public static TransType typeOf(String type) {
57+
return typeMap.get(type);
58+
}
59+
}

0 commit comments

Comments
 (0)