@Test public void testDetermineFieldMatches() { FieldMatchDeterminer sut = new FieldMatchDeterminer(); List<String> fieldsToAnalyze = new ArrayList<String>(); fieldsToAnalyze.add("typeOfAuto"); fieldsToAnalyze.add("name"); sut.setFieldsToAnalyze(fieldsToAnalyze); sut.setSearchAnalyzer(new TextStemmerAnalyzer()); FieldMatch matches = sut.determineFieldMatches(getExplanation(), "ford cars"); assertTrue(matches.isMatch("typeOfAuto")); assertTrue(matches.isMatch("name")); assertFalse(matches.isMatch("foobar")); Object[] matchedKeywords = matches.getMatchingKeywords("typeOfAuto").toArray(); assertEquals(1, matchedKeywords.length); assertEquals("cars", matchedKeywords[0]); matchedKeywords = matches.getMatchingKeywords("name").toArray(); assertEquals(2, matchedKeywords.length); assertEquals("cars", matchedKeywords[0]); assertEquals("ford", matchedKeywords[1]); }
public FieldMatch determineFieldMatches(final String inExplanationText, final String searchText) { String explanationText = inExplanationText; FieldMatch matchedKeywords = new FieldMatch(); if (fieldsToAnalyze.size() == 0) { return matchedKeywords; } log.debug("Explanation:" + explanationText); Pattern boostPattern = Pattern.compile("\\^[0-9]+.[0-9]+"); Matcher boostPatternMatcher = boostPattern.matcher(explanationText); explanationText = boostPatternMatcher.replaceAll(""); Map<String, String> tokenizedKeywords = tokenizeKeywords(searchText); for (String fieldName : fieldsToAnalyze) { Pattern weightPattern = Pattern.compile("\\sweight\\(" + fieldName + ":(\\w+)\\s", java.util.regex.Pattern.CASE_INSENSITIVE | java.util.regex.Pattern.MULTILINE); Matcher m = weightPattern.matcher(explanationText); boolean result = m.find(); while (result) { matchedKeywords.addMatch(fieldName, tokenizedKeywords.get(m.group(1))); result = m.find(); } } return matchedKeywords; }
FieldMatchDeterminer { public FieldMatch determineFieldMatches(final String inExplanationText, final String searchText) { String explanationText = inExplanationText; FieldMatch matchedKeywords = new FieldMatch(); if (fieldsToAnalyze.size() == 0) { return matchedKeywords; } log.debug("Explanation:" + explanationText); Pattern boostPattern = Pattern.compile("\\^[0-9]+.[0-9]+"); Matcher boostPatternMatcher = boostPattern.matcher(explanationText); explanationText = boostPatternMatcher.replaceAll(""); Map<String, String> tokenizedKeywords = tokenizeKeywords(searchText); for (String fieldName : fieldsToAnalyze) { Pattern weightPattern = Pattern.compile("\\sweight\\(" + fieldName + ":(\\w+)\\s", java.util.regex.Pattern.CASE_INSENSITIVE | java.util.regex.Pattern.MULTILINE); Matcher m = weightPattern.matcher(explanationText); boolean result = m.find(); while (result) { matchedKeywords.addMatch(fieldName, tokenizedKeywords.get(m.group(1))); result = m.find(); } } return matchedKeywords; } }
FieldMatchDeterminer { public FieldMatch determineFieldMatches(final String inExplanationText, final String searchText) { String explanationText = inExplanationText; FieldMatch matchedKeywords = new FieldMatch(); if (fieldsToAnalyze.size() == 0) { return matchedKeywords; } log.debug("Explanation:" + explanationText); Pattern boostPattern = Pattern.compile("\\^[0-9]+.[0-9]+"); Matcher boostPatternMatcher = boostPattern.matcher(explanationText); explanationText = boostPatternMatcher.replaceAll(""); Map<String, String> tokenizedKeywords = tokenizeKeywords(searchText); for (String fieldName : fieldsToAnalyze) { Pattern weightPattern = Pattern.compile("\\sweight\\(" + fieldName + ":(\\w+)\\s", java.util.regex.Pattern.CASE_INSENSITIVE | java.util.regex.Pattern.MULTILINE); Matcher m = weightPattern.matcher(explanationText); boolean result = m.find(); while (result) { matchedKeywords.addMatch(fieldName, tokenizedKeywords.get(m.group(1))); result = m.find(); } } return matchedKeywords; }  }
FieldMatchDeterminer { public FieldMatch determineFieldMatches(final String inExplanationText, final String searchText) { String explanationText = inExplanationText; FieldMatch matchedKeywords = new FieldMatch(); if (fieldsToAnalyze.size() == 0) { return matchedKeywords; } log.debug("Explanation:" + explanationText); Pattern boostPattern = Pattern.compile("\\^[0-9]+.[0-9]+"); Matcher boostPatternMatcher = boostPattern.matcher(explanationText); explanationText = boostPatternMatcher.replaceAll(""); Map<String, String> tokenizedKeywords = tokenizeKeywords(searchText); for (String fieldName : fieldsToAnalyze) { Pattern weightPattern = Pattern.compile("\\sweight\\(" + fieldName + ":(\\w+)\\s", java.util.regex.Pattern.CASE_INSENSITIVE | java.util.regex.Pattern.MULTILINE); Matcher m = weightPattern.matcher(explanationText); boolean result = m.find(); while (result) { matchedKeywords.addMatch(fieldName, tokenizedKeywords.get(m.group(1))); result = m.find(); } } return matchedKeywords; }  void setFieldsToAnalyze(final List<String> inFieldsToAnalyze); void setSearchAnalyzer(final Analyzer inSearchAnalyzer); FieldMatch determineFieldMatches(final String inExplanationText, final String searchText); }
FieldMatchDeterminer { public FieldMatch determineFieldMatches(final String inExplanationText, final String searchText) { String explanationText = inExplanationText; FieldMatch matchedKeywords = new FieldMatch(); if (fieldsToAnalyze.size() == 0) { return matchedKeywords; } log.debug("Explanation:" + explanationText); Pattern boostPattern = Pattern.compile("\\^[0-9]+.[0-9]+"); Matcher boostPatternMatcher = boostPattern.matcher(explanationText); explanationText = boostPatternMatcher.replaceAll(""); Map<String, String> tokenizedKeywords = tokenizeKeywords(searchText); for (String fieldName : fieldsToAnalyze) { Pattern weightPattern = Pattern.compile("\\sweight\\(" + fieldName + ":(\\w+)\\s", java.util.regex.Pattern.CASE_INSENSITIVE | java.util.regex.Pattern.MULTILINE); Matcher m = weightPattern.matcher(explanationText); boolean result = m.find(); while (result) { matchedKeywords.addMatch(fieldName, tokenizedKeywords.get(m.group(1))); result = m.find(); } } return matchedKeywords; }  void setFieldsToAnalyze(final List<String> inFieldsToAnalyze); void setSearchAnalyzer(final Analyzer inSearchAnalyzer); FieldMatch determineFieldMatches(final String inExplanationText, final String searchText);  }