Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
suggest-task
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
田川
suggest-task
Commits
04ea135a
Commit
04ea135a
authored
Jan 26, 2022
by
xupeng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
扩展拼音工具类
parent
8bdd2e20
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
163 additions
and
2 deletions
+163
-2
PinYinUtils.java
suggest-task/src/main/java/com/secoo/so/suggest/util/PinYinUtils.java
+162
-1
StringUtils.java
suggest-task/src/main/java/com/secoo/so/suggest/util/StringUtils.java
+1
-1
No files found.
suggest-task/src/main/java/com/secoo/so/suggest/util/PinYinUtils.java
View file @
04ea135a
package
com
.
secoo
.
so
.
suggest
.
util
;
package
com
.
secoo
.
so
.
suggest
.
util
;
import
com.github.stuxuhai.jpinyin.ChineseHelper
;
import
com.github.stuxuhai.jpinyin.ChineseHelper
;
import
com.github.stuxuhai.jpinyin.PinyinFormat
;
import
com.github.stuxuhai.jpinyin.PinyinHelper
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.lang3.StringUtils
;
@Slf4j
@Slf4j
public
class
PinYinUtils
{
public
class
PinYinUtils
{
...
@@ -20,7 +23,6 @@ public class PinYinUtils {
...
@@ -20,7 +23,6 @@ public class PinYinUtils {
return
tempStr
;
return
tempStr
;
}
}
/**
/**
* 繁体转换为简体
* 繁体转换为简体
*/
*/
...
@@ -35,5 +37,164 @@ public class PinYinUtils {
...
@@ -35,5 +37,164 @@ public class PinYinUtils {
return
tempStr
;
return
tempStr
;
}
}
/**
* 转换为有声调的拼音字符串
*
* @param str 汉字
* @return 有声调的拼音字符串
*/
public
static
String
changeToToneMarkPinYin
(
String
str
)
{
String
tempStr
=
null
;
try
{
tempStr
=
PinyinHelper
.
convertToPinyinString
(
str
,
" "
,
PinyinFormat
.
WITH_TONE_MARK
);
}
catch
(
Exception
e
)
{
log
.
error
(
"changeToToneMarkPinYin error"
,
e
);
}
return
tempStr
;
}
/**
* 转换为数字声调字符串
*
* @param str 需转换的汉字
* @return 转换完成的拼音字符串
*/
public
static
String
changeToToneNumberPinYin
(
String
str
)
{
String
tempStr
=
null
;
try
{
tempStr
=
PinyinHelper
.
convertToPinyinString
(
str
,
" "
,
PinyinFormat
.
WITH_TONE_NUMBER
);
}
catch
(
Exception
e
)
{
log
.
error
(
"changeToToneNumberPinYin error"
,
e
);
}
return
tempStr
;
}
/**
* 转换为不带音调的拼音字符串
*
* @param str 需转换的汉字
* @return 拼音字符串
*/
public
static
String
changeToWithoutTonePinYin
(
String
str
)
{
return
changeToWithoutTonePinYin
(
str
,
" "
);
}
/**
* 转换为不带音调的拼音字符串
*
* @param str 需转换的汉字
* @return 拼音字符串
*/
public
static
String
changeToWithoutTonePinYinNoSeparator
(
String
str
)
{
return
changeToWithoutTonePinYin
(
str
,
""
);
}
/**
* 转换为不带音调的拼音字符串
*
* @param str 需转换的汉字
* @return 拼音字符串
*/
public
static
String
changeToWithoutTonePinYin
(
String
str
,
String
separator
)
{
String
tempStr
=
null
;
try
{
tempStr
=
PinyinHelper
.
convertToPinyinString
(
str
,
separator
,
PinyinFormat
.
WITHOUT_TONE
);
}
catch
(
Exception
e
)
{
log
.
error
(
"changeToWithoutTonePinYin error"
,
e
);
}
return
tempStr
;
}
/**
* 转换为每个汉字对应拼音首字母字符串
*
* @param str 需转换的汉字
* @return 拼音字符串
*/
public
static
String
changeToGetShortPinYin
(
String
str
)
{
String
tempStr
=
null
;
try
{
tempStr
=
PinyinHelper
.
getShortPinyin
(
str
);
}
catch
(
Exception
e
)
{
log
.
error
(
"changeToGetShortPinYin error"
,
e
);
}
return
tempStr
;
}
/**
* 检查汉字是否为多音字
*
* @param str 需检查的汉字
* @return true 多音字,false 不是多音字
*/
public
static
boolean
checkHasMultiPinyin
(
char
str
)
{
boolean
check
=
false
;
try
{
check
=
PinyinHelper
.
hasMultiPinyin
(
str
);
}
catch
(
Exception
e
)
{
log
.
error
(
"checkHasMultiPinyin error"
,
e
);
}
return
check
;
}
/**
* 是否是拼音
*
* @param str
* @return
*/
public
static
boolean
isPinYin
(
String
str
)
{
if
(
org
.
apache
.
commons
.
lang3
.
StringUtils
.
isBlank
(
str
))
{
return
false
;
}
char
[]
chars
=
str
.
toCharArray
();
for
(
char
c
:
chars
)
{
boolean
isPinYin
=
(
c
>=
'a'
&&
c
<=
'z'
)
||
(
c
>=
'A'
&&
c
<=
'Z'
);
if
(!
isPinYin
)
{
return
false
;
}
}
return
true
;
}
/**
* 是否首字母是拼音
*/
public
static
boolean
isFirstPinYin
(
String
str
)
{
if
(
StringUtils
.
isBlank
(
str
))
{
return
false
;
}
char
[]
chars
=
str
.
toCharArray
();
char
c
=
chars
[
0
];
return
(
c
>=
'a'
&&
c
<=
'z'
)
||
(
c
>=
'A'
&&
c
<=
'Z'
);
}
/**
* 是否是英文词
*/
public
static
boolean
isEnglishWord
(
String
str
)
{
if
(
StringUtils
.
isBlank
(
str
))
{
return
false
;
}
char
[]
chars
=
str
.
toCharArray
();
for
(
char
c
:
chars
)
{
boolean
isValid
=
(
c
>=
'a'
&&
c
<=
'z'
)
||
(
c
>=
'A'
&&
c
<=
'Z'
)
||
Character
.
isWhitespace
(
c
);
if
(!
isValid
)
{
return
false
;
}
}
return
true
;
}
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
PinYinUtils
.
convertToTraditionalChinese
(
"中国人"
));
System
.
out
.
println
(
PinYinUtils
.
convertToSimplifiedChinese
(
"中国人"
));
System
.
out
.
println
(
PinYinUtils
.
changeToWithoutTonePinYin
(
"中国人"
));
System
.
out
.
println
(
PinYinUtils
.
changeToWithoutTonePinYinNoSeparator
(
"博柏利 运动鞋"
));
System
.
out
.
println
(
PinYinUtils
.
changeToWithoutTonePinYinNoSeparator
(
"silk in"
));
System
.
out
.
println
(
PinYinUtils
.
changeToWithoutTonePinYinNoSeparator
(
"化妆品bb霜遮瑕"
));
}
}
}
suggest-task/src/main/java/com/secoo/so/suggest/util/StringUtils.java
View file @
04ea135a
...
@@ -2017,7 +2017,7 @@ public abstract class StringUtils {
...
@@ -2017,7 +2017,7 @@ public abstract class StringUtils {
}
}
/**
/**
* 判断是否是英文字符
传你
* 判断是否是英文字符
串
*/
*/
public
static
boolean
isEnStr
(
String
word
)
{
public
static
boolean
isEnStr
(
String
word
)
{
boolean
result
=
word
.
matches
(
"[a-zA-Z]+"
);
boolean
result
=
word
.
matches
(
"[a-zA-Z]+"
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment