Search in sources :

Example 6 with Match

use of org.eclipse.sw360.cvesearch.datasource.matcher.Match in project sw360portal by sw360.

the class SearchLevels method guessForRelease.

private List<NeedleWithMeta> guessForRelease(CveSearchGuesser cveSearchGuesser, Release release, boolean useVersionInformation) throws IOException {
    if (useVersionInformation && !release.isSetVersion()) {
        return Collections.emptyList();
    }
    List<Match> vendorProductList;
    String productHaystack = release.getName();
    if (release.isSetVendor() && (release.getVendor().isSetShortname() || release.getVendor().isSetFullname())) {
        String vendorHaystack = nullToEmptyString(release.getVendor().getShortname()) + " " + nullToEmptyString(release.getVendor().getFullname());
        vendorProductList = cveSearchGuesser.guessVendorAndProducts(vendorHaystack, productHaystack);
    } else {
        vendorProductList = cveSearchGuesser.guessVendorAndProducts(productHaystack);
    }
    String cpeNeedlePostfix = ":" + (useVersionInformation ? release.getVersion() : "") + CPE_WILDCARD;
    Function<String, String> cpeBuilder = cpeNeedle -> CPE_NEEDLE_PREFIX + cpeNeedle + cpeNeedlePostfix;
    return vendorProductList.stream().map(match -> new NeedleWithMeta(cpeBuilder.apply(match.getNeedle()), "heuristic (dist. " + (useVersionInformation ? "0" : "1") + match.getDistance() + ")")).collect(Collectors.toList());
}
Also used : Properties(java.util.Properties) Release(org.eclipse.sw360.datahandler.thrift.components.Release) Predicate(java.util.function.Predicate) Match(org.eclipse.sw360.cvesearch.datasource.matcher.Match) CveSearchApi(org.eclipse.sw360.cvesearch.datasource.CveSearchApi) IOException(java.io.IOException) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) Logger(org.apache.log4j.Logger) CommonUtils(org.eclipse.sw360.datahandler.common.CommonUtils) List(java.util.List) Stream(java.util.stream.Stream) CommonUtils.nullToEmptyString(org.eclipse.sw360.datahandler.common.CommonUtils.nullToEmptyString) CveSearchGuesser(org.eclipse.sw360.cvesearch.datasource.CveSearchGuesser) Collections(java.util.Collections) CommonUtils.nullToEmptyString(org.eclipse.sw360.datahandler.common.CommonUtils.nullToEmptyString) Match(org.eclipse.sw360.cvesearch.datasource.matcher.Match)

Example 7 with Match

use of org.eclipse.sw360.cvesearch.datasource.matcher.Match in project sw360portal by sw360.

the class CveSearchGuesser method getBest.

public List<Match> getBest(List<Match> matches, int threshold) {
    if (matches.size() == 0) {
        return Collections.EMPTY_LIST;
    }
    List<Match> bestMatches = new ArrayList<>();
    int minDistance = matches.get(0).getDistance();
    Iterator<Match> matchesIterator = matches.iterator();
    Match current;
    do {
        current = matchesIterator.next();
        if (current.getDistance() > minDistance + threshold || current.getDistance() >= cutoff) {
            break;
        }
        bestMatches.add(current);
    } while (matchesIterator.hasNext());
    return bestMatches;
}
Also used : Match(org.eclipse.sw360.cvesearch.datasource.matcher.Match)

Example 8 with Match

use of org.eclipse.sw360.cvesearch.datasource.matcher.Match in project sw360portal by sw360.

the class ModifiedLevenshteinDistanceTest method getDistanceTest.

@Test
public void getDistanceTest() {
    String needle = "needle";
    String haystack = "haystack";
    Match match = levenshteinMatch(needle, haystack);
    assertThat(match.getNeedle(), is(needle));
    assertThat(match.getDistance(), is(greaterThan(0)));
}
Also used : ModifiedLevenshteinDistance.levenshteinMatch(org.eclipse.sw360.cvesearch.datasource.matcher.ModifiedLevenshteinDistance.levenshteinMatch) Test(org.junit.Test)

Example 9 with Match

use of org.eclipse.sw360.cvesearch.datasource.matcher.Match in project sw360portal by sw360.

the class ModifiedLevenshteinDistanceTest method getDistanceTestUpToPostfixMatch.

@Test
public void getDistanceTestUpToPostfixMatch() {
    String needle = "needle";
    String haystack = "needle postfix";
    Match match = levenshteinMatch(needle, haystack);
    assertThat(match.getDistance(), is(0));
}
Also used : ModifiedLevenshteinDistance.levenshteinMatch(org.eclipse.sw360.cvesearch.datasource.matcher.ModifiedLevenshteinDistance.levenshteinMatch) Test(org.junit.Test)

Example 10 with Match

use of org.eclipse.sw360.cvesearch.datasource.matcher.Match in project sw360portal by sw360.

the class ModifiedLevenshteinDistanceTest method getDistanceTestPartialSubstringMatch.

@Test
public void getDistanceTestPartialSubstringMatch() {
    String needle = "needle";
    String noise = "bla";
    String haystack = "prefix needle" + noise + " postfix";
    Match match = levenshteinMatch(needle, haystack);
    assertThat(match.getDistance(), is(noise.length()));
}
Also used : ModifiedLevenshteinDistance.levenshteinMatch(org.eclipse.sw360.cvesearch.datasource.matcher.ModifiedLevenshteinDistance.levenshteinMatch) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)8 ModifiedLevenshteinDistance.levenshteinMatch (org.eclipse.sw360.cvesearch.datasource.matcher.ModifiedLevenshteinDistance.levenshteinMatch)7 Match (org.eclipse.sw360.cvesearch.datasource.matcher.Match)3 IOException (java.io.IOException)2 Collectors (java.util.stream.Collectors)2 Logger (org.apache.log4j.Logger)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 JSONObject (com.liferay.portal.kernel.json.JSONObject)1 PrintWriter (java.io.PrintWriter)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 Collections.singletonList (java.util.Collections.singletonList)1 List (java.util.List)1 Properties (java.util.Properties)1 Function (java.util.function.Function)1 Predicate (java.util.function.Predicate)1 Stream (java.util.stream.Stream)1 TException (org.apache.thrift.TException)1 CveSearchApi (org.eclipse.sw360.cvesearch.datasource.CveSearchApi)1