001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.text.similarity; 018 019import java.util.Locale; 020 021/** 022 * A matching algorithm that is similar to the searching algorithms implemented in editors such 023 * as Sublime Text, TextMate, Atom and others. 024 * 025 * <p> 026 * One point is given for every matched character. Subsequent matches yield two bonus points. A higher score 027 * indicates a higher similarity. 028 * </p> 029 * 030 * <p> 031 * This code has been adapted from Apache Commons Lang 3.3. 032 * </p> 033 * 034 * @since 1.0 035 */ 036public class FuzzyScore { 037 038 /** 039 * Locale used to change the case of text. 040 */ 041 private final Locale locale; 042 043 /** 044 * This returns a {@link Locale}-specific {@link FuzzyScore}. 045 * 046 * @param locale The string matching logic is case insensitive. 047 A {@link Locale} is necessary to normalize both Strings to lower case. 048 * @throws IllegalArgumentException 049 * This is thrown if the {@link Locale} parameter is {@code null}. 050 */ 051 public FuzzyScore(final Locale locale) { 052 if (locale == null) { 053 throw new IllegalArgumentException("Locale must not be null"); 054 } 055 this.locale = locale; 056 } 057 058 /** 059 * Find the Fuzzy Score which indicates the similarity score between two 060 * Strings. 061 * 062 * <pre> 063 * score.fuzzyScore(null, null) = IllegalArgumentException 064 * score.fuzzyScore("not null", null) = IllegalArgumentException 065 * score.fuzzyScore(null, "not null") = IllegalArgumentException 066 * score.fuzzyScore("", "") = 0 067 * score.fuzzyScore("Workshop", "b") = 0 068 * score.fuzzyScore("Room", "o") = 1 069 * score.fuzzyScore("Workshop", "w") = 1 070 * score.fuzzyScore("Workshop", "ws") = 2 071 * score.fuzzyScore("Workshop", "wo") = 4 072 * score.fuzzyScore("Apache Software Foundation", "asf") = 3 073 * </pre> 074 * 075 * @param term a full term that should be matched against, must not be null 076 * @param query the query that will be matched against a term, must not be 077 * null 078 * @return result score 079 * @throws IllegalArgumentException if the term or query is {@code null} 080 */ 081 public Integer fuzzyScore(final CharSequence term, final CharSequence query) { 082 if (term == null || query == null) { 083 throw new IllegalArgumentException("CharSequences must not be null"); 084 } 085 086 // fuzzy logic is case insensitive. We normalize the Strings to lower 087 // case right from the start. Turning characters to lower case 088 // via Character.toLowerCase(char) is unfortunately insufficient 089 // as it does not accept a locale. 090 final String termLowerCase = term.toString().toLowerCase(locale); 091 final String queryLowerCase = query.toString().toLowerCase(locale); 092 093 // the resulting score 094 int score = 0; 095 096 // the position in the term which will be scanned next for potential 097 // query character matches 098 int termIndex = 0; 099 100 // index of the previously matched character in the term 101 int previousMatchingCharacterIndex = Integer.MIN_VALUE; 102 103 for (int queryIndex = 0; queryIndex < queryLowerCase.length(); queryIndex++) { 104 final char queryChar = queryLowerCase.charAt(queryIndex); 105 106 boolean termCharacterMatchFound = false; 107 for (; termIndex < termLowerCase.length() 108 && !termCharacterMatchFound; termIndex++) { 109 final char termChar = termLowerCase.charAt(termIndex); 110 111 if (queryChar == termChar) { 112 // simple character matches result in one point 113 score++; 114 115 // subsequent character matches further improve 116 // the score. 117 if (previousMatchingCharacterIndex + 1 == termIndex) { 118 score += 2; 119 } 120 121 previousMatchingCharacterIndex = termIndex; 122 123 // we can leave the nested loop. Every character in the 124 // query can match at most one character in the term. 125 termCharacterMatchFound = true; 126 } 127 } 128 } 129 130 return score; 131 } 132 133 /** 134 * Gets the locale. 135 * 136 * @return The locale 137 */ 138 public Locale getLocale() { 139 return locale; 140 } 141 142}