Recently, the conversion from Chinese characters to PinYin was used, so this article was found in many search result interference items. Because the original article has clear ideas and unique points of view, it is reproduced here. In essence, this article uses the lookup table method. The full text is basically a dictionary acquisition method.
Note: This article is transferred from http://blog.verycd.com/dash/cmd=showentry&eid=1677, the following is the original
Careful netizens may find that verycd's resource search supports pinyin. Whether you enter pinyin directly or homophone, our search Program You will be prompted for the correct keywords. For example: http://search.verycd.com/search? Q = Xiao yaxuan (Note: This link is outdated)
Previously we used a widely used online Chinese character to PinYin Function , After use, we find that the function's Algorithm Although simple, it can only obtain 3755 commonly used Chinese characters in gb2312 according to the pinyin arrangement, so its applicability is very limited. Most of the other Chinese characters are arranged in the sequence of radicals and strokes. Therefore, we can only create a Chinese character-pinyin table to achieve one-to-one correspondence.
At this time, the input method generator comes in handy and uses its inverse Conversion Function to generate a text file in Chinese pinyin. Then write a PHP applet to delete the phrases and polyphonic words in the text file. 1 <? PHP
2 // Note that the text file generated in 2000/XP is in unicode format and must be converted to gb2312
3 $ Py = File_get_contents ( " Winpy. txt " );
4 $ Arr_tmp = Explode ( " \ R \ n " , $ Py );
5 $ Arr_py = Array ();
6 Foreach ( $ Arr_tmp As $ V )
7 {
8 If ( Preg_match ( " /^ [ " . CHR ( 0x81 ) . " - " . CHR ( 0xfe ) . " ]. [A-Z]/ " , $ V ))
9 {
10 $ Word = Substr ( $ V , 0 , 2 );
11 If ( ! $ Arr_py [ $ Word ])
12 {
13 $ Arr_py [ $ Word ] = Substr ( $ V , 2 );
14 $ Output . = $ V . " \ N " ;
15 }
16 }
17 }
18 $ Handle = Fopen ( " Winpy2.txt " , ' W ' );
19 Fwrite ( $ Handle , $ Output );
20 ?>
Read winpy2.txt and then useString operation functionsYou can easily obtain the Chinese pinyin (SpecificCodeXdanger is still being written. I will not post it here)
Note: At the end of the processing, we ignored the multiphoneme factor and still need to be improved in the future.
OnlineAbout regular expressions to judge Chinese CharactersArticleMost of them use [CHR (0xa1)-CHR (0xff)] to judge Chinese characters. In fact, this can only identify thousands of Chinese Characters in gb2312. uncommon words and traditional Chinese characters are not included, [CHR (0x81)-CHR (0xfe)] is correct.
Note: The original attachment has been deleted. This article only provides the principle.