|
| 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 | + * http://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.schabi.newpipe.settings.preferencesearch.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. |
| 27 | + * A higher score 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 | + * Note: This class was forked from |
| 37 | + * <a href="https://git.io/JyYJg"> |
| 38 | + * apache/commons-text (8cfdafc) FuzzyScore.java |
| 39 | + * </a> |
| 40 | + */ |
| 41 | +public class FuzzyScore { |
| 42 | + |
| 43 | + /** |
| 44 | + * Locale used to change the case of text. |
| 45 | + */ |
| 46 | + private final Locale locale; |
| 47 | + |
| 48 | + |
| 49 | + /** |
| 50 | + * This returns a {@link Locale}-specific {@link FuzzyScore}. |
| 51 | + * |
| 52 | + * @param locale The string matching logic is case insensitive. |
| 53 | + A {@link Locale} is necessary to normalize both Strings to lower case. |
| 54 | + * @throws IllegalArgumentException |
| 55 | + * This is thrown if the {@link Locale} parameter is {@code null}. |
| 56 | + */ |
| 57 | + public FuzzyScore(final Locale locale) { |
| 58 | + if (locale == null) { |
| 59 | + throw new IllegalArgumentException("Locale must not be null"); |
| 60 | + } |
| 61 | + this.locale = locale; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Find the Fuzzy Score which indicates the similarity score between two |
| 66 | + * Strings. |
| 67 | + * |
| 68 | + * <pre> |
| 69 | + * score.fuzzyScore(null, null) = IllegalArgumentException |
| 70 | + * score.fuzzyScore("not null", null) = IllegalArgumentException |
| 71 | + * score.fuzzyScore(null, "not null") = IllegalArgumentException |
| 72 | + * score.fuzzyScore("", "") = 0 |
| 73 | + * score.fuzzyScore("Workshop", "b") = 0 |
| 74 | + * score.fuzzyScore("Room", "o") = 1 |
| 75 | + * score.fuzzyScore("Workshop", "w") = 1 |
| 76 | + * score.fuzzyScore("Workshop", "ws") = 2 |
| 77 | + * score.fuzzyScore("Workshop", "wo") = 4 |
| 78 | + * score.fuzzyScore("Apache Software Foundation", "asf") = 3 |
| 79 | + * </pre> |
| 80 | + * |
| 81 | + * @param term a full term that should be matched against, must not be null |
| 82 | + * @param query the query that will be matched against a term, must not be |
| 83 | + * null |
| 84 | + * @return result score |
| 85 | + * @throws IllegalArgumentException if the term or query is {@code null} |
| 86 | + */ |
| 87 | + public Integer fuzzyScore(final CharSequence term, final CharSequence query) { |
| 88 | + if (term == null || query == null) { |
| 89 | + throw new IllegalArgumentException("CharSequences must not be null"); |
| 90 | + } |
| 91 | + |
| 92 | + // fuzzy logic is case insensitive. We normalize the Strings to lower |
| 93 | + // case right from the start. Turning characters to lower case |
| 94 | + // via Character.toLowerCase(char) is unfortunately insufficient |
| 95 | + // as it does not accept a locale. |
| 96 | + final String termLowerCase = term.toString().toLowerCase(locale); |
| 97 | + final String queryLowerCase = query.toString().toLowerCase(locale); |
| 98 | + |
| 99 | + // the resulting score |
| 100 | + int score = 0; |
| 101 | + |
| 102 | + // the position in the term which will be scanned next for potential |
| 103 | + // query character matches |
| 104 | + int termIndex = 0; |
| 105 | + |
| 106 | + // index of the previously matched character in the term |
| 107 | + int previousMatchingCharacterIndex = Integer.MIN_VALUE; |
| 108 | + |
| 109 | + for (int queryIndex = 0; queryIndex < queryLowerCase.length(); queryIndex++) { |
| 110 | + final char queryChar = queryLowerCase.charAt(queryIndex); |
| 111 | + |
| 112 | + boolean termCharacterMatchFound = false; |
| 113 | + for (; termIndex < termLowerCase.length() |
| 114 | + && !termCharacterMatchFound; termIndex++) { |
| 115 | + final char termChar = termLowerCase.charAt(termIndex); |
| 116 | + |
| 117 | + if (queryChar == termChar) { |
| 118 | + // simple character matches result in one point |
| 119 | + score++; |
| 120 | + |
| 121 | + // subsequent character matches further improve |
| 122 | + // the score. |
| 123 | + if (previousMatchingCharacterIndex + 1 == termIndex) { |
| 124 | + score += 2; |
| 125 | + } |
| 126 | + |
| 127 | + previousMatchingCharacterIndex = termIndex; |
| 128 | + |
| 129 | + // we can leave the nested loop. Every character in the |
| 130 | + // query can match at most one character in the term. |
| 131 | + termCharacterMatchFound = true; |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + return score; |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Gets the locale. |
| 141 | + * |
| 142 | + * @return The locale |
| 143 | + */ |
| 144 | + public Locale getLocale() { |
| 145 | + return locale; |
| 146 | + } |
| 147 | + |
| 148 | +} |
0 commit comments