- 001
- 002
- 003
- 004
- 005
- 006
- 007
- 008
- 009
- 010
- 011
- 012
- 013
- 014
- 015
- 016
- 017
- 018
- 019
- 020
- 021
- 022
- 023
- 024
- 025
- 026
- 027
- 028
- 029
- 030
- 031
- 032
- 033
- 034
- 035
- 036
- 037
- 038
- 039
- 040
- 041
- 042
- 043
- 044
- 045
- 046
- 047
- 048
- 049
- 050
- 051
- 052
- 053
- 054
- 055
- 056
- 057
- 058
- 059
- 060
- 061
- 062
- 063
- 064
- 065
- 066
- 067
- 068
- 069
- 070
- 071
- 072
- 073
- 074
- 075
- 076
- 077
- 078
- 079
- 080
- 081
- 082
- 083
- 084
- 085
- 086
- 087
- 088
- 089
- 090
- 091
- 092
- 093
- 094
- 095
- 096
- 097
- 098
- 099
- 100
public class FSMSmilesMatcher {
private Callback mCallback;
private State mStartState;
public interface Callback {
public void onMatch(int matchStart, int matchEnd, int matchId);
}
//matching whole leading substring cause it's easier to build that way
private class State {
protected HashMap<String, State> substates; //substring which led to this state + next symbol
protected String whole; //whole match
protected int matchId;
//result = symbols to skip before starting next match
protected int process(String input, int matchStart, int matchEnd) {
if(matchStart >= input.length() || matchStart >= matchEnd)
return 0;
if(substates == null) {
if(whole == null || whole.length() == 0) {
return 0; //shouldn't happen
} else if(input.length() >= matchStart + whole.length() && input.substring(matchStart, matchStart + whole.length()).equals(whole)) {
mCallback.onMatch(matchStart, matchStart + whole.length(), matchId);
return whole.length()> 1 ? whole.length() - 1: 0;
}
} else if(matchEnd <= input.length()) {
State nextState = substates.get(input.substring(matchStart, matchEnd));
if(nextState != null)
return nextState.process(input, matchStart, matchEnd + 1);
}
return 0;
}
}
//checks only next symbol
//removes long tails (into single 'whole' match)
private class OptimizedState extends State{
private OptimizedState(State inState) {
if(inState.substates != null) {
this.substates = new HashMap<String, State>();
for(String key : inState.substates.keySet()) {
this.substates.put(key.substring(key.length()-1, key.length()).toLowerCase(), new OptimizedState(inState.substates.get(key)));
}
if(this.substates.size() == 1) {
State subState = this.substates.values().iterator().next();
if(subState.substates == null) {
this.substates = null;
this.whole = subState.whole;
this.matchId = subState.matchId;
}
}
} else {
this.whole = inState.whole;
this.matchId = inState.matchId;
}
}
@Override
protected int process(String input, int matchStart, int matchEnd) {
if(matchStart >= input.length() || matchStart >= matchEnd)
return 0;
if(substates == null) {
if(whole == null || whole.length() == 0) {
return 0; //shouldn't happen
} else if(input.length() >= matchStart + whole.length() && input.substring(matchStart, matchStart + whole.length()).equalsIgnoreCase(whole)) {
mCallback.onMatch(matchStart, matchStart + whole.length(), matchId);
return whole.length()> 1 ? whole.length() - 1: 0;
}
} else if(matchEnd <= input.length()) {
State nextState = substates.get(input.substring(matchEnd - 1, matchEnd).toLowerCase());
if(nextState != null)
return nextState.process(input, matchStart, matchEnd + 1);
}
return 0;
}
}
...
private HashMap<String, State> generateSubstates(List<ImageMatchElement> matches, String lead) {
List<String> variants = new ArrayList<String>();
HashMap<String, Integer> completeMatches = new HashMap<String, Integer>();
...
if(variants.size() == 0) {
return null;
} else {
HashMap<String, State> substates = new HashMap<String, State>();
for(String variant: variants) {
State state = new State();
if(completeMatches.containsKey(variant)) {
state.whole = variant;
state.matchId = completeMatches.get(variant);
} else {
state.substates = generateSubstates(matches, variant);
}
substates.put(variant, state);
}
...
Требовалось заменить коды смайликов в текстовом сообщении заменить на соответствующие картинки (типа ":* привет :-p ^_^" заменить на "[картинка] привет [картинка] [картинка]".