Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Finite state machine FST

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Today, I read an article about lucene's use of finite state machines, http://www.cnblogs.com/LBSer/p/4119841.html, which at first felt very similar to trie trees, but later found that they were different:

Trie tree is a tree structure, each child node has only one parent node, while FST is a mesh structure, each child node can have multiple parent nodes, so FST saves more space.

I also saw an article that also mentioned the application of finite state machines.

Https://www.cnblogs.com/dreamroute/p/8484457.html

The use of finite State Machine in luence

The reason why String inputs= {"abc", "abd", "acf", "acg"}; / / keyslong outputs= {1rec 3je 5je 7}; / / valuesFST fst=new FST (); for (int item0 / sensitiveWordList.add (I, I + length) {sensitiveWordList.add (I, I + length)) {sensitiveWordList.add (I, I + length); / / minus 1 is because for increases itself by I = I + length-1. }} return sensitiveWordList;} / * replace sensitive word characters * * @ param txt * @ param matchType * @ param replaceChar * @ return * / public String replaceSensitiveWord (String txt, int matchType, String replaceChar) {String resultTxt = txt; / / get all sensitive words Set set = getSensitiveWord (txt, matchType) Iterator iterator = set.iterator (); String word = null; String replaceString = null; while (iterator.hasNext ()) {word = iterator.next (); replaceString = getReplaceChars (replaceChar, word.length ()); resultTxt = resultTxt.replaceAll (word, replaceString);} return resultTxt } / * get the replacement string * * @ param replaceChar * @ param length * @ return * / private String getReplaceChars (String replaceChar, int length) {String resultReplace = replaceChar; for (int I = 1; I < length; iTunes +) {resultReplace + = replaceChar;} return resultReplace } / * check whether the text contains sensitive characters. The check rules are as follows:

* if it exists, the length of sensitive word characters is returned. There is no return 0 * * @ param txt * @ param beginIndex * @ param matchType * @ return * / public int CheckSensitiveWord (String txt, int beginIndex, int matchType) {/ / sensitive word ending identification bit: boolean flag = false for sensitive words with only 1 digit / / the default number of matching identifiers is 0 int matchFlag = 0; Map nowMap = this.sensitiveWordMap; for (int I = beginIndex; I < txt.length (); iTunes +) {char word = txt.charAt (I); / / get the specified key nowMap = (Map) nowMap.get (word) / / if it exists, determine whether it is the last if (nowMap! = null) {/ / find the corresponding key and match the identity + 1 matchFlag++ / / if it is the last matching rule, end the loop and return the number of matching identifiers if ("1" .equals (nowMap.get ("isEnd") {/ / the ending flag bit is true flag = true / / the minimum rule is returned directly, and the maximum rule needs to continue searching for if (1 = = matchType) {break;} / / does not exist, and directly returns else {break }} / / length must be greater than or equal to 1, which is the word if (matchFlag < 2 | |! flag) {matchFlag = 0;} return matchFlag;} private Set readSensitiveWordFile () {Set wordSet = new HashSet (); wordSet.add ("× ×"); wordSet.add ("× ×"); wordSet.add ("son of a bitch") WordSet.add ("son of a bitch"); return wordSet;} public static void main (String [] args) {All all=new All (); all.sensitiveWordMap=all.addSensitiveWordToHashMap (all.readSensitiveWordFile ()); String txt = "× × ×"; String hou = all.replaceSensitiveWord (txt, 1, "*"); System.out.println ("text before replacement is:" + txt) System.out.println ("the replaced text is:" + hou);}}

I feel that this is very similar to the trie tree I wrote before. DFA seems to be a tree, not a mesh.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report