We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Random kanji generator
Page Index Toggle Pages: 1
Random kanji generator (Read 2723 times)
Random kanji generator
Feb 25th, 2010, 8:09pm
 
Hello, newbie here. I would like to make random japanese characters (specifically kanji) appear in a sketch. I'm thinking that a random number generator could make numbers that would be read as unicode codes. Can anyone help me with this? Is this possible? Thanks!
Re: Random kanji generator
Reply #1 - Feb 25th, 2010, 11:07pm
 
It is possible, but I think you have to define several ranges. At least if you want to display katakana & hiragana along with kanji of various level.
You must use loadFont of a font with characters in the right ranges (Arial Unicode MS for example, or perhaps Cyberbit, Code2002 or MS Gothic/MS Mincho. See also Large, multi-script Unicode fonts.
By loading it in JAVA2D mode (default), it isn't converted to bitmap, so memory is preserved.

According to Wikipedia, the needed ranges are quite compact:
U+3040–U+309F Hiragana
U+30A0–U+30FF Katakana
U+4E00–U+9FBF Kanj
So we need to draw a number in the range 0 to (0xA0-0x40)+(0x100-0xA0)+(0x9FC0-0x4E00)=0x5280 (thanks, Google, for the answer on the computation...) and map it to the correct ranges.
Note that the said kanji range is actually the CJK Unified Ideographs range, grouping Chinese, Japanese and Korean characters, so it is actually a superset of what we need in Japanese.
It could be interesting to limit ourself to the jōyō kanji list, the base 1945 characters first learned by students of Japanese language.
I see, for example, a list in Jouyou kanji list page, need to extract the list of Unicode chars...
Mmm, can get this list from the kanjidic file with a simple grep " G[1-8] " kanjidic, it is easy enough to get the Unicode codes then.
Re: Random kanji generator
Reply #2 - Feb 28th, 2010, 9:37pm
 
Gotcha. But after the number ranges have been derived, how would I then go about turning numbers into text? Sorry, still new at this, so maybe you're assuming I know things that I don't. Is there a function that would take a number as an input and display a character as an output? Or would I have to manually create an array, and link numbers to characters?
Re: Random kanji generator
Reply #3 - Feb 28th, 2010, 11:16pm
 
It is quite simple, the char type in Java is just a number representing a character (or a character symbolized by its Unicode codepoint).
And Processing is able to show a single character (actually it decomposes strings into chars, so that's natural for it).

As you guessed, I am interested by the topic. So I started my own sketch. If you just want a final result, just wait a bit. If you want to develop your own, I can refrain from showing my sketch (only in early stages, anyway) if you want.

I just made a little proof-of-concept sketch, to test font usage (discovered I had to use a trick with createFont to avoid OutOfMemory error) and char display.
[EDIT: Added random selection of kana, pos, colors, etc.]
[EDIT 2: Added more fonts, discarding them if not found on system]
Code:
boolean bDebug = false;
String[] fonts =
{
"Arial Unicode MS", "MS Gothic", "MS Mincho",
"Batang", "Datum", "Gulim",
"MingLiU", "SimHei", "SimSun"
};
PFont[] jFonts = new PFont[fonts.length];
char character;
int posX, posY;

void setup()
{
size(700, 700);
frameRate(1); // 1 frame per second... OK for a slideshow!

// Define a dummy charset (1 char to use less memory)
// because Processing really creates the bitmap font
// even if it won't use it in this mode...
// So if we don't do that, we will get an out of memory error!
char[] dummy = new char[1]; dummy[0] = ' ';
// Create fonts having Japanese chars
int count = 0;
for (int i = 0; i < fonts.length; i++)
{
jFonts[count] = createFont(fonts[i], 128, true, dummy);
if (jFonts[count].getFont().canDisplay(demoChars[0]))
{
// This font is on the system and supports CJK
count++;
println("Using " + fonts[i]);
}
else
{
println(fonts[i] + " not found.");
}
}
// Discard unsupported fonts
jFonts = (PFont[]) subset(jFonts, 0, count);
println(count + " fonts with CJK support found on this system.");
}

char[] demoChars =
{
// The 10 most frequently used kanji (according to kanjidic)
0x65E5, 0x4E00, 0x4EBA, 0x56FD, 0x4F1A, 0x5E74, 0x5927, 0x5341, 0x4E8C, 0x672C,
// Semi-randomly chosen chars
0x98DF, '\u9762', 0x96FB, 38588
};

void draw()
{
background(#556677);

if (frameCount % 2 == 1)
{
float r = random(3);
if (r < 1)
{
// Hiragana
character = (char) (0x3041 + random(0x54));
}
else if (r < 2)
{
// Katakana
character = (char) (0x30A1 + random(0x57));
}
else
{
character = demoChars[int(random(demoChars.length))];
}
println(hex(character));
posX = int(5 + random(width - 140));
posY = int(130 + random(height - 150));
}

fill(128 + int(random(128)), 128 + int(random(128)), 128 + int(random(128)));
textFont(jFonts[int(random(jFonts.length))]);
text(character, posX, posY);

if (bDebug)
{
// Testing limits
fill(#FF0000);
text(character, 5, 130);
text(character, 5, 680);
text(character, 565, 130);
text(character, 565, 680);
}
}
Re: Random kanji generator
Reply #4 - Mar 1st, 2010, 4:40am
 
! Fantastic! Okay, I was completely unaware that char would take values like u9763 or 0x9763. This is the key. But please feel free to develop the code and/or post updates on this thread. I'm not jealous of the idea or see your coding stuff as a spoiling my fun or anything like that. I see the problem/enterprise/idea  an its results as an area of investigation that anyone can take part in/benefit from. Much thanks!
Re: Random kanji generator
Reply #5 - Mar 1st, 2010, 8:16am
 
Hmm. I'm still missing a link to make a random kanji generator. Have been trying to create numbers with the random function, but I still need a way to convert integers into chars. Well. Will keep digging around.
Re: Random kanji generator
Reply #6 - Mar 1st, 2010, 9:41am
 
Well, 0x98DF is an integer, actually.
You can do character = (char) (0x3041 + random(0x54)); for example.
I just show that in my updated code, above: I select randomly a kana or one of the 4 kanji I used before.
Re: Random kanji generator
Reply #7 - Mar 2nd, 2010, 5:44pm
 
! Damn! I love the net! Much thanks. I guess I still don't quite understand the data types though. Why is it that when I wrote something direct like

char letter = 36500;   it worked, but when I wrote

int number = 36500;
char letter = number;

then processing said "cannot convert from int to char?" Why aren't the two operations equivalent?
Re: Random kanji generator
Reply #8 - Mar 3rd, 2010, 12:13am
 
It is simple... once you know the answer!
Let's use a metaphor, incorrect like most metaphors, but illustrative.
If you fill a glass at a tap (or faucet), you can put only a little, half of the glass or up to the border.
If you take a larger glass, you can do the same, with of course more water if filled entirely.
Now, imagine to describe one operation in a generic way: "I pour water from glass 1 to glass 2".
If glass 1 is smaller or equal to glass 2, teacher will say OK.
If it is bigger, she will say "Warning! You can split water if glass 1 is too filled".

Compiler is like the teacher. It doesn't know in advance how much the glass (the int var) will be filled. So it prevents to pour (assign) the int var into the char var (much smaller).
char is two bytes and limited to 65536 values. int is four bytes and can go up to 2 147 483 647. So you can have a surprise doing that operation... You can force the compiler to do the action, though, telling you are sure the int won't hold too much, or that you don't care for spilled water: char letter = (char) number;
Re: Random kanji generator
Reply #9 - Mar 3rd, 2010, 6:55am
 
Note that I just updated the code above, trying more fonts, but discarding those not found on the system.
Also added some frequently used kanji.
Re: Random kanji generator
Reply #10 - Mar 17th, 2010, 5:41am
 
Hey there, been away a bit, had to attend to a few projects. Sorry for not thanking you for your excellent explanation which I just saw now. It all makes sense now. I'd been wondering what the unfamiliar parentheses in your code were. Hadn't run into the force operation before. Wink
Page Index Toggle Pages: 1