Skip to content

Commit 0bf8045

Browse files
committed
somewhat working pinyinify, need more testing though
1 parent 1098558 commit 0bf8045

File tree

2 files changed

+91
-84
lines changed

2 files changed

+91
-84
lines changed

pinyinify.js

Lines changed: 82 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,134 @@
11
var toneMarks = {
2-
a: ["a", "\u0101", "\u00e1", "\u01ce", "\u00e0", "a"],
3-
e: ["e", "\u0113", "\u00e9", "\u011b", "\u00e8", "e"],
4-
i: ["i", "\u012b", "\u00ed", "\u01d0", "\u00ec", "i"],
5-
o: ["o", "\u014d", "\u00f3", "\u01d2", "\u00f2", "o"],
6-
u: ["u", "\u016b", "\u00fa", "\u01d4", "\u00f9", "u"],
7-
v: ["\u00fc", "\u01d6", "\u01d8", "\u01da", "\u01dc", "\u00fc"]
2+
a: ["a", "\u0101", "\u00e1", "\u01ce", "\u00e0", "a"],
3+
e: ["e", "\u0113", "\u00e9", "\u011b", "\u00e8", "e"],
4+
i: ["i", "\u012b", "\u00ed", "\u01d0", "\u00ec", "i"],
5+
o: ["o", "\u014d", "\u00f3", "\u01d2", "\u00f2", "o"],
6+
u: ["u", "\u016b", "\u00fa", "\u01d4", "\u00f9", "u"],
7+
v: ["\u00fc", "\u01d6", "\u01d8", "\u01da", "\u01dc", "\u00fc"]
88
};
99

1010
String.prototype.isAlpha = function() {
1111

12-
return /^[A-Za-z]$/.test(this);
12+
return /^[A-Za-z]$/.test(this);
1313
}
1414

1515
String.prototype.isPinyinVowel = function() {
1616

17-
return /^[aeiouv\u00fc]$/.test(this);
17+
return /^[aeiouv\u00fc]$/.test(this);
1818
}
1919

2020
String.prototype.lastIndexOfRegex = function(regExp) {
2121

22-
var lastIndex = -1;
23-
for (var i = 0; i < this.length; i++) {
22+
var lastIndex = -1;
23+
for (var i = 0; i < this.length; i++) {
2424

25-
if (regExp.test(this.charAt(i))) {
25+
if (regExp.test(this.charAt(i))) {
2626

27-
lastIndex = i;
28-
}
29-
}
27+
lastIndex = i;
28+
}
29+
}
3030

31-
return lastIndex;
31+
return lastIndex;
3232
}
3333

3434
String.prototype.replaceAt = function(index, replacement) {
3535

36-
//TODO
36+
if (index >= 0 && index < this.length && typeof replacement === "string") {
37+
38+
return this.substring(0, index) + replacement
39+
+ this.substring(index + 1);
40+
} else {
41+
42+
return this;
43+
}
3744
}
3845

3946
/**
4047
* Takes a single pinyin word using tone numbers and converts to tone symbols.
4148
*/
4249
String.prototype.convertPinyin = function() {
4350

44-
var str = this.toLocaleLowerCase();
51+
var str = this.toLocaleLowerCase();
4552

46-
var toneNumIndex = str.search(/[1-5]/);
47-
var firstVowelIndex = str.search(/[aeiouv\u00fc]/);
48-
if (str.length > 7 || toneNumIndex < 1 ||
49-
toneNumIndex !== str.length - 1 ||
50-
firstVowelIndex < 0) {
53+
var toneNumIndex = str.search(/[1-5]/);
54+
var firstVowelIndex = str.search(/[aeiouv\u00fc]/);
55+
if (str.length > 7 || toneNumIndex < 1 ||
56+
toneNumIndex !== str.length - 1 ||
57+
firstVowelIndex < 0) {
5158

52-
console.log("String.prototype.convertPinyin:" + this +
53-
" is not a valid pinyin word.")
54-
return this;
55-
}
59+
console.log("String.prototype.convertPinyin:" + this +
60+
" is not a valid pinyin word.")
61+
return this;
62+
}
5663

57-
var toneNum = parseInt(str[toneNumIndex]);
58-
if (/[ae]/.test(str)) {
64+
var toneNum = parseInt(str[toneNumIndex]);
65+
if (/[ae]/.test(str)) {
5966

60-
var index = str.search(/[ae]/);
61-
str = str.replaceAt(index, toneMarks[str.charAt(index)][toneNum]);
62-
} else if (/ou/.test(str)) {
67+
var index = str.search(/[ae]/);
68+
str = str.replaceAt(index, toneMarks[str.charAt(index)][toneNum]);
69+
} else if (/ou/.test(str)) {
6370

64-
var index = str.search(/ou/);
65-
str = str.replaceAt(index, toneMarks[str.charAt(index)][toneNum]);
66-
} else {
71+
var index = str.search(/ou/);
72+
str = str.replaceAt(index, toneMarks[str.charAt(index)][toneNum]);
73+
} else {
6774

68-
var index = str.lastIndexOfRegex(/[aeiouv\u00fc]/);
69-
var vowel = str.charAt(index);
70-
if (vowel == "\u00fc") {
75+
var index = str.lastIndexOfRegex(/[aeiouv\u00fc]/);
76+
var vowel = str.charAt(index);
77+
if (vowel == "\u00fc") {
7178

72-
vowel = "v";
73-
}
74-
str = str.replaceAt(index, toneMarks[vowel][toneNum]);
75-
}
79+
vowel = "v";
80+
}
81+
str = str.replaceAt(index, toneMarks[vowel][toneNum]);
82+
}
7683

77-
return str;
84+
str = str.substring(0, str.length - 1);
85+
return str;
7886
}
7987

