SpringBoot汉字转拼音

SpringBoot汉字转拼音

引入依赖

1
2
3
4
5
<dependency>
<groupId>com.hankcs</groupId>
<artifactId>hanlp</artifactId>
<version>portable-1.8.3</version>
</dependency>

使用

汉字转拼音

1
2
3
4
5
6
import org.apache.commons.collections4.ListUtils;

public static void main(String[] args) {
List<Pinyin> pinyins = PinyinDictionary.convertToPinyin("重庆");
ListUtils.emptyIfNull(pinyins).forEach(v-> System.out.print(v.getPinyinWithoutTone()));
}

汉字首字母

1
2
3
4
5
6
import org.apache.commons.collections4.ListUtils;

public static void main(String[] args) {
List<Pinyin> pinyins = PinyinDictionary.convertToPinyin("重庆");
ListUtils.emptyIfNull(pinyins).forEach(v-> System.out.print(v.getFirstChar()));
}

汉字符号音调

1
2
3
4
5
6
import org.apache.commons.collections4.ListUtils;

public static void main(String[] args) {
List<Pinyin> pinyins = PinyinDictionary.convertToPinyin("重庆");
ListUtils.emptyIfNull(pinyins).forEach(v-> System.out.print(v.getPinyinWithToneMark()));
}

汉字数字音调

1
2
3
4
5
6
import org.apache.commons.collections4.ListUtils;

public static void main(String[] args) {
List<Pinyin> pinyins = PinyinDictionary.convertToPinyin("重庆");
ListUtils.emptyIfNull(pinyins).forEach(v-> System.out.print(v));
}

汉字声母

1
2
3
4
5
6
import org.apache.commons.collections4.ListUtils;

public static void main(String[] args) {
List<Pinyin> pinyins = PinyinDictionary.convertToPinyin("重庆");
ListUtils.emptyIfNull(pinyins).forEach(v-> System.out.print(v.getShengmu()));
}

汉字音调

1
2
3
4
5
6
import org.apache.commons.collections4.ListUtils;

public static void main(String[] args) {
List<Pinyin> pinyins = PinyinDictionary.convertToPinyin("重庆");
ListUtils.emptyIfNull(pinyins).forEach(v-> System.out.print(v.getTone()));
}
如果对您有帮助,可以打赏呦!