Commit 2ede8de8 authored by Itamar Syn-Hershko's avatar Itamar Syn-Hershko
Browse files

Supporting latest HebMorph release

parent 20749589
......@@ -3,6 +3,6 @@ jvm=true
name=analysis-hebrew
description=Hebrew analyzer powered by HebMorph
classname=com.code972.elasticsearch.plugins.AnalysisPlugin
elasticsearch.version=2.3.5
java.version=1.7
version=2.3.5
elasticsearch.version=5.0.0
java.version=1.8
version=5.0.0
......@@ -9,14 +9,14 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<lucene.version>5.5.2</lucene.version>
<elasticsearch.version>2.4.0</elasticsearch.version>
<hebmorph.version>2.4.0</hebmorph.version>
<lucene.version>6.2.1</lucene.version>
<elasticsearch.version>5.0.0</elasticsearch.version>
<hebmorph.version>6.0.0</hebmorph.version>
</properties>
<groupId>com.code972.hebmorph</groupId>
<artifactId>elasticsearch-analysis-hebrew</artifactId>
<version>2.4.0-SNAPSHOT</version>
<version>5.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>elasticsearch-analysis-hebrew</name>
<url>http://code972.com/</url>
......
package com.code972.elasticsearch.plugins;
import com.code972.elasticsearch.rest.action.RestHebrewAnalyzerCheckWordAction;
import com.code972.hebmorph.DictionaryLoader;
import com.code972.hebmorph.hspell.HSpellDictionaryLoader;
import com.google.common.reflect.Reflection;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.analysis.AnalysisModule;
import org.elasticsearch.plugins.Plugin;
......@@ -13,14 +16,32 @@ public class AnalysisPlugin extends Plugin {
* If hebrew.dict.path is defined, try loading that.
*/
public AnalysisPlugin(Settings settings) {
final DictionaryLoader loader;
// TODO try locating HebMorphDictionaryLoader
loader = new HSpellDictionaryLoader();
Class dictLoader;
// TODO log dictionary loader used
// If a specific dictionary path was specified, force to use that
final String path = settings.get("hebrew.dict.path");
if (path != null && !path.isEmpty()) {
DictReceiver.setDictionary(path);
} else if (DictReceiver.getDictionary() == null) {
DictReceiver.setDictionary(loader, path);
} else {
// Default to loading from default locations
DictReceiver.setDictionary(loader);
}
if (DictReceiver.getDictionary() == null) {
throw new IllegalArgumentException("Could not load any dictionary. Aborting!");
}
}
@Override
public String name() {
return "elasticsearch-analysis-hebrew";
......
package com.code972.elasticsearch.plugins;
import com.code972.hebmorph.DictionaryLoader;
import com.code972.hebmorph.datastructures.DictHebMorph;
import com.code972.hebmorph.hspell.HSpellDictionaryLoader;
import org.elasticsearch.SpecialPermission;
import java.io.File;
......@@ -14,25 +14,20 @@ import java.security.PrivilegedAction;
* to initialize loading them and initializing the HebMorph analyzers.
*/
public class DictReceiver {
private static String[] filePaths = {"plugins/analysis-hebrew/dictionary.dict", "/var/lib/hebmorph/dictionary.dict",
"plugins/analysis-hebrew/hspell-data-files/", "/var/lib/hspell-data-files/"};
private static DictHebMorph dict = null;
public static DictHebMorph getDictionary() {
if (dict == null) {
dict = setDefaultDictionary();
}
return dict;
}
private static class LoadDictAction implements PrivilegedAction<DictHebMorph> {
private final String path;
private final HSpellDictionaryLoader loader;
private final DictionaryLoader loader;
public LoadDictAction(final String path) {
public LoadDictAction(final String path, final DictionaryLoader loader) {
this.path = path;
this.loader = new HSpellDictionaryLoader();
this.loader = loader;
}
@Override
......@@ -42,6 +37,7 @@ public class DictReceiver {
try {
return loader.loadDictionaryFromPath(path);
} catch (IOException e) {
// TODO remove printing
e.printStackTrace();
}
}
......@@ -49,7 +45,7 @@ public class DictReceiver {
}
}
public static boolean setDictionary(String path) {
public static boolean setDictionary(DictionaryLoader loader, String path) {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
// unprivileged code such as scripts do not have SpecialPermission
......@@ -57,24 +53,25 @@ public class DictReceiver {
}
if (path != null) {
final DictHebMorph tmp = AccessController.doPrivileged(new LoadDictAction(path));
final DictHebMorph tmp = AccessController.doPrivileged(new LoadDictAction(path, loader));
if (dict != null) {
dict = tmp;
return true;
}
}
return false;
}
private static DictHebMorph setDefaultDictionary() {
public static DictHebMorph setDictionary(DictionaryLoader loader) {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
// unprivileged code such as scripts do not have SpecialPermission
sm.checkPermission(new SpecialPermission());
}
for (final String path : filePaths) {
final DictHebMorph dict = AccessController.doPrivileged(new LoadDictAction(path));
for (final String path : loader.dictionaryPossiblePaths()) {
final DictHebMorph dict = AccessController.doPrivileged(new LoadDictAction(path, loader));
if (dict != null)
return dict;
}
......
package com.code972.elasticsearch.plugins;
import com.code972.hebmorph.DictionaryLoader;
import com.code972.hebmorph.hspell.HSpellDictionaryLoader;
import org.elasticsearch.common.inject.AbstractModule;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers;
public class HebMorphAnalysisModule extends AbstractModule {
protected final ESLogger log = Loggers.getLogger(this.getClass());
@Override
protected void configure() {
try {
Class hebMorphDictLoader;
if ((hebMorphDictLoader = Class
.forName("com.code972.hebmorph.HebMorphDictionaryLoader")) != null) {
bind(DictionaryLoader.class).to(hebMorphDictLoader).asEagerSingleton();
log.info("Auditlog available ({})", hebMorphDictLoader.getSimpleName());
} else {
throw new ClassNotFoundException();
}
} catch (ClassNotFoundException e) {
bind(DictionaryLoader.class).to(HSpellDictionaryLoader.class).asEagerSingleton();
log.info("Using hspell dictionary loader");
}
}
}
package com.code972.elasticsearch.plugins;
/**
* Created by synhershko on 06/11/2016.
*/
public class TestDictionaryLoaderDetection {
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment