Next, here are the details of several matching algorithms:
1. Exact match
The exact match is simple enough to determine if the string is equal. Here to judge all pinyin and all numbers. If the pinyin has been met, the number is no longer judged. Anyway, it's a man.
Private scoreandhits completematch (String reg) {scoreandhits scoreandhits = new scoreandhits ( -1, 0f,new arraylist<pointpair> ());for (int i = 0; i < fullnamenumberwithoutspace.size (); i++) {String str = Fullnamenumberwithoutspace.get (i);if (Reg.equals (str)) {scoreAndHits.nameIndex = i; Scoreandhits.score = match_level_complete;scoreandhits.pairs.add (New pointpair (i, -1)); Scoreandhits.matchlevel = level_complete;return scoreandhits;}} for (Int i = 0; i < phones.size (); i++) {PhoneStruct Phone = phones.get (i);if (Reg.equals (phone.phonenumber)) {scoreandhits.nameindex = i;scoreandhits.score = match_level_complete;scoreandhits.pairs.add (New PointPair (i, -1)); Scoreandhits.matchtype = match_type_phone;scoreandhitS.matchlevel = level_complete;return scoreandhits;}} Walk here to show that there is no match return new scoreandhits ( -1, 0f, new arraylist<pointpair> ());}
2. (Can you think of a nice name 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0004.gif "alt=" J_0004.gif "/>"
Private scoreandhits foreacronymoverflowmatch (String reg) { // because it is possible to Polyphone, this method is used to compare the matching degree of different pinyin and take the largest one scoreandhits scoreandhits = new scoreandhits ( -1, 0f,new arraylist<pointpair> ());for (int i = 0; i < fullnamenumber.size (); i++) {ArrayList<String> names = Fullnamenumber.get (i); Scoreandhits tmpscore = foreacronymoverflowmatch (Names, reg);if (tmpscore.score > scoreandhits.score) {scoreandhits = tmpscore;scoreandhits.nameindex = i;}} Scoreandhits.matchlevel = level_fore_acronym_overflow;return scoreandhits;} In the case of the first letter, the second letter may have three cases// one, the neighbor position of the word in the first letter charat (x+1);// second, at the first letter of the second word// three, None of the above is consistent, mismatched, out of Private scoreandhits foreacronymoverflowmatch (Arraylist<string> names, String reg) &NBSP;{//&NBSP; used to derive a matching value for a phonetic alphabet. Scoreandhits scoreandhits = new scoreandhits ( -1, 0f,new arraylist<pointpair > ());if (Names.get (0). charAt (0) == reg.charat (0)) { //actually crosswords () method is the method to find the matching value, Loloverflowmatchvalue value = crosswords (names, reg, 0, 0, 0); Int cross = crosswords (names, reg, 0, 0, 0). crossed;if (cross > 0) {scoreandhits.score = match_level_fore_acronym_ overflow + cross* match_score_reward - (Names.size () - cross) * Match_ Miss_punish;scoreandhits.pairs = value.pairs;}} Return scoreandhits;} /** * returns a string of characters that can span the length of another string, matching the word as much as possible according to the above matching rules. To ensure that * can match the longest length, just make sure that the next character starts with a character that matches * the longest length, in other words, if you want 96758 to match the longest string, just make sure that 6758 matches the longest * a string, then 758, then 58 .... For example, if the name is pananning, type pan, then you should match three * initials, PAN, not the first surname of Pinyinpan. * this is a recursion. * * @param names * @param regString * matching strings * @param listIndex * matches the first word of the list to ListIndex * @param strindex * Match to the Strindex letter in the ListIndex word * @param regIndex * regchar matching position, such as matching to 96758 of 7, that is, regindex==2. * @return */ Private overflowmatchvalue crosswords (Arraylist<string> names,string regstring, int listindex, int strindex, int regindex) {overflowmatchvalue result = new overflowmatchvalue (0, false);overflowmatchvalue reser = new Overflowmatchvalue (0, false); Overflowmatchvalue impul = new overflowmatchvalue (0, false);if (regIndex < regstring.length () - 1) {char nextchar = regstring.charat (regIndex + 1);if (listindex < names.size () - 1&& nextchar == names.get (listindex + 1). CharAt (0)) {impul = crosswords (names, regstring, &NBSP;LISTINDEX&NBSP;+&NBSP;1,&NBSP;0,REGINDEX&NBSP;+&NBSP;1);} if (Strindex < names.get (listIndex). Length () - 1&& nextchar == names.get (ListIndex). CharAt (strindex + 1)) {reser = crosswords (names, regstring, listindex, strindex + 1,regindex + 1);}} else {result = new overflowmatchvalue ((strindex == 0) ? 1 : 0, true); Result.pairs.add (0, new pointpair (Listindex, strindex));} if (reser.matched | | impul.matched) {if (impul.crossed > reser.crossed) {result = impul ;} else {result = reser;} result.matched = true;result.crossed = ((strindex == 0) ? 1 : 0) + math.max (result.crossed, result.crossed); Result.pairs.add (0, new pointpair ( Listindex, strindex));} Return result;} Static class overflowmatchvalue {public int crossed = 0;public boolean matched = false;public ArrayList<PointPair> pairs = new Arraylist<pointpair> ();p ublic overflowmatchvalue (int c, boolean m) { This.crossed = c;this.matched = m;}}
3. After the initial letter overflow match . (Can you think of a nice name 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0004.gif "alt=" j_0004.gif "/> "
The front of the first letter overflow match is basically the same, except that the first letter of the match is no longer the first letter of the surname.
Private scoreandhits backacronymoverflowmatch (String reg) { //is almost scoreandhits scoreandhits = new scoreandhits ( -1, 0f,new ArrayList<PointPair> ());for (int i = 0; i < Fullnamenumber.size (); i++) {arraylist<string> names = fullnamenumber.get (i); Scoreandhits tmp = backacronymoverflowmatch (Names, reg);if (tmp.score > Scoreandhits.score) {scoreandhits = tmp;scoreandhits.nameindex = i;}} Scoreandhits.matchlevel = level_back_acronym_overflow;return scoreandhits;} Private scoreandhits backacronymoverflowmatch (Arraylist<string> names,string reg) {int score = 0;int punish = 0; Scoreandhits scoreandhits = new scoreandhits ( -1, 0f,new arraylist<pointpair > ()); may call multiple crosswords, depending on the length of the name. This is different from the previous for (Int i = 0; i < names.size (); i++) {String string = (String) names.get (i);if (String.charat (0) == reg.charat (0)) { Overflowmatchvalue value = crosswords (names, reg, i, 0, 0); Int cross = value.crossed;int lost = names.size () - cross;if (cross > score | | cross == score && punish > lost) {scoreAndHits.pairs = value.pairs;score = cross;punish = lost;}}} if (score > 0) {scoreandhits.score = match_level_back_acronym_overflow + score* match_score_reward - punish * match_miss_punish;return scoreandhits ;} else {return new scoreandhits ( -1, 0f, new arraylist<pointpair> ());}}
4. A rear-end headless match. (It's bad, that's what that means anyway)
Private scoreandhits backheadlessparagraphmatch (String reg) {int punish = 0 ; Scoreandhits scoreandhits = new scoreandhits ( -1, -1f,new arraylist<pointpair > ()); Scoreandhits.matchlevel = level_headless;scoreandhits.matchtype = match_type_phone ;// don't match the name, assuming no one will get so outrageous for (Int i = 0; i < phones.size (); i++) {phonestruct phone = phones.get (i); Int sco = phone.phonenumber.indexof (reg );if (sco >= 0) {int lost = phone.phonenumber.length () - Reg.length ();if (scoreandhits.score < sco | | sco == scoreandhits.score&& punish > lost) {scoreAndHits.score = sco;scoreAndHits.nameIndex = i;// Although it is possible to match the string very long, but because it is coherent, so long as the// Add one is enough. ScoreAndHits.pairs.add (New pointpair (I, sco));p UNISH&NBsp;= lost;}}} if (scoreandhits.score >= 0) {scoreAndHits.score = Match_Level_Headless - scoreandhits.score* match_score_reward - punish * match_miss_punish;} Return scoreandhits;}
It is possible to get a match between the input string and the contact, and the rest is called and displayed, but this is not the focus of this article 650) this.width=650; "Src=" http://img.baidu.com/hi/jx2/j_0010. GIF "alt=" J_0010.gif "/>
This article is from the "nashlegend" blog, make sure to keep this source http://nashlegend.blog.51cto.com/5635342/1566111
Android Speed dial matching algorithm (ii)