Skip to content

Instantly share code, notes, and snippets.

@enjalot
Created March 10, 2014 03:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enjalot/9459053 to your computer and use it in GitHub Desktop.
Save enjalot/9459053 to your computer and use it in GitHub Desktop.
how pinyin works
{"description":"how pinyin works","endpoint":"","display":"svg","public":true,"require":[{"name":"crossfilter","url":"http://square.github.io/crossfilter/crossfilter.v1.min.js"}],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"pingpong","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"ajax-caching":true,"thumbnail":"http://i.imgur.com/lvEQSlf.png"}
var chars;
d3.csv("https://s3.amazonaws.com/pinyin/lists/cedict-singles.csv", function(err, data) {
chars = data;
vis();
})
//http://en.wikipedia.org/wiki/Pinyin#Initials
// cheating a little bit to go more with how we type than the official
// pinyin system. technically w and y arent initials, but for all practical
// typing purposes they are
var initials = [
"zh", "ch", "sh", "b", "p", "m", "f", "d", "t", "n", "l", "g", "k", "h", "j",
"q", "x", "r", "z", "c", "s", "w", "y"
];
var initialsLen = initials.length;
//http://en.wikipedia.org/wiki/Pinyin#Finals
var finals = [
"ang", "an", "ai", "ao",
"iang", "ian", "ia",
"iong", "ie", "ing", "in", "iu",
"u:an", "u:e", "u:n", "u:",
"uang", "uan", "ua", "ui", "un", "uo",
"eng", "en", "ei", "e",
"ou", "ong",
"i", "o", "a"
];
var finalsLen = finals.length;
function vis() {
var xf = crossfilter(chars.slice(100));
var starts = xf.dimension(function(d) {
// split on , for chars with multiple pronounciations
// we go with the first pinyin for now.
var pin = d.pinyin.split(",")[0];
// find out which initial matches
for(var i = 0; i < initialsLen; i++) {
if(pin.indexOf(initials[i]) === 0) return initials[i];
}
return "";
});
var ends = xf.dimension(function(d) {
var pin = d.pinyin.split(",")[0];
// find out which final matches
for(var i = 0; i < finalsLen; i++) {
if(pin.indexOf(finals[i]) === 0) return finals[i];
}
return "";
});
/*
var tones = xf.dimension(function(d) {
return false;
});
*/
var groups = starts.group().all()
//console.log(groups)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment