Skip to content

Commit

Permalink
Merge pull request belerweb#1 from AlvezKim/master
Browse files Browse the repository at this point in the history
Thanks.
  • Loading branch information
belerweb committed Dec 11, 2014
2 parents 79b44cc + dde0b3a commit 1e60c41
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,8 @@ String[] getHanyuPinyinStringArray(char ch) {
private boolean isValidRecord(String record) {
final String noneStr = "(none0)";

if ((null != record) && !record.equals(noneStr) && record.startsWith(Field.LEFT_BRACKET)
&& record.endsWith(Field.RIGHT_BRACKET)) {
return true;
} else
return false;
return (null != record) && !record.equals(noneStr) && record.startsWith(Field.LEFT_BRACKET)
&& record.endsWith(Field.RIGHT_BRACKET);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ static String convertHanyuPinyinToGwoyeuRomatzyh(String hanyuPinyinStr) {
String xpathQuery2 =
"../" + PinyinRomanizationType.GWOYEU_ROMATZYH.getTagName()
+ tones[Integer.parseInt(toneNumberStr) - 1] + "/text()";
String targetPinyinStrWithoutToneNumber = hanyuNode.xpathSelectString(xpathQuery2);

gwoyeuStr = targetPinyinStrWithoutToneNumber;
gwoyeuStr = hanyuNode.xpathSelectString(xpathQuery2);
}
} catch (ParseException e) {
e.printStackTrace();
Expand Down
12 changes: 3 additions & 9 deletions src/main/java/net/sourceforge/pinyin4j/PinyinFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,9 @@ private static String convertToneNumber2ToneMark(final String pinyinStr) {

char markedVowel = allMarkedVowelStr.charAt(vowelLocation);

StringBuffer resultBuffer = new StringBuffer();

resultBuffer.append(lowerCasePinyinStr.substring(0, indexOfUnmarkedVowel).replaceAll("v",
"ü"));
resultBuffer.append(markedVowel);
resultBuffer.append(lowerCasePinyinStr.substring(indexOfUnmarkedVowel + 1,
lowerCasePinyinStr.length() - 1).replaceAll("v", "ü"));

return resultBuffer.toString();
return lowerCasePinyinStr.substring(0, indexOfUnmarkedVowel).replaceAll("v",
"ü") + markedVowel + lowerCasePinyinStr.substring(indexOfUnmarkedVowel + 1,
lowerCasePinyinStr.length() - 1).replaceAll("v", "ü");

} else
// error happens in the procedure of locating vowel
Expand Down
81 changes: 42 additions & 39 deletions src/main/java/net/sourceforge/pinyin4j/PinyinHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
* @author Li Min ([email protected])
*/
public class PinyinHelper {

private static final String[] ARR_EMPTY = {};
private static final String EMPTY = "";
/**
* Get all unformmatted Hanyu Pinyin presentations of a single Chinese
* character (both Simplified and Tranditional)
Expand Down Expand Up @@ -73,14 +76,14 @@ static public String[] toHanyuPinyinStringArray(char ch) {
* describes the desired format of returned Hanyu Pinyin String
*
* @return a String array contains all Hanyu Pinyin presentations with tone
* numbers; return null for non-Chinese character
*
* numbers; return empty string for non-Chinese character
*
* @throws BadHanyuPinyinOutputFormatCombination
* if certain combination of output formats happens
*
*
* @see HanyuPinyinOutputFormat
* @see BadHanyuPinyinOutputFormatCombination
*
*
*/
static public String[] toHanyuPinyinStringArray(char ch, HanyuPinyinOutputFormat outputFormat)
throws BadHanyuPinyinOutputFormatCombination {
Expand All @@ -90,7 +93,7 @@ static public String[] toHanyuPinyinStringArray(char ch, HanyuPinyinOutputFormat
/**
* Return the formatted Hanyu Pinyin representations of the given Chinese
* character (both in Simplified and Tranditional) in array format.
*
*
* @param ch
* the given Chinese character
* @param outputFormat
Expand All @@ -111,12 +114,12 @@ static private String[] getFormattedHanyuPinyinStringArray(char ch,
return pinyinStrArray;

} else
return null;
return ARR_EMPTY;
}

/**
* Delegate function
*
*
* @param ch
* the given Chinese character
* @return unformatted Hanyu Pinyin strings; null if the record is not found
Expand All @@ -128,15 +131,15 @@ private static String[] getUnformattedHanyuPinyinStringArray(char ch) {
/**
* Get all unformmatted Tongyong Pinyin presentations of a single Chinese
* character (both Simplified and Tranditional)
*
*
* @param ch
* the given Chinese character
*
*
* @return a String array contains all unformmatted Tongyong Pinyin
* presentations with tone numbers; null for non-Chinese character
*
*
* @see #toHanyuPinyinStringArray(char)
*
*
*/
static public String[] toTongyongPinyinStringArray(char ch) {
return convertToTargetPinyinStringArray(ch, PinyinRomanizationType.TONGYONG_PINYIN);
Expand All @@ -145,15 +148,15 @@ static public String[] toTongyongPinyinStringArray(char ch) {
/**
* Get all unformmatted Wade-Giles presentations of a single Chinese
* character (both Simplified and Tranditional)
*
*
* @param ch
* the given Chinese character
*
*
* @return a String array contains all unformmatted Wade-Giles presentations
* with tone numbers; null for non-Chinese character
*
*
* @see #toHanyuPinyinStringArray(char)
*
*
*/
static public String[] toWadeGilesPinyinStringArray(char ch) {
return convertToTargetPinyinStringArray(ch, PinyinRomanizationType.WADEGILES_PINYIN);
Expand All @@ -162,16 +165,16 @@ static public String[] toWadeGilesPinyinStringArray(char ch) {
/**
* Get all unformmatted MPS2 (Mandarin Phonetic Symbols 2) presentations of
* a single Chinese character (both Simplified and Tranditional)
*
*
* @param ch
* the given Chinese character
*
*
* @return a String array contains all unformmatted MPS2 (Mandarin Phonetic
* Symbols 2) presentations with tone numbers; null for non-Chinese
* character
*
*
* @see #toHanyuPinyinStringArray(char)
*
*
*/
static public String[] toMPS2PinyinStringArray(char ch) {
return convertToTargetPinyinStringArray(ch, PinyinRomanizationType.MPS2_PINYIN);
Expand All @@ -180,15 +183,15 @@ static public String[] toMPS2PinyinStringArray(char ch) {
/**
* Get all unformmatted Yale Pinyin presentations of a single Chinese
* character (both Simplified and Tranditional)
*
*
* @param ch
* the given Chinese character
*
*
* @return a String array contains all unformmatted Yale Pinyin
* presentations with tone numbers; null for non-Chinese character
*
*
* @see #toHanyuPinyinStringArray(char)
*
*
*/
static public String[] toYalePinyinStringArray(char ch) {
return convertToTargetPinyinStringArray(ch, PinyinRomanizationType.YALE_PINYIN);
Expand All @@ -203,7 +206,7 @@ static public String[] toYalePinyinStringArray(char ch) {
* @return string representations of target Chinese Romanization system
* corresponding to the given Chinese character in array format;
* null if error happens
*
*
* @see PinyinRomanizationType
*/
private static String[] convertToTargetPinyinStringArray(char ch,
Expand All @@ -222,21 +225,21 @@ private static String[] convertToTargetPinyinStringArray(char ch,
return targetPinyinStringArray;

} else
return null;
return ARR_EMPTY;
}

/**
* Get all unformmatted Gwoyeu Romatzyh presentations of a single Chinese
* character (both Simplified and Tranditional)
*
*
* @param ch
* the given Chinese character
*
*
* @return a String array contains all unformmatted Gwoyeu Romatzyh
* presentations with tone numbers; null for non-Chinese character
*
*
* @see #toHanyuPinyinStringArray(char)
*
*
*/
static public String[] toGwoyeuRomatzyhStringArray(char ch) {
return convertToGwoyeuRomatzyhStringArray(ch);
Expand All @@ -245,10 +248,10 @@ static public String[] toGwoyeuRomatzyhStringArray(char ch) {
/**
* @param ch
* the given Chinese character
*
*
* @return Gwoyeu Romatzyh string representations corresponding to the given
* Chinese character in array format; null if error happens
*
*
* @see PinyinRomanizationType
*/
private static String[] convertToGwoyeuRomatzyhStringArray(char ch) {
Expand All @@ -265,19 +268,19 @@ private static String[] convertToGwoyeuRomatzyhStringArray(char ch) {
return targetPinyinStringArray;

} else
return null;
return ARR_EMPTY;
}

/**
* Get a string which all Chinese characters are replaced by corresponding
* main (first) Hanyu Pinyin representation.
*
*
* <p>
* <b>Special Note</b>: If the return contains "none0", that means that
* Chinese character is in Unicode CJK talbe, however, it has not
* pronounciation in Chinese. <b> This interface will be removed in next
* release. </b>
*
*
* @param str
* A given string contains Chinese characters
* @param outputFormat
Expand All @@ -289,15 +292,15 @@ private static String[] convertToGwoyeuRomatzyhStringArray(char ch) {
* @return a String identical to the original one but all recognizable
* Chinese characters are converted into main (first) Hanyu Pinyin
* representation
*
*
* @deprecated DO NOT use it again because the first retrived pinyin string
* may be a wrong pronouciation in a certain sentence context.
* <b> This interface will be removed in next release. </b>
*/
static public String toHanyuPinyinString(String str, HanyuPinyinOutputFormat outputFormat,
String seperater) throws BadHanyuPinyinOutputFormatCombination {

StringBuffer resultPinyinStrBuf = new StringBuffer();
StringBuilder resultPinyinStrBuf = new StringBuilder();

for (int i = 0; i < str.length(); i++) {
String mainPinyinStrOfChar = getFirstHanyuPinyinString(str.charAt(i), outputFormat);
Expand All @@ -318,14 +321,14 @@ static public String toHanyuPinyinString(String str, HanyuPinyinOutputFormat out
/**
* Get the first Hanyu Pinyin of a Chinese character <b> This function will
* be removed in next release. </b>
*
*
* @param ch
* The given Unicode character
* @param outputFormat
* Describes the desired format of returned Hanyu Pinyin string
* @return Return the first Hanyu Pinyin of given Chinese character; return
* null if the input is not a Chinese character
*
*
* @deprecated DO NOT use it again because the first retrived pinyin string
* may be a wrong pronouciation in a certain sentence context.
* <b> This function will be removed in next release. </b>
Expand All @@ -337,7 +340,7 @@ static private String getFirstHanyuPinyinString(char ch, HanyuPinyinOutputFormat
if ((null != pinyinStrArray) && (pinyinStrArray.length > 0)) {
return pinyinStrArray[0];
} else {
return null;
return EMPTY;
}
}

Expand Down

0 comments on commit 1e60c41

Please sign in to comment.