private void AddSimpleNext(string actual, string next, List <char> interval) { AbstractRule <char> roleChar = new SimpleListIncludeRule <char>(1, interval); AbstractRule <char> roleEmpty = new SimpleIncludeRule <char>(SYMBOLEMPTY, ValueDefaultTransitions); AbstractRule <char> roleExclusion = new SimpleListExclusionRule <char>(interval, alphabet.ToList(), ValueDefaultTransitions); Automa.AddTransition(actual, next, roleChar); Automa.AddTransition(actual, next, roleEmpty); Automa.AddTransition(actual, actual, roleExclusion); //Automa.AddTransition(actual, actual, roleEmpty); Automa.AddTransition(actual, next, roleExclusion); }
public virtual Automa <char> RegexToAutoma() { Automa = new Automa <char>(GetAlphabet().ToList(), SYMBOLEMPTY); var stateInitial = Automa.AddState(NextNameState(), 1, 0); if (string.IsNullOrEmpty(RegexFuzzy)) { return(Automa); } string temp = string.Empty; bool escape = false; for (int i = 0; i < RegexFuzzy.Length; i++) { if (RegexFuzzy[i] == '\\') { //NOTHING } else if (escape) { switch (RegexFuzzy[i]) { case 't': temp += '\t'; break; case 'n': temp += '\n'; break; case 'r': temp += '\r'; break; default: temp += RegexFuzzy[i]; break; } } else { switch (RegexFuzzy[i]) { case '?': temp += "{0,1}"; break; case '+': temp += "{1,}"; break; case '*': temp += "{0,}"; break; default: temp += RegexFuzzy[i]; break; } } if (RegexFuzzy[i] == '\\') { escape = true; } else { escape = false; } } RegexFuzzy = temp;//;RegexFuzzy.Replace("?", "{0,1}").Replace("+", "{1,}").Replace("*", "{0,}"); List <State <char> > lastStates = new List <State <char> >(); lastStates.Add(stateInitial); for (int i = 0; i < RegexFuzzy.Length; i++) { bool final = (i == RegexFuzzy.Length - 1); TypeCharEnum typeActual = GetTypeChar(RegexFuzzy[i]); TypeCharEnum typeNext = TypeCharEnum.Final; if (!final) { typeNext = GetTypeChar(RegexFuzzy[i + 1]); } if (typeActual == TypeCharEnum.Interval || typeActual == TypeCharEnum.Symbol || (typeActual == TypeCharEnum.Loop && typeNext == TypeCharEnum.Final)) { var nextState = Automa.AddState(NextNameState(), 0, Convert.ToInt16(final)); List <char> interval = null; if (ValidateInterval(i)) { interval = AddInterval(ref i, nextState, lastStates); typeNext = TypeCharEnum.Final; final = (i == RegexFuzzy.Length - 1); if (!final) { typeNext = GetTypeChar(RegexFuzzy[i + 1]); } } else //if () { foreach (var item in lastStates) { AddSimpleNext(item.Name, nextState.Name, RegexFuzzy[i]); } } if (typeNext == TypeCharEnum.Loop) { AddLoop(ref i, lastStates, nextState, final, interval); final = (i == RegexFuzzy.Length - 1); } else { lastStates.Clear(); lastStates.Add(nextState); } if (final) { List <char> inclusion = alphabet.ToList(); inclusion.Add(SYMBOLEMPTY); AbstractRule <char> roleAlphabet = new SimpleListIncludeRule <char>(ValueDefaultTransitions, inclusion); foreach (var item in lastStates) { item.PertinenceFinal = 1; Automa.FinalStates.Add(item); Automa.AddTransition(item.Name, item.Name, roleAlphabet); } } } } return(Automa); }
public virtual double Match(string source) { TableAutomaProcessing = new List <List <double> >(); int iSource = 0; char cActual = SYMBOLEMPTY; TableAutomaProcessing.Add(new List <double>()); for (int i = 0; i < Automa.States.Count; i++) { TableAutomaProcessing[0].Add(Automa.States[i].PertinenceInitial); } int steps = (source.Length * 2) + 1; for (int step = 1; step <= steps; step++) { if (iSource == source.Length) { iSource++; cActual = SYMBOLEMPTY; } else { if (step % 2 == 1) { cActual = SYMBOLEMPTY; } else { cActual = source[iSource++]; } } TableAutomaProcessing.Add(new List <double>()); for (int i = 0; i < Automa.States.Count; i++) { int stepOld = step - 1; double pertinence = double.MinValue; int indexMinor = 0; for (int j = 0; j < TableAutomaProcessing[stepOld].Count; j++) { double maxState = TableAutomaProcessing[stepOld][j]; double pertinenceNew = 0; if (maxState > 0) { pertinenceNew = Automa.SearchPertinence(Automa.States[j], Automa.States[i], cActual, Norm, Conorm); pertinenceNew = Norm.Calculate(pertinenceNew, maxState); } if (cActual == SYMBOLEMPTY) { pertinenceNew = Conorm.Calculate(pertinenceNew, TableAutomaProcessing[stepOld][i]); } if (pertinence < pertinenceNew) { pertinence = pertinenceNew; indexMinor = j; } } TableAutomaProcessing[step].Add(pertinence); } } double maxFinal = 0; for (int i = 0; i < Automa.States.Count; i++) { if (Automa.States[i].PertinenceFinal > 0) { maxFinal = Conorm.Calculate(Norm.Calculate(TableAutomaProcessing[steps][i], Automa.States[i].PertinenceFinal), maxFinal); } } return(maxFinal); }
private void AddLoop(ref int i, List <State <char> > lastStates, State <char> state, bool final, List <char> interval) { int ini = 0, fim = 0; char current = RegexFuzzy[i]; i = GetLoop(ref ini, ref fim, i); if (ini != 0) { lastStates.Clear(); } lastStates.Add(state); int j; State <char> ant = lastStates[0]; State <char> prox = null; for (j = 0; j < ini - 1; j++) { prox = Automa.AddState(NextNameState(), 0, Convert.ToInt16(final && ini == j - 1)); if (interval == null) { AddSimpleNext(ant.Name, prox.Name, current); } else { AddSimpleNext(ant.Name, prox.Name, interval); } ant = prox; } if (ini != 0) { lastStates.Clear(); lastStates.Add(ant); } if (fim == 0) { var last = lastStates.Last(); if (interval == null) { AddSimpleNext(last.Name, last.Name, current); } else { AddSimpleNext(last.Name, last.Name, interval); } } else { for (j = ini; j < fim; j++) { var nextState = Automa.AddState(NextNameState(), 0, Convert.ToInt16(final)); foreach (var item in lastStates) { if (interval == null) { AddSimpleNext(item.Name, nextState.Name, current); } else { AddSimpleNext(item.Name, nextState.Name, interval); } } lastStates.Add(nextState); } } }