@Override
public CalculateWordCountDTO calculateWordCount(String str) throws ServiceException {
CalculateWordCountDTO rtn = new CalculateWordCountDTO();
int wordCount = 0;
int characterCount = 0;
int chineseCount = 0;
int englishLetterCount = 0;
int figureCount = 0;
int spaceCount = 0;
int symbolCount = 0;
int lineCount = 0;
try {
do {
if (ValidateUtils.isNullOrEmpty(str)) {
rtn.setCharacterCount(characterCount)
.setChineseCount(chineseCount)
.setEnglishLetterCount(englishLetterCount)
.setFigureCount(figureCount)
.setSpaceCount(spaceCount)
.setSymbolCount(symbolCount)
.setWordCount(wordCount)
.setLineCount(lineCount);
break;
}
boolean lineFlag = false;
wordCount = str.length();
for (int i = 0; i < str.length(); i++) {
String tempStr = str.substring(i, i + 1);
String pattern = "\\r";
if (Pattern.matches(pattern, tempStr)) {
lineCount++;
lineFlag = true;
continue;
}
pattern = "\\n";
if (Pattern.matches(pattern, tempStr)) {
if (!lineFlag) {
lineCount++;
lineFlag = false;
}
continue;
}
characterCount += tempStr.getBytes("GBK").length;
if (isChinese(tempStr)) {
chineseCount++;
} else if (isEnglishLetter(tempStr)) {
englishLetterCount++;
} else if (isFigure(tempStr)) {
figureCount++;
} else if (isSpace(tempStr)) {
spaceCount++;
} else {
symbolCount++;
}
}
rtn.setCharacterCount(characterCount)
.setChineseCount(chineseCount)
.setEnglishLetterCount(englishLetterCount)
.setFigureCount(figureCount)
.setSpaceCount(spaceCount)
.setSymbolCount(symbolCount)
.setWordCount(wordCount-lineCount)
.setLineCount(lineCount);
} while (false);
} catch (Exception e) {
throw new ServiceException(e, ErrorCodeEnum.UNKNOWN.getErrorCode());
}
return rtn;
}
/***
* 判断字符是否为中文
* @param str 需要判断的字符
* @return 中文返回true,非中文返回false
*/
private boolean isChinese(String str) {
String pattern = "^[\\u4E00-\\u9FA5]+$";
return Pattern.matches(pattern, str);
}
/***
* 判断字符是否为英文字母
* @param str 需要判断的字符
*/
private boolean isEnglishLetter(String str) {
String pattern = "^[a-zA-Z]+$";
return Pattern.matches(pattern, str);
}
/***
* 判断字符是否为数字
* @param str 需要判断的字符
*/
private boolean isFigure(String str) {
String pattern = "^[0-9]+$";
return Pattern.matches(pattern, str);
}
/***
* 判断字符是否为空格
* @param str 需要判断的字符
*/
private boolean isSpace(String str) {
String pattern = "\\s";
return Pattern.matches(pattern, str);
}