View Javadoc
1   /*
2    * Copyright 2012-2019 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.helper;
17  
18  import java.util.Map;
19  
20  import javax.annotation.PostConstruct;
21  
22  import org.apache.tika.language.detect.LanguageDetector;
23  import org.apache.tika.language.detect.LanguageResult;
24  import org.codelibs.core.lang.StringUtil;
25  import org.codelibs.fess.mylasta.direction.FessConfig;
26  import org.codelibs.fess.util.ComponentUtil;
27  import org.codelibs.fess.util.DocumentUtil;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  public class LanguageHelper {
32      private static final Logger logger = LoggerFactory.getLogger(LanguageHelper.class);
33  
34      protected String[] langFields;
35  
36      protected String[] supportedLanguages;
37  
38      protected LanguageDetector detector;
39  
40      protected int maxTextLength;
41  
42      @PostConstruct
43      public void init() {
44          final FessConfig fessConfig = ComponentUtil.getFessConfig();
45          langFields = fessConfig.getIndexerLanguageFieldsAsArray();
46          supportedLanguages = fessConfig.getSupportedLanguagesAsArray();
47          maxTextLength = fessConfig.getIndexerLanguageDetectLengthAsInteger().intValue();
48      }
49  
50      public void updateDocument(final Map<String, Object> doc) {
51          final FessConfig fessConfig = ComponentUtil.getFessConfig();
52          String language = getSupportedLanguage(DocumentUtil.getValue(doc, fessConfig.getIndexFieldLang(), String.class));
53          if (language == null) {
54              for (final String f : langFields) {
55                  if (doc.containsKey(f)) {
56                      language = detectLanguage(DocumentUtil.getValue(doc, f, String.class));
57                      if (language != null) {
58                          if (logger.isDebugEnabled()) {
59                              logger.debug("set {} to lang field", language);
60                          }
61                          doc.put(fessConfig.getIndexFieldLang(), language);
62                          break;
63                      }
64                  }
65              }
66              if (language == null) {
67                  return;
68              }
69          }
70  
71          for (final String f : langFields) {
72              final String lf = f + "_" + language;
73              if (doc.containsKey(f) && !doc.containsKey(lf)) {
74                  doc.put(lf, doc.get(f));
75                  if (logger.isDebugEnabled()) {
76                      logger.debug("add {} field", lf);
77                  }
78              }
79          }
80      }
81  
82      protected String detectLanguage(final String text) {
83          if (StringUtil.isBlank(text)) {
84              return null;
85          }
86          final String target = getDetectText(text);
87          final LanguageResult result = detector.detect(target);
88          if (logger.isDebugEnabled()) {
89              logger.debug("detected lang:{}({}) from {}", result, result.getRawScore(), target);
90          }
91          return getSupportedLanguage(result.getLanguage());
92      }
93  
94      protected String getDetectText(final String text) {
95          final String result;
96          if (text.length() <= maxTextLength) {
97              result = text;
98          } else {
99              result = text.substring(0, maxTextLength);
100         }
101         return result.replaceAll("\\s+", " ");
102     }
103 
104     protected String getSupportedLanguage(final String lang) {
105         if (StringUtil.isBlank(lang)) {
106             return null;
107         }
108         for (final String l : supportedLanguages) {
109             if (l.equals(lang)) {
110                 return l;
111             }
112         }
113         return null;
114     }
115 
116     public void setDetector(final LanguageDetector detector) {
117         this.detector = detector;
118     }
119 
120 }