You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by ki...@apache.org on 2018/04/21 22:50:06 UTC

jena git commit: JENA-1488: add a selective folding analyzer

Repository: jena
Updated Branches:
  refs/heads/master be6edc53d -> a56f7710d


JENA-1488: add a selective folding analyzer


Project: http://git-wip-us.apache.org/repos/asf/jena/repo
Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/a56f7710
Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/a56f7710
Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/a56f7710

Branch: refs/heads/master
Commit: a56f7710d88e368824042863e1ebef9afc7fd5f3
Parents: be6edc5
Author: Bruno P. Kinoshita <ki...@users.noreply.github.com>
Authored: Mon Apr 9 21:38:14 2018 +1200
Committer: Bruno P. Kinoshita <ki...@users.noreply.github.com>
Committed: Sun Apr 22 10:42:21 2018 +1200

----------------------------------------------------------------------
 .../text/filter/SelectiveFoldingFilter.java     |  92 +++++++++++++
 .../text/filter/TestSelectiveFoldingFilter.java | 134 +++++++++++++++++++
 2 files changed, 226 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/a56f7710/jena-text/src/main/java/org/apache/jena/query/text/filter/SelectiveFoldingFilter.java
----------------------------------------------------------------------
diff --git a/jena-text/src/main/java/org/apache/jena/query/text/filter/SelectiveFoldingFilter.java b/jena-text/src/main/java/org/apache/jena/query/text/filter/SelectiveFoldingFilter.java
new file mode 100644
index 0000000..7483a06
--- /dev/null
+++ b/jena-text/src/main/java/org/apache/jena/query/text/filter/SelectiveFoldingFilter.java
@@ -0,0 +1,92 @@
+/**
+ * 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.jena.query.text.filter;
+
+import java.io.IOException;
+import java.util.Objects;
+
+import org.apache.lucene.analysis.CharArraySet;
+import org.apache.lucene.analysis.TokenFilter;
+import org.apache.lucene.analysis.TokenStream;
+import org.apache.lucene.analysis.miscellaneous.ASCIIFoldingFilter;
+import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
+import org.apache.lucene.util.ArrayUtil;
+
+/**
+ * A Lucene filter based on ASCIIFoldingFilter, but that allows the
+ * caller to provide a white list for selective folding filter. Entries in the
+ * white list are ignored during the filtering. This is useful for languages
+ * that require certain characters to be ignored.
+ *
+ * @link ASCIIFoldingFilter
+ */
+
+public final class SelectiveFoldingFilter extends TokenFilter {
+    /*
+     * text:defineFilter :selectiveFoldingFilter ;
+     * text:filter [
+     *      a text:GenericFilter ;
+     *      text:class "org.apache.jena.query.text.filter.SelectiveFoldingFilter" ;
+     *      text:params (
+     *           [ 
+     *                text:paramName "whitelisted" ;
+     *                text:paramType text:TypeSet ;
+     *                text:paramValue ("ç" "á")
+     *           ]
+     *      )
+     * ]
+     */
+
+    private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
+
+    /**
+     * List of whitelisted characters.
+     */
+    private CharArraySet whitelisted;
+
+    public SelectiveFoldingFilter(TokenStream input, CharArraySet whitelisted) {
+        super(input);
+        Objects.requireNonNull(whitelisted, "You must provide the list of whiltelisted characters.");
+        this.whitelisted = CharArraySet.unmodifiableSet(CharArraySet.copy(whitelisted));
+    }
+
+    @Override
+    public boolean incrementToken() throws IOException {
+        if (input.incrementToken()) {
+            final char[] buffer = termAtt.buffer();
+            final int length = termAtt.length();
+            // prepare the output char array, adapted from ASCIIFoldingFilter
+            final int maxSizeNeeded = 4 * length;
+            char[] output = new char[ArrayUtil.oversize(maxSizeNeeded, Character.BYTES)];
+            for (int i = 0; i < length; ++i) {
+                final char c = buffer[i];
+                if (c >= '\u0080' && !whitelisted.contains(c)) {
+                    // here we are using the method that will iterate always over a list with a
+                    // single char
+                    ASCIIFoldingFilter.foldToASCII(buffer, i, output, i, 1);
+                } else {
+                    output[i] = c;
+                }
+            }
+            termAtt.copyBuffer(output, 0, length);
+            return true;
+        }
+        return false;
+    }
+}

