Browse Source

#1823 improve highlight content

Shinsuke Sugaya 7 years ago
parent
commit
b98a70226c

+ 10 - 11
src/main/java/org/codelibs/fess/helper/ViewHelper.java

@@ -124,7 +124,7 @@ public class ViewHelper {
 
     protected String escapedHighlightPost = null;
 
-    protected String highlightSplitPattern;
+    protected Set<Integer> hihglightTerminalCharSet = new HashSet<>();
 
     protected ActionHook actionHook = new ActionHook();
 
@@ -138,7 +138,7 @@ public class ViewHelper {
         highlightTagPre = fessConfig.getQueryHighlightTagPre();
         highlightTagPost = fessConfig.getQueryHighlightTagPost();
         highlightedFields = fessConfig.getQueryHighlightContentDescriptionFieldsAsArray();
-        highlightSplitPattern = fessConfig.getQueryHighlightSplitPattern();
+        fessConfig.getQueryHighlightTerminalChars().codePoints().forEach(hihglightTerminalCharSet::add);
     }
 
     public String getContentTitle(final Map<String, Object> document) {
@@ -193,17 +193,16 @@ public class ViewHelper {
 
     protected String escapeHighlight(final String text) {
         final String escaped = LaFunctions.h(text);
-        final String[] values = escaped.split(highlightSplitPattern, 2);
-        final String value;
-        if (values.length > 1) {
-            if (values[0].indexOf(escapedHighlightPre) == -1) {
-                value = values[1];
-            } else {
-                value = escaped;
+        int pos = escaped.indexOf(escapedHighlightPre);
+        while (pos >= 0) {
+            int c = escaped.codePointAt(pos);
+            if (Character.isISOControl(c) || hihglightTerminalCharSet.contains(c)) {
+                break;
             }
-        } else {
-            value = values[0];
+            pos--;
         }
+
+        final String value = escaped.substring(pos + 1);
         return value.replaceAll(escapedHighlightPre, highlightTagPre).replaceAll(escapedHighlightPost, highlightTagPost);
     }
 

+ 12 - 12
src/main/java/org/codelibs/fess/mylasta/direction/FessConfig.java

@@ -587,10 +587,10 @@ public interface FessConfig extends FessEnv, org.codelibs.fess.mylasta.direction
     /** The key of the configuration. e.g. true */
     String QUERY_REPLACE_TERM_WITH_PREFIX_QUERY = "query.replace.term.with.prefix.query";
 
-    /** The key of the configuration. e.g. [\p{Cc}\p{Z}\u3002] */
-    String QUERY_HIGHLIGHT_SPLIT_PATTERN = "query.highlight.split.pattern";
+    /** The key of the configuration. e.g. !.?։؟۔܀܁܂।၊။።፧፨᙮᠃᠉‼‽⁇⁈⁉。﹒﹗!.?。 */
+    String QUERY_HIGHLIGHT_TERMINAL_CHARS = "query.highlight.terminal.chars";
 
-    /** The key of the configuration. e.g. 80 */
+    /** The key of the configuration. e.g. 60 */
     String QUERY_HIGHLIGHT_FRAGMENT_SIZE = "query.highlight.fragment.size";
 
     /** The key of the configuration. e.g. 2 */
@@ -3125,22 +3125,22 @@ public interface FessConfig extends FessEnv, org.codelibs.fess.mylasta.direction
     boolean isQueryReplaceTermWithPrefixQuery();
 
     /**
-     * Get the value for the key 'query.highlight.split.pattern'. <br>
-     * The value is, e.g. [\p{Cc}\p{Z}\u3002] <br>
+     * Get the value for the key 'query.highlight.terminal.chars'. <br>
+     * The value is, e.g. !.?։؟۔܀܁܂।၊။።፧፨᙮᠃᠉‼‽⁇⁈⁉。﹒﹗!.?。 <br>
      * @return The value of found property. (NotNull: if not found, exception but basically no way)
      */
-    String getQueryHighlightSplitPattern();
+    String getQueryHighlightTerminalChars();
 
     /**
      * Get the value for the key 'query.highlight.fragment.size'. <br>
-     * The value is, e.g. 80 <br>
+     * The value is, e.g. 60 <br>
      * @return The value of found property. (NotNull: if not found, exception but basically no way)
      */
     String getQueryHighlightFragmentSize();
 
     /**
      * Get the value for the key 'query.highlight.fragment.size' as {@link Integer}. <br>
-     * The value is, e.g. 80 <br>
+     * The value is, e.g. 60 <br>
      * @return The value of found property. (NotNull: if not found, exception but basically no way)
      * @throws NumberFormatException When the property is not integer.
      */
@@ -6771,8 +6771,8 @@ public interface FessConfig extends FessEnv, org.codelibs.fess.mylasta.direction
             return is(FessConfig.QUERY_REPLACE_TERM_WITH_PREFIX_QUERY);
         }
 
-        public String getQueryHighlightSplitPattern() {
-            return get(FessConfig.QUERY_HIGHLIGHT_SPLIT_PATTERN);
+        public String getQueryHighlightTerminalChars() {
+            return get(FessConfig.QUERY_HIGHLIGHT_TERMINAL_CHARS);
         }
 
         public String getQueryHighlightFragmentSize() {
@@ -8411,8 +8411,8 @@ public interface FessConfig extends FessEnv, org.codelibs.fess.mylasta.direction
             defaultMap.put(FessConfig.QUERY_GEO_FIELDS, "location");
             defaultMap.put(FessConfig.QUERY_BROWSER_LANG_PARAMETER_NAME, "browser_lang");
             defaultMap.put(FessConfig.QUERY_REPLACE_TERM_WITH_PREFIX_QUERY, "true");
-            defaultMap.put(FessConfig.QUERY_HIGHLIGHT_SPLIT_PATTERN, "[\\p{Cc}\\p{Z}\\u3002]");
-            defaultMap.put(FessConfig.QUERY_HIGHLIGHT_FRAGMENT_SIZE, "80");
+            defaultMap.put(FessConfig.QUERY_HIGHLIGHT_TERMINAL_CHARS, "!.?։؟۔܀܁܂।၊။።፧፨᙮᠃᠉‼‽⁇⁈⁉。﹒﹗!.?。");
+            defaultMap.put(FessConfig.QUERY_HIGHLIGHT_FRAGMENT_SIZE, "60");
             defaultMap.put(FessConfig.QUERY_HIGHLIGHT_NUMBER_OF_FRAGMENTS, "2");
             defaultMap.put(FessConfig.QUERY_HIGHLIGHT_TYPE, "fvh");
             defaultMap.put(FessConfig.QUERY_HIGHLIGHT_TAG_PRE, "<strong>");

+ 2 - 2
src/main/resources/fess_config.properties

@@ -296,8 +296,8 @@ query.timeout.logging=true
 query.geo.fields=location
 query.browser.lang.parameter.name=browser_lang
 query.replace.term.with.prefix.query=true
-query.highlight.split.pattern=[\\p{Cc}\\p{Z}\\u3002]
-query.highlight.fragment.size=80
+query.highlight.terminal.chars=\u0021\u002E\u003F\u0589\u061F\u06D4\u0700\u0701\u0702\u0964\u104A\u104B\u1362\u1367\u1368\u166E\u1803\u1809\u203C\u203D\u2047\u2048\u2049\u3002\uFE52\uFE57\uFF01\uFF0E\uFF1F\uFF61
+query.highlight.fragment.size=60
 query.highlight.number.of.fragments=2
 query.highlight.type=fvh
 query.highlight.tag.pre=<strong>

+ 3 - 0
src/test/java/org/codelibs/fess/helper/ViewHelperTest.java

@@ -221,6 +221,9 @@ public class ViewHelperTest extends UnitFessTestCase {
         String text;
 
         text = "111 222" + viewHelper.originalHighlightTagPre + "aaa" + viewHelper.originalHighlightTagPost;
+        assertEquals("111 222" + viewHelper.highlightTagPre + "aaa" + viewHelper.highlightTagPost, viewHelper.escapeHighlight(text));
+
+        text = "111.222" + viewHelper.originalHighlightTagPre + "aaa" + viewHelper.originalHighlightTagPost;
         assertEquals("222" + viewHelper.highlightTagPre + "aaa" + viewHelper.highlightTagPost, viewHelper.escapeHighlight(text));
 
         text = "111\n222" + viewHelper.originalHighlightTagPre + "aaa" + viewHelper.originalHighlightTagPost;