{Java}MeCab


https://kakakakakku.hatenablog.com/entry/20120228/1330359548
https://www.oracle.com/java/technologies/downloads/#jdk17-windows
https://taku910.github.io/mecab/#install-windows


OS: Windows Server 2022
文字コード: SHIFT-JIS


-- 1. Java 17のインストール

[Environment]::SetEnvironmentVariable('JAVA_HOME', 'C:\Program Files\Java\jdk-17', 'User')
get-childitem env:JAVA_HOME

java -version
javac -version

notepad helloworld.java
public class helloworld {
  public static void main(String args) {
    System.out.println("Hello Java !");
  }
}

javac helloworld.java
java helloworld


-- 2. MeCabのインストール

下記をPATHに追加
C:\Program Files (x86)\MeCab\bin

mecab --help

-- 3. 動作確認

notepad C:\a.txt
顧客マスタ
注文明細テーブル
SKUマスタ
当月入金額

 

notepad MeCab.java

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class MeCab {
  public static void main(String args) {
    try {
      String[] command = { "cmd.exe", "/C", "mecab", "C:\\a.txt" };
      Process ps = Runtime.getRuntime().exec(command);
      BufferedReader bReader_i = new BufferedReader(new InputStreamReader(ps.getInputStream() ) );
      String targetLine;
      while (true) {
        targetLine = bReader_i.readLine();
        if (targetLine == null) {
          break;
        } else if (targetLine.equals("EOS") ) {
          continue;
        } else {
         String targetType = targetLine.split("[\t|,]")[1];
          if (targetType.equals("名詞") ) {
            System.out.println(targetLine);
          }
        }
      }
      ps.waitFor();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}


javac MeCab.java
java MeCab