8088
var pinyinify = function(str) {
8189

82-
if (typeof str !== 'string') {
90+
if (typeof str !== 'string') {
8391

84-
return str;
85-
}
92+
return str;
93+
}
8694

87-
var res = "";
88-
var i = 0;
89-
while (str.length > 0) {
95+
var res = "";
96+
var i = 0;
97+
while (str.length > 0) {
9098

91-
var char = str.charAt(i);
92-
if (char.isAlpha()) {
99+
var char = str.charAt(i);
100+
if (char.isAlpha()) {
93101

94-
if (i !== 0) {
102+
if (i !== 0) {
95103

96-
res += str.substring(0, i);
97-
str = str.substring(i);
98-
i = 0;
99-
}
100-
var toneNumIndex = str.search(/[1-5]/);
101-
if (toneNumIndex > 0 && toneNumIndex < 7) {
104+
res += str.substring(0, i);
105+
str = str.substring(i);
106+
i = 0;
107+
}
108+
var toneNumIndex = str.search(/[1-5]/);
109+
if (toneNumIndex > 0 && toneNumIndex < 7) {
102110

103-
res += str.substring(0, toneNumIndex + 1).convertPinyin();
104-
str = str.substring(toneNumIndex + 1);
105-
} else {
111+
res += str.substring(0, toneNumIndex + 1).convertPinyin();
112+
str = str.substring(toneNumIndex + 1);
113+
} else {
106114

107-
var whitespaceIndex = str.search(/\s/);
108-
if (whitespaceIndex < 0) {
115+
var whitespaceIndex = str.search(/\s/);
116+
if (whitespaceIndex < 0) {
109117

110-
res += str.substring(0);
111-
str = "";
112-
} else {
118+
res += str.substring(0);
119+
str = "";
120+
} else {
113121

114-
res += str.substring(0, whitespaceIndex + 1);
115-
str = str.substring(whitespaceIndex + 1);
116-
}
117-
}
122+
res += str.substring(0, whitespaceIndex + 1);
123+
str = str.substring(whitespaceIndex + 1);
124+
}
125+
}
118126

119-
} else {
127+
} else {
120128

121-
i++;
122-
}
123-
}
129+
i++;
130+
}
131+
}
124132

125-
return res;
133+
return res;
126134
}

test.html

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4-
<script>
5-
var submit = function(str) {
6-
// document.getElementById('result').innerHTML = pinyinify(str);
7-
document.getElementById('result').innerHTML = /ou/.test(str);
8-
}
9-
</script>
10-
<script src="pinyinify.js"></script>
4+
<script>
5+
var submit = function(str) {
6+
document.getElementById('result').innerHTML = pinyinify(str);
7+
}
8+
</script>
9+
<script src="pinyinify.js"></script>
1110
</head>
1211
<body>
13-
<input type="text" id="textfield" name="textfield">
14-
<button onclick="submit(document.getElementById('textfield').value)" name="Submit">Submit</button>
15-
<p id="result"></p>
12+
<input type="text" id="textfield" name="textfield">
13+
<button onclick="submit(document.getElementById('textfield').value)" name="Submit">Submit</button>
14+
<p id="result"></p>
1615
</body>
1716
</html>

0 commit comments

Comments
 (0)