Using the code lists all the idioms used in Jin Yong's novels

Day last year, Jin Yong died, Jerry was on the way to work at the Chengdu Metro Line learned the news, immediately wrote an article at home to mourn: Jin Yong's martial arts world and the SAP arena .

Fleeting time of year, everyone is busy in their lives, many people have forgotten golden old's death, but this Jerry Jin Yong diehard powder, for the first anniversary of his death still remember very clearly.

Jerry because a lot of things on hand, do not have time to write articles in this special day to commemorate, and to send a short code for it.

Requirements: List all idioms any one of Jin Yong's novels appear.

Realization: Jerry deployed on Github is a web application at the following link:

http://jerrywang-sap.cn/FioriODataTestTool2014/WebContent/050_Keyword.html

First, click on the hyperlink "idiom Complete Works":

clipboard1,1

After clicking, the web application is stored in a text file stored locally in all 19,830 idioms, loaded into memory in the form of a tree, and displayed on the web page:

clipboard2,2

Then copy the contents of one of his novels, paste it into the pages of "content" area, click the button "test":

clipboard3,3

Can be seen in just 246 milliseconds, this will all idioms over one million words of the "Kung Fu" where there, highlighted in red to highlight the way out.

clipboard4,4

This feature is implemented ye? Jerry Chrome open Web page, F12 Developer Tools open, you can see the JavaScript source code, of course, from my Github get on.

Jerry simply, under implementation principle. Web application there is a text file, which maintains all the Chinese language idiom, separated by semicolons.

clipboard5,5

Runtime, these contents will be loaded into memory, constructed as a tree, as shown below:

clipboard6,6

Wherein the leaf nodes to distinguish between attribute to true end.

The core logic located phrase search search function, let us use the "Swordsman" in a resounding slogan "Sun and Moon God of Chiaki Wanzai, monopolized" to step through, to understand its implementation logic.

clipboard7,7

While the outer loop into the 165 line, 173 line and then into the inner for loop, to detect whether there is the beginning of the test idiom first character string "day." Because of the idiom it is four characters, so the need to individually probe used for the inner loop, if the element is encountered tblCur.end true, indicating a phrase found in the test string.

The figure is tblCur contents for the inner loop of the first performance:

clipboard8,8

Performing a second inner loop, a tree from the point at this time tblCur all phrase "la" beginning consisting of:

clipboard9,9

The implementation of the third iteration of the inner loop, because the tree in the "Day - Month" this branch of the following is not "God" this node, so the end of the current inner loop, while loop to return to the outer layer by break, input string the second character "month" of a new round of temptation, and so on.

clipboard10,10

Finally, from the "thousands" of this character, along a path through the tree in memory of "Autumn - Wan", finally came to end a leaf node attribute to true "set", note the "thousands" offset input string amount, a memory array arrMatch to go.

clipboard11,11
clipboard12,12

After the trial is completed all of the character string to be inputted, according to the offset stored in arrMatch, highlighting the corresponding character string, complete retrieval.

clipboard13,13
The tree data structure has a perfect performance in the implementation of this requirement in.

Although Jin Yong left us, but he described those characters and the story takes place, will last forever in this world.

Read more

For more Jerry's original article, please pay attention to the public number "Wang Zixi":
No public Screenshot

Guess you like

Origin www.cnblogs.com/sap-jerry/p/11792220.html