Skip to content

Commit 1098558

Browse files
committed
almost finished first draft, untested
1 parent 6778086 commit 1098558

File tree

3 files changed

+74
-14
lines changed

3 files changed

+74
-14
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@ The function `pinyinify()` takes a string parameter, which can be one of the fol
88
+ A pinyin word or phrase using tone numbers. Examples: `mao1`, `chi1fan4`, `wo3 men5`. For simplicity's sake, the function will process most words with tone numbers, including ones that may not be valid pinyin.
99
+ A string containing both pinyin words and non-pinyin words, characters, or symbols. Example: `My Chinese name is yang2kai3wen2.` **Be careful:** any word that resembles the structure of pinyin (has a number at the end of it, for example) may be given tone marks. Therefore, it is recommended that you avoid this option if possible.
1010

11-
If you pass a string that meets one of the requirements above, the function will return a string with the tone numbers converted to tone marks. Otherwise, it will return an unchanged string.
11+
If you pass a string that meets one of the requirements above, the function will return a string with the tone numbers converted to tone marks. Otherwise, it will return an unchanged string.
12+
13+
## Limitations
14+
<!-- TODO lowercase only, incomplete validation, etc.-->

pinyinify.js

Lines changed: 69 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
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"]
8+
};
19

210
String.prototype.isAlpha = function() {
311

@@ -9,18 +17,64 @@ String.prototype.isPinyinVowel = function() {
917
return /^[aeiouv\u00fc]$/.test(this);
1018
}
1119

20+
String.prototype.lastIndexOfRegex = function(regExp) {
21+
22+
var lastIndex = -1;
23+
for (var i = 0; i < this.length; i++) {
24+
25+
if (regExp.test(this.charAt(i))) {
26+
27+
lastIndex = i;
28+
}
29+
}
30+
31+
return lastIndex;
32+
}
33+
34+
String.prototype.replaceAt = function(index, replacement) {
35+
36+
//TODO
37+
}
38+
1239
/**
13-
* Unicode tone marks (in order)
14-
* a: u0101 u00e1 u01ce u00e0
15-
* e: u0113 u00e9 u011b u00e8
16-
* i: u012b u00ed u01d0 u00ec
17-
* o: u014d u00f3 u01d2 u00f2
18-
* u: u016b u00fa u01d4 u00f9
19-
* v: u01d6 u01d8 u01da u01dc
40+
* Takes a single pinyin word using tone numbers and converts to tone symbols.
2041
*/
2142
String.prototype.convertPinyin = function() {
2243

23-
//TODO
44+
var str = this.toLocaleLowerCase();
45+
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) {
51+
52+
console.log("String.prototype.convertPinyin:" + this +
53+
" is not a valid pinyin word.")
54+
return this;
55+
}
56+
57+
var toneNum = parseInt(str[toneNumIndex]);
58+
if (/[ae]/.test(str)) {
59+
60+
var index = str.search(/[ae]/);
61+
str = str.replaceAt(index, toneMarks[str.charAt(index)][toneNum]);
62+
} else if (/ou/.test(str)) {
63+
64+
var index = str.search(/ou/);
65+
str = str.replaceAt(index, toneMarks[str.charAt(index)][toneNum]);
66+
} else {
67+
68+
var index = str.lastIndexOfRegex(/[aeiouv\u00fc]/);
69+
var vowel = str.charAt(index);
70+
if (vowel == "\u00fc") {
71+
72+
vowel = "v";
73+
}
74+
str = str.replaceAt(index, toneMarks[vowel][toneNum]);
75+
}
76+
77+
return str;
2478
}
2579

2680
var pinyinify = function(str) {
@@ -37,14 +91,17 @@ var pinyinify = function(str) {
3791
var char = str.charAt(i);
3892
if (char.isAlpha()) {
3993

40-
res += str.substring(0, i);
41-
str = str.substring(i);
42-
i = 0;
94+
if (i !== 0) {
95+
96+
res += str.substring(0, i);
97+
str = str.substring(i);
98+
i = 0;
99+
}
43100
var toneNumIndex = str.search(/[1-5]/);
44101
if (toneNumIndex > 0 && toneNumIndex < 7) {
102+
45103
res += str.substring(0, toneNumIndex + 1).convertPinyin();
46104
str = str.substring(toneNumIndex + 1);
47-
48105
} else {
49106

50107
var whitespaceIndex = str.search(/\s/);

test.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<script>
55
var submit = function(str) {
66
// document.getElementById('result').innerHTML = pinyinify(str);
7-
document.getElementById('result').innerHTML = isAlpha(str);
7+
document.getElementById('result').innerHTML = /ou/.test(str);
88
}
99
</script>
1010
<script src="pinyinify.js"></script>

0 commit comments

Comments
 (0)