-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from intuit/linear-cost-optimization
Linear cost optimization
- Loading branch information
Showing
38 changed files
with
1,049 additions
and
1,436 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 40 additions & 26 deletions
66
src/main/java/com/intuit/fuzzymatcher/component/ElementMatch.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,55 @@ | ||
package com.intuit.fuzzymatcher.component; | ||
|
||
import com.intuit.fuzzymatcher.domain.*; | ||
import com.intuit.fuzzymatcher.domain.Element; | ||
import com.intuit.fuzzymatcher.domain.Match; | ||
import com.intuit.fuzzymatcher.domain.Token; | ||
import org.apache.commons.lang3.BooleanUtils; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import java.util.*; | ||
|
||
/** | ||
* Matches at element level with aggregated results from token. | ||
* This uses the ScoringFunction defined at each element to get the aggregated Element score for matched tokens | ||
*/ | ||
public class ElementMatch { | ||
|
||
private static TokenMatch tokenMatch = new TokenMatch(); | ||
private final TokenRepo tokenRepo; | ||
|
||
public Stream<Match<Element>> matchElements(ElementClassification elementClassification, Stream<Element> elements) { | ||
Stream<Token> tokenStream = elements.flatMap(Element::getTokens); | ||
Stream<Match<Token>> matchedTokens = tokenMatch.matchTokens(elementClassification, tokenStream); | ||
return rollupElementScore(matchedTokens); | ||
public ElementMatch() { | ||
this.tokenRepo = new TokenRepo(); | ||
} | ||
|
||
private Stream<Match<Element>> rollupElementScore(Stream<Match<Token>> matchedTokenStream) { | ||
public Set<Match<Element>> matchElement(Element element) { | ||
Set<Match<Element>> matchElements = new HashSet<>(); | ||
Map<Element, Integer> elementTokenScore = new HashMap<>(); | ||
|
||
Map<Element, Map<Element, List<Match<Token>>>> groupBy = matchedTokenStream | ||
.collect(Collectors.groupingBy((matchToken -> matchToken.getData().getElement()), | ||
Collectors.groupingBy(matchToken -> matchToken.getMatchedWith().getElement()))); | ||
List<Token> tokens = element.getTokens(); | ||
tokens.stream() | ||
.filter(token -> BooleanUtils.isNotFalse(element.getDocument().isSource())) | ||
.forEach(token -> { | ||
elementThresholdMatching(token, elementTokenScore, matchElements); | ||
}); | ||
|
||
return groupBy.entrySet().parallelStream().flatMap(leftElementEntry -> | ||
leftElementEntry.getValue().entrySet().parallelStream().map(rightElementEntry -> { | ||
List<Score> childScoreList = rightElementEntry.getValue() | ||
.stream().map(d -> d.getScore()) | ||
.collect(Collectors.toList()); | ||
tokens.forEach(token -> tokenRepo.put(token)); | ||
|
||
return new Match<Element>(leftElementEntry.getKey(), rightElementEntry.getKey(), childScoreList); | ||
}).filter(match -> match.getResult() > match.getData().getThreshold())); | ||
return matchElements; | ||
} | ||
|
||
|
||
private void elementThresholdMatching(Token token, Map<Element, Integer> elementTokenScore, Set<Match<Element>> matchingElements) { | ||
Set<Element> matchElements = tokenRepo.get(token); | ||
Element element = token.getElement(); | ||
|
||
// Token Match Found | ||
if (matchElements != null) { | ||
matchElements.forEach(matchElement -> { | ||
int score = elementTokenScore.getOrDefault(matchElement, 0) + 1; | ||
elementTokenScore.put(matchElement, score); | ||
// Element Score above threshold | ||
double elementScore = element.getScore(score, matchElement); | ||
|
||
// Element match Found | ||
if (elementScore > element.getThreshold()) { | ||
Match<Element> elementMatch = new Match<>(element, matchElement, elementScore); | ||
matchingElements.remove(elementMatch); | ||
matchingElements.add(elementMatch); | ||
} | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 0 additions & 21 deletions
21
src/main/java/com/intuit/fuzzymatcher/component/TokenMatch.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.