Skip to content

Commit

Permalink
release branch 0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
houbb committed Jan 15, 2022
1 parent 1035a1e commit 6376ebb
Show file tree
Hide file tree
Showing 19 changed files with 459 additions and 195 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,9 @@
|:---|:---|:---|:---|
| O | 简化繁简体的转换 | 2021-10-04 21:26:17 | |
| F | 修复 `` 拼音错误问题 | 2021-10-04 21:26:17 | |

# release_0.3.0

| 类型 | 变化 | 时间 | 备注 |
|:---|:---|:---|:---|
| A | 同音字实现 | 2022-01-15 21:26:17 | |
71 changes: 55 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,36 +35,45 @@

- 支持判断是否为同音字

- 支持单独获取声调信息
- 支持同音字

- 支持获取声母韵母信息
### v0.3.0 主要变更

### v0.2.2 主要变更

- 简化繁简体转换

- 修复 `` 的拼音错误
- 支持同音字

# 快速开始

## 准备

jdk 1.8+
jdk 1.7+

## maven 引入

```xml
<dependency>
<groupId>com.github.houbb</groupId>
<artifactId>pinyin</artifactId>
<version>0.2.2</version>
<version>0.3.0</version>
</dependency>
```

## 快速开始

参考 [PinyinHelperTest](https://github.com/houbb/pinyin/blob/master/src/test/java/com/github/houbb/pinyin/test/util/PinyinHelperTest.java)

### 方法概览

| 方法 | 返回值 | 说明 |
|:----|:----|:----|
| toPinyin(String) | String | 文本转换为拼音 |
| toPinyin(String, PinyinStyleEnum) | String | 文本转换为拼音,可指定拼音样式 |
| toPinyin(String, PinyinStyleEnum, String) | String | 文本转换为拼音,可指定拼音样式,可指定连接符号 |
| toPinyinList(char) | List<String> | 返回汉字所有拼音列表 |
| toPinyinList(char, PinyinStyleEnum) | List<String> | 返回汉字所有拼音列表,指定拼音样式 |
| hasSamePinyin(char, char) | boolean | 判断两个汉字是否有相同的读音 |
| samePinyinMap(char) | Map<String, List<String>> | 返回汉字的同音字MAP,key 为拼音 NUM_LAST 模式 |
| samePinyinList(String) | List<String> | 返回拼音 NUM_LAST 模式对应的同音字 |

### 返回中文的拼音

使用 `PinyinHelper.toPinyin(string)` 进行中文转换。
Expand Down Expand Up @@ -189,6 +198,40 @@ String pinyin = PinyinHelper.toPinyin("奮斗");
Assert.assertEquals("fèn dòu", pinyin);
```

# 同音字

## 同音字 map

返回一个汉字,所有拼音对应的同音字列表。

```java
final char hanzi2 = '';
Map<String,List<String>> map2 = PinyinHelper.samePinyinMap(hanzi2);
```

对应的同音字结果为:

```
{tong2=[㠉, 㠽, 㣚, 㣠, 㤏, 㮔, 㸗, 㼧, 㼿, 䂈, 䆚, 䮵, 䳋, 䴀, 䶱, 仝, 佟, 侗, 偅, 僮, 勭, 同, 哃, 垌, 峂, 峒, 峝, 庝, 彤, 晍, 曈, 朣, 桐, 橦, 氃, 洞, 浵, 湩, 潼, 烔, 燑, 爞, 犝, 狪, 獞, 痌, 眮, 瞳, 砼, 硐, 硧, 秱, 穜, 童, 筒, 筩, 粡, 絧, 膧, 艟, 茼, 蚒, 蜼, 蟲, 衕, 詷, 赨, 酮, 重, 鉖, 鉵, 銅, 铜, 餇, 鮦, 鲖, 鼕, 𠖄, 𡦜, 𢈉, 𢏕, 𢓘, 𣑸, 𣪯, 𤱇, 𤺄, 𥩌, 𥫂, 𦏆, 𦒍, 𦨴, 𧇌, 𧊚, 𧋒, 𧋚, 𧌝, 𧳆, 𨚯, 𨜳, 𨝯, 𨠌, 𩍅, 𩩅, 𩻡, 𪀭, 𫍣], zhong4=[㐺, 㲴, 㼿, 䱰, 中, 乑, 仲, 众, 偅, 堹, 妕, 媑, 狆, 眾, 祌, 种, 種, 穜, 筗, 緟, 茽, 蚛, 蟲, 衆, 衶, 衷, 褈, 諥, 踵, 重, 𠱧, 𡥿, 𢝆, 𣱧, 𤚏, 𥻝, 𦌋, 𦔉, 𧬤, 𧳮, 𨉢, 𩾋, 𩿀], chong2=[㓽, 㹐, 䌬, 䖝, 䳯, 崇, 崈, 漴, 烛, 爞, 痋, 种, 種, 緟, 茧, 虫, 蝩, 蟲, 褈, 酮, 重, 隀, 𡿂, 𢖄, 𢝈, 𣐯, 𧝎, 𨛱, 𩅃, 𩌨, 𩜖, 𩞉, 𩞋]}
```

每一个读音作为 key,对应的同音字作为 list。

当然,有时候我们希望获取指定拼音的同音字列表。

## 同音字 List

```java
final String pinyinNumLast = "zhong4";
List<String> pinyinList = PinyinHelper.samePinyinList(pinyinNumLast);
```

对应结果:

```
[㐺, 㲴, 㼿, 䱰, 中, 乑, 仲, 众, 偅, 堹, 妕, 媑, 狆, 眾, 祌, 种, 種, 穜, 筗, 緟, 茽, 蚛, 蟲, 衆, 衶, 衷, 褈, 諥, 踵, 重, 𠱧, 𡥿, 𢝆, 𣱧, 𤚏, 𥻝, 𦌋, 𦔉, 𧬤, 𧳮, 𨉢, 𩾋, 𩿀]
```

# 自定义拼音词库

已有的词库很难满足各种各样的场景,本工具提供自定义拼音词库的功能。
Expand Down Expand Up @@ -277,14 +320,10 @@ pinyin4j 的汉语字符串转换是不支持分词的,本项目在支持分

- [x] 引导类开放分词的自定义配置

- [ ] 同音字列表返回
- [x] 同音字列表返回

- [ ] 谐音字判断
- [ ] 同韵字列表返回

- [ ] 谐音字列表返回
- [ ] 音近字

- [ ] 拼音转汉字

- [ ] 词组拼音系统词典瘦身

如果 chars 也是同样的拼音效果,则移除对应的词组拼音。降低内存,并且提升加载速度。
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.houbb</groupId>
<artifactId>pinyin</artifactId>
<version>0.2.2</version>
<version>0.3.0</version>

<properties>
<!--============================== All PLUGINS START ==============================-->
Expand All @@ -28,7 +28,7 @@

<!--============================== JAR START ==============================-->
<!--inter-->
<heaven.version>0.1.144</heaven.version>
<heaven.version>0.1.154</heaven.version>
<nlp-common.version>0.0.3</nlp-common.version>

<!--other-->
Expand Down Expand Up @@ -259,4 +259,4 @@
</profile>
</profiles>

</project>
</project>
6 changes: 3 additions & 3 deletions release.bat
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ ECHO "============================= RELEASE START..."

:: 版本号信息(需要手动指定)
:::: 旧版本名称
SET version=0.2.2
SET version=0.3.0
:::: 新版本名称
SET newVersion=0.2.3
SET newVersion=0.3.1
:::: 组织名称
SET groupName=com.github.houbb
:::: 项目名称
Expand All @@ -39,4 +39,4 @@ git status
ECHO "2. PUSH TO GITHUB DONE."

:: 发布到 maven 中央仓库
mvn clean deploy -P release
mvn clean deploy -P release
14 changes: 14 additions & 0 deletions src/main/java/com/github/houbb/pinyin/api/IPinyin.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public interface IPinyin {
* @return 结果
* @since 0.1.0
*/
@Deprecated
List<Integer> toneNumList(final String chinese, final IPinyinContext context);

/**
Expand All @@ -58,6 +59,7 @@ public interface IPinyin {
* @return 结果
* @since 0.1.0
*/
@Deprecated
List<Integer> toneNumList(final char chinese, final IPinyinContext context);

/**
Expand All @@ -67,6 +69,7 @@ public interface IPinyin {
* @return 结果
* @since 0.1.1
*/
@Deprecated
List<String> shengMuList(final String chinese, final IPinyinContext context);

/**
Expand All @@ -76,6 +79,17 @@ public interface IPinyin {
* @return 结果
* @since 0.1.1
*/
@Deprecated
List<String> yunMuList(final String chinese, final IPinyinContext context);

/**
* 相同的拼音列表
* @param pinyin 拼音
* @param sameToneNum 相同的声调
* @return 结果
* @since 0.3.0
*/
List<String> samePinyinList(String pinyin, final boolean sameToneNum, final IPinyinContext context);


}
7 changes: 7 additions & 0 deletions src/main/java/com/github/houbb/pinyin/api/IPinyinContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,11 @@ public interface IPinyinContext {
*/
String connector();

/**
* 拼音反向
*
* @return 实现
* @since 0.3.0
*/
IPinyinToneReverse pinyinToneReverse();
}
24 changes: 24 additions & 0 deletions src/main/java/com/github/houbb/pinyin/api/impl/Pinyin.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.github.houbb.pinyin.api.IPinyinContext;
import com.github.houbb.pinyin.spi.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

Expand Down Expand Up @@ -108,6 +109,29 @@ public String handle(String s) {
});
}

@Override
public List<String> samePinyinList(String pinyin, boolean sameToneNum, IPinyinContext context) {
IPinyinToneReverse pinyinToneReverse = context.pinyinToneReverse();

// 是否相同的拼音
if(sameToneNum) {
return pinyinToneReverse.getHanziList(pinyin);
}

// 构建所有的拼音
List<String> resultList = new ArrayList<>();
String pinyinRaw = pinyin.substring(0, pinyin.length()-1);
for(int i = 1; i <= 5; i++) {
String pinyinLast = pinyinRaw+ i;
List<String> characterList = pinyinToneReverse.getHanziList(pinyinLast);
if(CollectionUtil.isNotEmpty(characterList)) {
resultList.addAll(characterList);
}
}

return resultList;
}

/**
* 转换为拼音列表
* @param string 字符串
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/github/houbb/pinyin/api/impl/PinyinContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ public class PinyinContext implements IPinyinContext {
*/
private String connector;

/**
* 拼音反向
* @since 0.3.0
*/
private IPinyinToneReverse pinyinToneReverse;

/**
* 返回实例
* @since 0.1.1
Expand Down Expand Up @@ -114,4 +120,14 @@ public PinyinContext connector(String connector) {
this.connector = connector;
return this;
}

@Override
public IPinyinToneReverse pinyinToneReverse() {
return pinyinToneReverse;
}

public PinyinContext pinyinToneReverse(IPinyinToneReverse pinyinToneReverse) {
this.pinyinToneReverse = pinyinToneReverse;
return this;
}
}
Loading

0 comments on commit 6376ebb

Please sign in to comment.