AJAX的拼音首字母查找对应中文的portlet

来源: 作者: 2007-02-10 出处:pcdog.com

ajax  java  javascript  xml  
下一页 1 2 
    在现实生活中,我们需要一些系统提供输入拼音首字母,返回与其对应中文的功能,这样可以提高人机交互性以及提高系统的友好性。
    结合之前所做的portlet技术,还有AJAX,让我们在web应用这块来说说这个不是太复杂的应用吧:
    对于我们的汉字与拼音对应词库生成需要如下资源:
    1、首先要一个该系统所属的中文词库
    2、一份汉字与拼音的对照表
    接下来我们会用这个中文词库去匹配汉字与拼音对照表之中的数据,找出与各个词语对应的拼音来,然后在这个文件中文词语后面生成其对应的汉语拼音声母首字母序列。
    首先我们需要用汉字拼音对照表生成体统中文词库对应的,拼音列表。之后我们需要将这个拼音系统词库列表存储到内存之中。考虑到效率等综合因素,我们选取了TreeMap这个类,它以其优秀的内部结构使得containsKey(), get(), put() 和 remove()等操作能够保持其时间复杂度在对数级上,即logN。为了能够保持拼音对应汉字的能够随着字母的增减而对应显示,我们选用了TreeMap中的SubMap()方法,其返回值是一个SortedMap对象。这下面的代码使我从之前的OOo应用中取出的,大家改改就可以用了。

             


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--> 1 // XActionListener
 2 
 3               public void textChanged(TextEvent rEvent) {
 4 
 5                      Object searchTextBox = xControlContainer
 6 
 7                                    .getControl(searchText);
 8 
 9  
10 
11                      XTextComponent yText = (XTextComponent) UnoRuntime.queryInterface(
12 
13                                    XTextComponent.class, searchTextBox);
14 
15                      searchString = yText.getText();
16 
17                      searchString = searchString.toLowerCase();
18 
19                      logger.debug("searchString is " + searchString);
20 
21                      Object resultComboBoxModel = xControlContainer
22 
23                                    .getControl(resultComboBox);
24 
25  
26 
27                      XComboBox xComboBox = (XComboBox) UnoRuntime.queryInterface(
28 
29                                    XComboBox.class, resultComboBoxModel);
30 
31  
32 
33                      Object label = xControlContainer.getControl("Label1");
34 
35                      XFixedText xLabel = (XFixedText) UnoRuntime.queryInterface(
36 
37                                    XFixedText.class, label);
38 
39                      char shiftChar = searchString.charAt(searchString.length() - 1);
40 
41                      shiftChar++;
42 
43                      String tempString = searchString.substring(0,
44 
45                                    searchString.length() - 1);
46 
47                      tempString = tempString + shiftChar;
48 
49                      if (searchString.length() == 1)
50 
51                             tempString = "" + shiftChar;
52 
53                      logger.debug("tempString is " + tempString);
54 
55                      SortedMap showMap = (SortedMap) chineseMedicalTermWithPropertyHash
56 
57                                    .subMap(searchString, tempString);
58 
59  
60 
61                      // xLabel.setText(chineseMedicalTermWithPropertyHash.size()+"
62 
63                      // check");
64 
65  
66 
67                      xComboBox.removeItems((short) 0, (short) 127);
68 
69                      short j = 0;
70 
71                      Iterator it = showMap.entrySet().iterator();
72 
73                      while (it.hasNext()) {
74 
75                             Map.Entry me = (Map.Entry) it.next();
76 
77                             Object ov = me.getValue();
78 
79                             xComboBox.addItem(ov.toString(), j);
80 
81                             j++;
82 
83                      }
84 
85               }
86 

更多内容请看PCdog.com--Ajax技术专题
下一页 1 2 
上一篇:确保无线 J2ME 的安全
下一篇:Apusic应用服务器和CORBA互操作