http://git-wip-us.apache.org/repos/asf/jena/blob/a56f7710/jena-text/src/test/java/org/apache/jena/query/text/filter/TestSelectiveFoldingFilter.java
----------------------------------------------------------------------
diff --git a/jena-text/src/test/java/org/apache/jena/query/text/filter/TestSelectiveFoldingFilter.java b/jena-text/src/test/java/org/apache/jena/query/text/filter/TestSelectiveFoldingFilter.java
new file mode 100644
index 0000000..7d4a7ae
--- /dev/null
+++ b/jena-text/src/test/java/org/apache/jena/query/text/filter/TestSelectiveFoldingFilter.java
@@ -0,0 +1,134 @@
+/**
+ * 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.jena.query.text.filter;
+
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+import java.io.StringReader;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.lucene.analysis.CharArraySet;
+import org.apache.lucene.analysis.standard.StandardTokenizer;
+import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Test {@link SelectiveFoldingFilter}.
+ */
+
+public class TestSelectiveFoldingFilter {
+
+    private StringReader inputText;
+    private CharArraySet whitelisted;
+
+    @Before
+    public void setUp() {
+        inputText = new StringReader("Señora Siobhán, look at that façade");
+    }
+
+    /**
+     * An empty white list means that the default behaviour of the Lucene's ASCIIFoldingFilter applies.
+     * @throws IOException from Lucene API
+     */
+    @Test
+    public void testEmptyWhiteListIsOkay() throws IOException {
+        whitelisted = new CharArraySet(Collections.emptyList(), false);
+        List<String> tokens = collectTokens(inputText, whitelisted);
+        List<String> expected = Arrays.asList("Senora", "Siobhan", "look", "at", "that", "facade");
+        assertTrue(tokens.equals(expected));
+    }
+
+    @Test
+    public void testSingleCharacterWhiteListed() throws IOException {
+        whitelisted = new CharArraySet(Arrays.asList("ç"), false);
+        List<String> tokens = collectTokens(inputText, whitelisted);
+        List<String> expected = Arrays.asList("Senora", "Siobhan", "look", "at", "that", "façade");
+        assertTrue(tokens.equals(expected));
+    }
+
+    @Test
+    public void testCompleteWhiteListed() throws IOException {
+        whitelisted = new CharArraySet(Arrays.asList("ñ", "á", "ç"), false);
+        List<String> tokens = collectTokens(inputText, whitelisted);
+        // here we should have the complete input
+        List<String> expected = Arrays.asList("Señora", "Siobhán", "look", "at", "that", "façade");
+        assertTrue(tokens.equals(expected));
+    }
+
+    @Test
+    public void testCaseMatters() throws IOException {
+        // note the first capital letter
+        whitelisted = new CharArraySet(Arrays.asList("Ñ", "á", "ç"), false);
+        List<String> tokens = collectTokens(inputText, whitelisted);
+        List<String> expected = Arrays.asList("Senora", "Siobhán", "look", "at", "that", "façade");
+        assertTrue(tokens.equals(expected));
+    }
+
+    @Test
+    public void testMismatchWhiteList() throws IOException {
+        whitelisted = new CharArraySet(Arrays.asList("ú", "ć", "ž"), false);
+        List<String> tokens = collectTokens(inputText, whitelisted);
+        List<String> expected = Arrays.asList("Senora", "Siobhan", "look", "at", "that", "facade");
+        assertTrue(tokens.equals(expected));
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void testNullWhiteListThrowsError() throws IOException {
+        collectTokens(inputText, null);
+    }
+
+    @Test
+    public void testEmptyInput() throws IOException {
+        whitelisted = new CharArraySet(Arrays.asList("ç"), false);
+        inputText = new StringReader("");
+        List<String> tokens = collectTokens(inputText, whitelisted);
+        List<String> expected = Collections.emptyList();
+        assertTrue(tokens.equals(expected));
+    }
+
+    /**
+     * Return the list of CharTermAttribute converted to a list of String's.
+     *
+     * @param whitelisted white-list
+     * @return list of CharTermAttribute converted to a list of String's
+     * @throws IOException from Lucene API
+     */
+    private List<String> collectTokens(StringReader inputText, CharArraySet whitelisted) throws IOException {
+        StandardTokenizer tokenizer = new StandardTokenizer();
+        tokenizer.setReader(inputText);
+
+        SelectiveFoldingFilter selectiveFoldingFilter = new SelectiveFoldingFilter(tokenizer, whitelisted);
+
+        CharTermAttribute termAttrib = (CharTermAttribute) selectiveFoldingFilter.getAttribute(CharTermAttribute.class);
+
+        selectiveFoldingFilter.reset();
+        List<String> tokens = new ArrayList<>();
+        while (selectiveFoldingFilter.incrementToken()) {
+            tokens.add(termAttrib.toString());
+        }
+        selectiveFoldingFilter.end();
+        selectiveFoldingFilter.close();
+        return tokens;
+    }
+}