Skip to content

Commit

Permalink
fix #404 - add support for --languageModel command line option in HTT…
Browse files Browse the repository at this point in the history
…PServer
  • Loading branch information
czojo26 authored and danielnaber committed Jan 19, 2017
1 parent bc0dfe2 commit e71663d
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.languagetool.server;

import org.apache.commons.lang3.ArrayUtils;
import org.jetbrains.annotations.Nullable;
import org.languagetool.Language;
import org.languagetool.Languages;
Expand All @@ -38,6 +39,8 @@ enum Mode { LanguageTool, AfterTheDeadline }

/** The default port on which the server is running (8081). */
public static final int DEFAULT_PORT = 8081;

static final String LANGUAGE_MODEL_OPTION = "--languageModel";

protected boolean verbose = false;
protected boolean publicAccess = false;
Expand Down Expand Up @@ -86,7 +89,7 @@ public HTTPServerConfig(int serverPort, boolean verbose) {
for (int i = 0; i < args.length; i++) {
switch (args[i]) {
case "--config":
parseConfigFile(new File(args[++i]));
parseConfigFile(new File(args[++i]), !ArrayUtils.contains(args, LANGUAGE_MODEL_OPTION));
break;
case "-p":
case "--port":
Expand All @@ -105,11 +108,14 @@ public HTTPServerConfig(int serverPort, boolean verbose) {
throw new IllegalArgumentException("Missing argument for '--allow-origin'");
}
break;
case LANGUAGE_MODEL_OPTION:
setLanguageModelDirectory(args[++i]);
break;
}
}
}

private void parseConfigFile(File file) {
private void parseConfigFile(File file, boolean loadLangModel) {
try {
Properties props = new Properties();
try (FileInputStream fis = new FileInputStream(file)) {
Expand All @@ -124,11 +130,8 @@ private void parseConfigFile(File file) {
throw new IllegalArgumentException("Max queue size must be >= 0: " + maxWorkQueueSize);
}
String langModel = getOptionalProperty(props, "languageModel", null);
if (langModel != null) {
languageModelDir = new File(langModel);
if (!languageModelDir.exists() || !languageModelDir.isDirectory()) {
throw new RuntimeException("LanguageModel directory not found or is not a directory: " + languageModelDir);
}
if (langModel != null && loadLangModel) {
setLanguageModelDirectory(langModel);
}
maxCheckThreads = Integer.parseInt(getOptionalProperty(props, "maxCheckThreads", "10"));
if (maxCheckThreads < 1) {
Expand All @@ -151,6 +154,13 @@ private void parseConfigFile(File file) {
}
}

private void setLanguageModelDirectory(String langModelDir) {
languageModelDir = new File(langModelDir);
if (!languageModelDir.exists() || !languageModelDir.isDirectory()) {
throw new RuntimeException("LanguageModel directory not found or is not a directory: " + languageModelDir);
}
}

/*
* @param verbose if true, the text to be checked will be displayed in case of exceptions
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,18 @@ protected static void printCommonConfigFileOptions() {
System.out.println(" 'maxWorkQueueSize' - reject request if request queue gets larger than this (optional)");
System.out.println(" 'rulesFile' - a file containing rules configuration, such as .langugagetool.cfg (optional)");
}

protected static void printCommonOptions() {
System.out.println(" --port, -p PRT port to bind to, defaults to " + DEFAULT_PORT + " if not specified");
System.out.println(" --public allow this server process to be connected from anywhere; if not set,");
System.out.println(" it can only be connected from the computer it was started on");
System.out.println(" --allow-origin ORIGIN set the Access-Control-Allow-Origin header in the HTTP response,");
System.out.println(" --port, -p PRT port to bind to, defaults to " + DEFAULT_PORT + " if not specified");
System.out.println(" --public allow this server process to be connected from anywhere; if not set,");
System.out.println(" it can only be connected from the computer it was started on");
System.out.println(" --allow-origin ORIGIN set the Access-Control-Allow-Origin header in the HTTP response,");
System.out.println(" used for direct (non-proxy) JavaScript-based access from browsers;");
System.out.println(" example: --allow-origin \"*\"");
System.out.println(" --verbose, -v in case of exceptions, log the input text (up to 500 characters)");
System.out.println(" --verbose, -v in case of exceptions, log the input text (up to 500 characters)");
System.out.println(" --languageModel a directory with '1grams', '2grams', '3grams' sub directories (per language)");
System.out.println(" which contain a Lucene index (optional, overwrites 'languageModel'");
System.out.println(" parameter in properties files)");
}

protected static void checkForNonRootUser() {
Expand All @@ -141,7 +144,7 @@ protected ThreadPoolExecutor getExecutorService(LinkedBlockingQueue<Runnable> wo
}

static class StoppingThreadPoolExecutor extends ThreadPoolExecutor {

StoppingThreadPoolExecutor(int threadPoolSize, LinkedBlockingQueue<Runnable> workQueue) {
super(threadPoolSize, threadPoolSize, 0L, TimeUnit.MILLISECONDS, workQueue);
}
Expand All @@ -157,5 +160,5 @@ protected void afterExecute(Runnable r, Throwable t) {
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
*/
package org.languagetool.server;

import org.hamcrest.core.Is;
import org.junit.Assert;
import org.junit.Test;
import java.io.IOException;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
Expand Down Expand Up @@ -48,4 +51,20 @@ public void testArgumentParsing() {
assertThat(config4.isVerbose(), is(false));
}

@Test
public void shouldLoadLanguageModelDirectoryFromCommandLineArguments() throws IOException {
//given
ClassLoader classLoader = this.getClass().getClassLoader();
String languageModelDirectory = "languageModelDirectory";
String targetLanguageModelDirectory = classLoader.getResource("org/languagetool/server/" + languageModelDirectory).getFile();

//when
HTTPServerConfig config = new HTTPServerConfig(new String[]{HTTPServerConfig.LANGUAGE_MODEL_OPTION, targetLanguageModelDirectory});

//then
Assert.assertNotNull(config.languageModelDir);
Assert.assertTrue(config.languageModelDir.exists());
Assert.assertTrue(config.languageModelDir.getAbsolutePath().endsWith(languageModelDirectory));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
File created to save directory structure.
4 changes: 4 additions & 0 deletions languagetool-standalone/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
#### Russian
* added and improved rules

#### Command-line
* Added a `--languageModel` option to the embedded server, thanks to
Michał Janik (issue #404)

#### Java API
* Some deprecated methods have been removed.

Expand Down

0 comments on commit e71663d

Please sign in to comment.