??? 在現(xiàn)實生活中,我們需要一些系統(tǒng)提供輸入拼音首字母,返回與其對應(yīng)中文的功能,這樣可以提高人機交互性以及提高系統(tǒng)的友好性。
??? 結(jié)合之前所做的portlet技術(shù),還有AJAX,讓我們在web應(yīng)用這塊來說說這個不是太復雜的應(yīng)用吧:
??? 對于我們的漢字與拼音對應(yīng)詞庫生成需要如下資源:
??? 1、首先要一個該系統(tǒng)所屬的中文詞庫
??? 2、一份漢字與拼音的對照表
??? 接下來我們會用這個中文詞庫去匹配漢字與拼音對照表之中的數(shù)據(jù),找出與各個詞語對應(yīng)的拼音來,然后在這個文件中文詞語后面生成其對應(yīng)的漢語拼音聲母首字母序列。
??? 首先我們需要用漢字拼音對照表生成體統(tǒng)中文詞庫對應(yīng)的,拼音列表。之后我們需要將這個拼音系統(tǒng)詞庫列表存儲到內(nèi)存之中。考慮到效率等綜合因素,我們選取了TreeMap這個類,它以其優(yōu)秀的內(nèi)部結(jié)構(gòu)使得containsKey(), get(), put() 和 remove()等操作能夠保持其時間復雜度在對數(shù)級上,即logN。為了能夠保持拼音對應(yīng)漢字的能夠隨著字母的增減而對應(yīng)顯示,我們選用了TreeMap中的SubMap()方法,其返回值是一個SortedMap對象。這下面的代碼使我從之前的OOo應(yīng)用中取出的,大家改改就可以用了。
????????????? ?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?
最重要的在這里:
SortedMap showMap = (SortedMap)
chineseMedicalTermWithPropertyHash
?????????????????????????????????? .subMap(searchString,
tempString);
是從TreeMap實例中取出在length-1到length的所有關(guān)鍵字組成的一個SortedMap實例,它的特性是:
A map that further guarantees that it will be in ascending
key order, sorted according to the natural ordering of its keys (see the
Comparable interface), or by a comparator provided at sorted map
creation time. This order is reflected when iterating over the sorted map's
collection views (returned by the entrySet, keySet and values
methods). Several additional operations are provided to take advantage of the
ordering. (This interface is the map analogue of the SortedSet
interface.)
?????? 而對于AJAX部分來講更是簡單。我們知道AJAX與一般的web應(yīng)用區(qū)別就是在它使用了Javascript的一個對象XMLHttpResponse/XMLHttpRequest,從而達到了異步的傳輸效果,提高了人機交互性。在本例中,我們沒有使用什么AJAX的框架,而是直接使用了在html標簽中最基本的javascript函數(shù)的方式來實現(xiàn),從而達到了異步響應(yīng)的功能。其中解決了一個問題,現(xiàn)在說說。那就是如果單單要實現(xiàn)AJAX傳輸數(shù)據(jù),不能夠使用session來傳輸,這樣的效果總是會慢一拍,為什么會這樣呢?請大家想想,過段時間答復。:)
??? 至于portlet,只要滿足JSR168的就行了,難度不是太大。只要把doView()方法覆蓋了,就沒有大問題了的。大家可以試試。
本文依據(jù)《創(chuàng)作共用約定》之“署名-禁止派生-非商業(yè)用途”方式發(fā)布,即你可以免費拷貝、分發(fā)、呈現(xiàn)和表演當前作品,但是必須基于以下條款:
對于任何二次使用或分發(fā),你必須讓其他人明確當前作品的授權(quán)條款。
在得到作者的明確允許下,這里的某些條款可以放棄。