1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * https://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.text.similarity; 18 19 import java.util.Locale; 20 21 /** 22 * A matching algorithm that is similar to the searching algorithms implemented in editors such 23 * as Sublime Text, TextMate, Atom and others. 24 * 25 * <p> 26 * One point is given for every matched character. Subsequent matches yield two bonus points. A higher score 27 * indicates a higher similarity. 28 * </p> 29 * 30 * <p> 31 * This code has been adapted from Apache Commons Lang 3.3. 32 * </p> 33 * 34 * @since 1.0 35 */ 36 public class FuzzyScore { 37 38 /** 39 * Locale used to change the case of text. 40 */ 41 private final Locale locale; 42 43 /** 44 * This returns a {@link Locale}-specific {@link FuzzyScore}. 45 * 46 * @param locale The string matching logic is case insensitive. A {@link Locale} is necessary to normalize both Strings to lower case. 47 * @throws IllegalArgumentException This is thrown if the {@link Locale} parameter is {@code null}. 48 */ 49 public FuzzyScore(final Locale locale) { 50 if (locale == null) { 51 throw new IllegalArgumentException("Locale must not be null"); 52 } 53 this.locale = locale; 54 } 55 56 /** 57 * Find the Fuzzy Score which indicates the similarity score between two Strings. 58 * 59 * <pre> 60 * score.fuzzyScore(null, null) = IllegalArgumentException 61 * score.fuzzyScore("not null", null) = IllegalArgumentException 62 * score.fuzzyScore(null, "not null") = IllegalArgumentException 63 * score.fuzzyScore("", "") = 0 64 * score.fuzzyScore("Workshop", "b") = 0 65 * score.fuzzyScore("Room", "o") = 1 66 * score.fuzzyScore("Workshop", "w") = 1 67 * score.fuzzyScore("Workshop", "ws") = 2 68 * score.fuzzyScore("Workshop", "wo") = 4 69 * score.fuzzyScore("Apache Software Foundation", "asf") = 3 70 * </pre> 71 * 72 * @param term a full term that should be matched against, must not be null. 73 * @param query the query that will be matched against a term, must not be null. 74 * @return result score. 75 * @throws IllegalArgumentException if the term or query is {@code null}. 76 */ 77 public Integer fuzzyScore(final CharSequence term, final CharSequence query) { 78 if (term == null || query == null) { 79 throw new IllegalArgumentException("CharSequences must not be null"); 80 } 81 82 // fuzzy logic is case insensitive. We normalize the Strings to lower 83 // case right from the start. Turning characters to lower case 84 // via Character.toLowerCase(char) is unfortunately insufficient 85 // as it does not accept a locale. 86 final String termLowerCase = term.toString().toLowerCase(locale); 87 final String queryLowerCase = query.toString().toLowerCase(locale); 88 89 // the resulting score 90 int score = 0; 91 92 // the position in the term which will be scanned next for potential 93 // query character matches 94 int termIndex = 0; 95 96 // index of the previously matched character in the term 97 int previousMatchingCharacterIndex = Integer.MIN_VALUE; 98 99 for (int queryIndex = 0; queryIndex < queryLowerCase.length(); queryIndex++) { 100 final char queryChar = queryLowerCase.charAt(queryIndex); 101 102 boolean termCharacterMatchFound = false; 103 for (; termIndex < termLowerCase.length() 104 && !termCharacterMatchFound; termIndex++) { 105 final char termChar = termLowerCase.charAt(termIndex); 106 107 if (queryChar == termChar) { 108 // simple character matches result in one point 109 score++; 110 111 // subsequent character matches further improve 112 // the score. 113 if (previousMatchingCharacterIndex + 1 == termIndex) { 114 score += 2; 115 } 116 117 previousMatchingCharacterIndex = termIndex; 118 119 // we can leave the nested loop. Every character in the 120 // query can match at most one character in the term. 121 termCharacterMatchFound = true; 122 } 123 } 124 } 125 126 return score; 127 } 128 129 /** 130 * Gets the locale. 131 * 132 * @return The locale 133 */ 134 public Locale getLocale() { 135 return locale; 136 } 137 138 }