gus-asf commented on a change in pull request #1965:
URL: https://github.com/apache/lucene-solr/pull/1965#discussion_r505119975



##########
File path: 
lucene/analysis/common/src/test/org/apache/lucene/analysis/miscellaneous/TestTypeAsSynonymFilter.java
##########
@@ -0,0 +1,99 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.analysis.miscellaneous;
+
+import java.util.Collections;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import org.apache.lucene.analysis.BaseTokenStreamTestCase;
+import org.apache.lucene.analysis.CannedTokenStream;
+import org.apache.lucene.analysis.Token;
+import org.apache.lucene.analysis.TokenStream;
+
+/**
+ * Test that this filter moves the value in type to a synonym token with the 
same offsets. This is rarely
+ * useful by itself, but in combination with another filter that updates the 
type value with an appropriate
+ * synonym can be used to identify synonyms before tokens are modified by 
further analysis, and then
+ * add them at the end, ensuring that the synonym value has not ben subjected 
to the intervening analysis.
+ * This typically applies when the analysis would remove characters that 
should remain in the synonym.
+ */
+public class TestTypeAsSynonymFilter extends BaseTokenStreamTestCase {
+
+  /**
+   * Test the straight forward case with the simplest constructor. Simply 
converts every
+   * type to a synonym. Typically one wants to also set an ignore list 
containing "word" unless
+   * that default value is removed by prior analysis.
+   */
+  public void testSimple() throws Exception {
+
+    Token token = new Token("foo", 0, 2);
+    token.setType("bar");
+    Token token2 = new Token("foo", 4, 6);
+    TokenStream ts = new CannedTokenStream(token, token2);
+    ts = new TypeAsSynonymFilter(ts);
+
+    // "word" is the default type!
+    assertTokenStreamContents(ts, new String[] {
+        "foo", "bar","foo","word"},new int[] {0,0,4,4}, new int[]{2,2,6,6}, 
new int[] {1,0,1,0});
+  }
+
+  /**
+   * Tests that we can add a prefix to the synonym (for example, to keep it 
from ever matching user input directly),
+   * and test that we can ignore a list of type values we don't wish to turn 
into synonyms.
+   */
+  public void testWithPrefixAndIgnore() throws Exception {
+    Token[] tokens = new Token[] {
+        new Token("foo", 1, 3),
+        new Token("foo", 5, 7),
+        new Token("foo", 9, 11),
+    } ;
+    tokens[0].setType("bar");
+    tokens[2].setType("ignoreme");
+    TokenStream ts = new CannedTokenStream(tokens);
+    ts = new TypeAsSynonymFilter(ts,"pfx_", 
Stream.of("word","ignoreme").collect(Collectors.toSet()), 0);

Review comment:
       Ah yeah sure. Originally developed vs 8.4 but now I can use it :)




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to