OpenKeyWord  Build_ID: 457, Datum: 01.02.2020 07:45:48
Dont repeat yourself. - Do it once and only once!
Matcher.java
1 /*
2  ==============================================================================
3  Copyright © 2012 - 2019 IT-Beratung Hrabovszki
4  ==============================================================================
5 
6  This file is part of OpenKeyWord.
7 
8  OpenKeyWord is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  OpenKeyWord is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with OpenKeyWord. If not, see <http://www.gnu.org/licenses/>.
20 
21  Diese Datei ist Teil von OpenKeyWord.
22 
23  OpenKeyWord ist Freie Software: Sie können es unter den Bedingungen
24  der GNU General Public License, wie von der Free Software Foundation,
25  Version 3 der Lizenz oder (nach Ihrer Wahl) jeder späteren
26  veröffentlichten Version, weiterverbreiten und/oder modifizieren.
27 
28  OpenKeyWord wird in der Hoffnung, dass es nützlich sein wird, aber
29  OHNE JEDE GEWÄHRLEISTUNG, bereitgestellt; sogar ohne die implizite
30  Gewährleistung der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
31  Siehe die GNU General Public License für weitere Details.
32 
33  Sie sollten eine Kopie der GNU General Public License zusammen mit
34  OpenKeyWord erhalten haben. Wenn nicht, siehe <http://www.gnu.org/licenses/>.
35 */
36 
37 package okw.core;
38 
39 import java.util.ArrayList;
40 
41 public class Matcher {
42 
43  public static int LevenshteinDistance(String a, String b) {
44  a = a.toLowerCase();
45  b = b.toLowerCase();
46  // i == 0
47  int [] costs = new int [b.length() + 1];
48  for (int j = 0; j < costs.length; j++)
49  costs[j] = j;
50  for (int i = 1; i <= a.length(); i++) {
51  // j == 0; nw = lev(i - 1, j)
52  costs[0] = i;
53  int nw = i - 1;
54  for (int j = 1; j <= b.length(); j++) {
55  int cj = Math.min(1 + Math.min(costs[j], costs[j - 1]), a.charAt(i - 1) == b.charAt(j - 1) ? nw : nw + 1);
56  nw = costs[j];
57  costs[j] = cj;
58  }
59  }
60  return costs[b.length()];
61  }
62 
63  public static boolean LevenshteinMatch(String fpsActuell, String fpsExpected, int fpiDistance)
64  {
65  Boolean lvbReturn = false;
66 
67  int lviDistanceActuell = LevenshteinDistance( fpsActuell, fpsExpected);
68 
69 
70  if ( lviDistanceActuell <= fpiDistance )
71  {
72  lvbReturn = true;
73  }
74 
75  return lvbReturn;
76  }
77 
83  public static boolean LevenshteinMatch(ArrayList<String> fpALActuell, ArrayList<String> fpALExpected, int fpiDistance)
84  {
85  boolean lvb_Return = true;
86 
87  Integer ActualSize = (Integer)fpALActuell.size();
88  Integer ExpectedSize = (Integer)fpALExpected.size();
89 
90  if( ActualSize.equals( ExpectedSize ))
91  {
92 
93  for (int i = 0; i < fpALActuell.size(); i++)
94  {
95  if ( ! Matcher.LevenshteinMatch(fpALActuell.get(i), fpALExpected.get(i), fpiDistance ) )
96  {
97  lvb_Return = false;
98  break;
99  }
100  }
101  }
102  else
103  {
104  lvb_Return = false;
105  }
106 
107  return lvb_Return;
108  }
109 
110 
116  public static boolean WildcardMatch(String fpsActuell, String fpsExpected)
117  {
118  boolean lvb_Return = false;
119 
120  lvb_Return = fpsActuell.matches(fpsExpected.replace("?", ".").replace("*", ".*").replace("#", "\\d"));
121 
122  return lvb_Return;
123  }
124 
125 
131  public static boolean WildcardMatch(ArrayList<String> fpALActuell, ArrayList<String> fpALExpected)
132  {
133  boolean lvb_Return = true;
134 
135  Integer ActualSize = (Integer)fpALActuell.size();
136  Integer ExpectedSize = (Integer)fpALExpected.size();
137 
138  if( ActualSize.equals( ExpectedSize ))
139  {
140 
141  for (int i = 0; i < fpALActuell.size(); i++)
142  {
143  if ( ! Matcher.WildcardMatch(fpALActuell.get(i), fpALExpected.get(i)) )
144  {
145  lvb_Return = false;
146  break;
147  }
148  }
149  }
150  else
151  {
152  lvb_Return = false;
153  }
154 
155  return lvb_Return;
156  }
157 
158 
159  public static boolean RegexMatch(String fpsActuell, String fpsExpectedRegex)
160  {
161  boolean lvb_Return = false;
162 
163  lvb_Return = fpsActuell.matches(fpsExpectedRegex);
164 
165  return lvb_Return;
166  }
167 
173  public static boolean RegexMatch(ArrayList<String> fpALActuell, ArrayList<String> fpALExpected)
174  {
175  boolean lvb_Return = true;
176 
177  Integer ActualSize = (Integer)fpALActuell.size();
178  Integer ExpectedSize = (Integer)fpALExpected.size();
179 
180  if( ActualSize.equals( ExpectedSize ))
181  {
182 
183  for (int i = 0; i < fpALActuell.size(); i++)
184  {
185  if ( ! Matcher.RegexMatch(fpALActuell.get(i), fpALExpected.get(i)) )
186  {
187  lvb_Return = false;
188  break;
189  }
190  }
191  }
192  else
193  {
194  lvb_Return = false;
195  }
196 
197  return lvb_Return;
198  }
199 }
okw.core.Matcher.WildcardMatch
static boolean WildcardMatch(String fpsActuell, String fpsExpected)
Definition: Matcher.java:116
okw.core.Matcher.WildcardMatch
static boolean WildcardMatch(ArrayList< String > fpALActuell, ArrayList< String > fpALExpected)
Definition: Matcher.java:131
okw.core.Matcher.LevenshteinMatch
static boolean LevenshteinMatch(ArrayList< String > fpALActuell, ArrayList< String > fpALExpected, int fpiDistance)
Definition: Matcher.java:83
okw.core.Matcher
Definition: Matcher.java:41
okw.core.Matcher.RegexMatch
static boolean RegexMatch(ArrayList< String > fpALActuell, ArrayList< String > fpALExpected)
Definition: Matcher.java:173