| /* |
| * 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 opennlp.tools.jsmlearning; |
| |
| import java.io.BufferedReader; |
| import java.io.File; |
| import java.io.IOException; |
| import java.io.InputStream; |
| import java.io.InputStreamReader; |
| |
| public class TreeKernelRunner { |
| private void runEXE(String[] command, String runPath){ |
| Runtime r = Runtime.getRuntime(); |
| Process mStartProcess = null; |
| try { |
| mStartProcess = r.exec( command, null, new File(runPath)); |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } |
| |
| StreamLogger outputGobbler = new StreamLogger(mStartProcess.getInputStream()); |
| outputGobbler.start(); |
| |
| try { |
| int returnCode = mStartProcess.waitFor(); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } |
| } |
| |
| public void runLearner(String dir, String learning_file, String model_file) |
| { |
| dir = dir.replace('/', '\\'); |
| |
| if (!dir.endsWith("\\")) |
| dir+="\\"; |
| String[] runString = new String[]{dir+"svm_learn.exe","-t", "5", dir+learning_file, dir+model_file}; |
| runEXE(runString, dir); |
| } |
| |
| |
| //svm_classify example_file model_file predictions_file |
| public void runClassifier(String dir, String example_file, String model_file, String predictions_file) |
| { |
| dir = dir.replace('/', '\\'); |
| |
| if (!dir.endsWith("\\")) |
| dir+="\\"; |
| String[] runString = new String[]{dir+"svm_classify.exe", dir+example_file, dir+model_file, dir+predictions_file}; |
| runEXE(runString, dir); |
| } |
| |
| static class StreamLogger extends Thread{ |
| |
| private InputStream mInputStream; |
| |
| public StreamLogger(InputStream is) { |
| this.mInputStream = is; |
| } |
| |
| public void run() { |
| try { |
| InputStreamReader isr = new InputStreamReader(mInputStream); |
| BufferedReader br = new BufferedReader(isr); |
| String line = null; |
| while ((line = br.readLine()) != null) { |
| System.out.println(line); |
| } |
| } catch (IOException ioe) { |
| ioe.printStackTrace(); |
| } |
| } |
| |
| } |
| |
| public static void main(String[] args){ |
| TreeKernelRunner runner = new TreeKernelRunner(); |
| runner.runLearner("C:\\stanford-corenlp\\tree_kernel\\", "training.txt", "arg0.model1.txt"); |
| runner.runClassifier("C:\\stanford-corenlp\\tree_kernel\\", "arg0.test", "arg0.model1.txt", "arg0.output1.txt"); |
| } |
| } |
| |
| /* |
| exec: |
| |
| public Process exec(String command, String envp[], File dir) |
| |
| |
| |
| @param command a specified system command. |
| @param envp array of strings, each element of which |
| has environment variable settings in format |
| <i>name</i>=<i>value</i>. |
| @param dir the working directory of the subprocess, or |
| <tt>null</tt> if the subprocess should inherit |
| the working directory of the current process. |
| |
| В ди�трибутиве два exe-файла: svm_learn.exe и svm_classify.exe. |
| |
| 1. svm_learn.exe берет файл � примерами, обрабатывает его, �троит файл model м правилами обучение. |
| |
| Примеры запу�ка: |
| svm_learn -t 5 learning_file model_file - �то �амый про�той вариант запу�ка, SubSetTreeKernel (допу�кают�� разрывы при обходе деревьев) |
| |
| svm_learn -t 5 -D 0 learning_file model_file - другой вариант �дра, SubTreeKernel |
| |
| Пример файла лежит на его �траничке. Там же опи�ание параметров. |
| |
| 2. svm_classify.exe берет файл � те�товыми примерами, файл � моделью, по�троенный svm_learn, и запи�ывает результаты обучени� в файл predictions_file. |
| |
| Запу�к: svm_classify example_file model_file predictions_file |
| |
| Файл имеет тот же формат, что и входные примеры. Образец лежит в архиве на �траничке Мо�китти. |
| Можно �разу же указывать, к какому кла��у отно�ит�� пример (1 или -1 в начале �троки). В �том �лучае точно�ть и полнота оценивают�� автоматиче�ки. Или �тавить там 0. |
| */ |