-
Notifications
You must be signed in to change notification settings - Fork 25.7k
Add stopword support to IntervalBuilder #39637
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
87647f0
Use Intervals.extend() to preserve stopword gaps when building interv…
romseygeek 899f1a4
Merge remote-tracking branch 'origin/master' into interval-spaces
romseygeek 3ccac74
cleanup
romseygeek 3b7c1d4
More tests
romseygeek b8178d9
Fix simple synonynms
romseygeek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -143,38 +143,50 @@ protected static IntervalsSource combineSources(List<IntervalsSource> sources, i | |
| protected List<IntervalsSource> analyzeTerms(TokenStream ts) throws IOException { | ||
| List<IntervalsSource> terms = new ArrayList<>(); | ||
| TermToBytesRefAttribute bytesAtt = ts.addAttribute(TermToBytesRefAttribute.class); | ||
| PositionIncrementAttribute posAtt = ts.addAttribute(PositionIncrementAttribute.class); | ||
| ts.reset(); | ||
| while (ts.incrementToken()) { | ||
| BytesRef term = bytesAtt.getBytesRef(); | ||
| terms.add(Intervals.term(BytesRef.deepCopyOf(term))); | ||
| int precedingSpaces = posAtt.getPositionIncrement() - 1; | ||
| terms.add(extend(Intervals.term(BytesRef.deepCopyOf(term)), precedingSpaces)); | ||
| } | ||
| ts.end(); | ||
| return terms; | ||
| } | ||
|
|
||
| public static IntervalsSource extend(IntervalsSource source, int precedingSpaces) { | ||
| if (precedingSpaces == 0) { | ||
| return source; | ||
| } | ||
| return Intervals.extend(source, precedingSpaces, 0); | ||
| } | ||
|
|
||
| protected IntervalsSource analyzeSynonyms(TokenStream ts, int maxGaps, boolean ordered) throws IOException { | ||
| List<IntervalsSource> terms = new ArrayList<>(); | ||
| List<IntervalsSource> synonyms = new ArrayList<>(); | ||
| TermToBytesRefAttribute bytesAtt = ts.addAttribute(TermToBytesRefAttribute.class); | ||
| PositionIncrementAttribute posAtt = ts.addAttribute(PositionIncrementAttribute.class); | ||
| ts.reset(); | ||
| int spaces = 0; | ||
| while (ts.incrementToken()) { | ||
| if (posAtt.getPositionIncrement() == 1) { | ||
| int posInc = posAtt.getPositionIncrement(); | ||
| if (posInc > 0) { | ||
| if (synonyms.size() == 1) { | ||
| terms.add(synonyms.get(0)); | ||
| terms.add(extend(synonyms.get(0), spaces)); | ||
| } | ||
| else if (synonyms.size() > 1) { | ||
| terms.add(Intervals.or(synonyms.toArray(new IntervalsSource[0]))); | ||
| terms.add(extend(Intervals.or(synonyms.toArray(new IntervalsSource[0])), spaces)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, you should use |
||
| } | ||
| synonyms.clear(); | ||
| spaces = posInc - 1; | ||
| } | ||
| synonyms.add(Intervals.term(BytesRef.deepCopyOf(bytesAtt.getBytesRef()))); | ||
| } | ||
| if (synonyms.size() == 1) { | ||
| terms.add(synonyms.get(0)); | ||
| terms.add(extend(synonyms.get(0), spaces)); | ||
| } | ||
| else { | ||
| terms.add(Intervals.or(synonyms.toArray(new IntervalsSource[0]))); | ||
| terms.add(extend(Intervals.or(synonyms.toArray(new IntervalsSource[0])), spaces)); | ||
| } | ||
| return combineSources(terms, maxGaps, ordered); | ||
| } | ||
|
|
||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -94,6 +94,22 @@ public void testPhrase() throws IOException { | |
|
|
||
| } | ||
|
|
||
| public void testPhraseWithStopword() throws IOException { | ||
|
|
||
| CannedTokenStream ts = new CannedTokenStream( | ||
| new Token("term1", 1, 1, 2), | ||
| new Token("term3", 2, 5, 6) | ||
| ); | ||
|
|
||
| IntervalsSource source = BUILDER.analyzeText(new CachingTokenFilter(ts), 0, true); | ||
| IntervalsSource expected = Intervals.phrase( | ||
| Intervals.term("term1"), Intervals.extend(Intervals.term("term3"), 1, 0) | ||
| ); | ||
|
|
||
| assertEquals(expected, source); | ||
|
|
||
| } | ||
|
|
||
| public void testSimpleSynonyms() throws IOException { | ||
|
|
||
| CannedTokenStream ts = new CannedTokenStream( | ||
|
|
@@ -112,16 +128,32 @@ public void testSimpleSynonyms() throws IOException { | |
|
|
||
| } | ||
|
|
||
| public void testGraphSynonyms() throws IOException { | ||
| public void testSimpleSynonymsWithGap() throws IOException { | ||
| // term1 [] term2/term3/term4 term5 | ||
| CannedTokenStream ts = new CannedTokenStream( | ||
| new Token("term1", 1, 2), | ||
| new Token("term2", 2, 3, 4), | ||
| new Token("term3", 0, 3, 4), | ||
| new Token("term4", 0, 3, 4), | ||
| new Token("term5", 5, 6) | ||
| ); | ||
|
|
||
| IntervalsSource source = BUILDER.analyzeText(new CachingTokenFilter(ts), -1, true); | ||
| IntervalsSource expected = Intervals.ordered( | ||
| Intervals.term("term1"), | ||
| Intervals.extend(Intervals.or(Intervals.term("term2"), Intervals.term("term3"), Intervals.term("term4")), 1, 0), | ||
| Intervals.term("term5") | ||
| ); | ||
| assertEquals(expected, source); | ||
| } | ||
|
|
||
| // term1 term2/term3:2 term4 term5 | ||
| public void testGraphSynonyms() throws IOException { | ||
|
|
||
| Token graphToken = new Token("term2", 3, 4); | ||
| graphToken.setPositionLength(2); | ||
| // term1 term2:2/term3 term4 term5 | ||
|
|
||
| CannedTokenStream ts = new CannedTokenStream( | ||
| new Token("term1", 1, 2), | ||
| graphToken, | ||
| new Token("term2", 1, 3, 4, 2), | ||
| new Token("term3", 0, 3, 4), | ||
| new Token("term4", 5, 6), | ||
| new Token("term5", 6, 7) | ||
|
|
@@ -138,4 +170,50 @@ public void testGraphSynonyms() throws IOException { | |
|
|
||
| } | ||
|
|
||
| public void testGraphSynonymsWithGaps() throws IOException { | ||
|
|
||
| // term1 [] term2:4/term3 [] [] term4 term5 | ||
|
|
||
| CannedTokenStream ts = new CannedTokenStream( | ||
| new Token("term1", 1, 2), | ||
| new Token("term2", 2, 3, 4, 4), | ||
| new Token("term3", 0, 3, 4), | ||
| new Token("term4", 3, 5, 6), | ||
| new Token("term5", 6, 7) | ||
| ); | ||
|
|
||
| IntervalsSource source = BUILDER.analyzeText(new CachingTokenFilter(ts), -1, true); | ||
| IntervalsSource expected = Intervals.ordered( | ||
| Intervals.term("term1"), | ||
| Intervals.or( | ||
| Intervals.extend(Intervals.term("term2"), 1, 0), | ||
| Intervals.phrase( | ||
| Intervals.extend(Intervals.term("term3"), 1, 0), | ||
| Intervals.extend(Intervals.term("term4"), 2, 0))), | ||
| Intervals.term("term5") | ||
| ); | ||
|
|
||
| assertEquals(expected, source); | ||
|
|
||
| } | ||
|
|
||
| public void testGraphTerminatesOnGap() throws IOException { | ||
| // term1 term2:2/term3 term4 [] term5 | ||
| CannedTokenStream ts = new CannedTokenStream( | ||
| new Token("term1", 1, 2), | ||
| new Token("term2", 1, 2, 3, 2), | ||
| new Token("term3", 0, 2, 3), | ||
| new Token("term4", 2, 3), | ||
| new Token("term5", 2, 6, 7) | ||
| ); | ||
|
|
||
| IntervalsSource source = BUILDER.analyzeText(new CachingTokenFilter(ts), -1, true); | ||
| IntervalsSource expected = Intervals.ordered( | ||
| Intervals.term("term1"), | ||
| Intervals.or(Intervals.term("term2"), Intervals.phrase("term3", "term4")), | ||
| Intervals.extend(Intervals.term("term5"), 1, 0) | ||
| ); | ||
| assertEquals(expected, source); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you also add a test with a simple word synonym that starts after a gap ? |
||
|
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
synonymscontains the word at the position before the gap so theextendshould be applied forward ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It applies backwards because we're using PositionIncrement to detect when there's a gap preceding us in the TokenStream. If you've got a posInc of 2, that means theres a gap before you in the stream, so you need to extend backwards 1 to cover it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, actually no, it's a bug - I added the test you suggested and it fails, have updated.