Opennlp-named-entity-recognition
OpenNLP-名前付きエンティティの認識
特定のテキストから名前、人、場所、およびその他のエンティティを見つけるプロセスは、* N amed E ntity R * ecognition(NER)と呼ばれます。 この章では、OpenNLPライブラリを使用してJavaプログラムでNERを実行する方法について説明します。
オープンNLPを使用した名前付きエンティティ認識
さまざまなNERタスクを実行するために、OpenNLPは異なる事前定義モデル、つまりen-nerdate.bn、en-ner-location.bin、en-ner-organization.bin、en-ner-person.bin、およびen-ner-timeを使用します。ビン。 これらのファイルはすべて、所定の生テキスト内の各エンティティを検出するようにトレーニングされた事前定義モデルです。
*opennlp.tools.namefind* パッケージには、NERタスクの実行に使用されるクラスとインターフェースが含まれています。 OpenNLPライブラリを使用してNERタスクを実行するには、する必要があります-
- TokenNameFinderModel クラスを使用して、それぞれのモデルをロードします。
- NameFinder クラスをインスタンス化します。
- 名前を見つけて印刷します。
以下は、特定の生テキストから名前エンティティを検出するプログラムを作成するために従うべき手順です。
ステップ1:モデルの読み込み
文検出のモデルは、パッケージ opennlp.tools.namefind に属する TokenNameFinderModel という名前のクラスで表されます。
NERモデルを読み込むには-
- モデルの InputStream オブジェクトを作成します(FileInputStreamをインスタンス化し、適切なNERモデルのパスをString形式でコンストラクターに渡します)。
- 次のコードブロックに示すように、 TokenNameFinderModel クラスをインスタンス化し、モデルの InputStream (オブジェクト)をコンストラクターにパラメーターとして渡します。
//Loading the NER-person model
InputStream inputStreamNameFinder = new FileInputStream(".../en-nerperson.bin");
TokenNameFinderModel model = new TokenNameFinderModel(inputStreamNameFinder);
ステップ2:NameFinderMEクラスのインスタンス化
パッケージ opennlp.tools.namefind の NameFinderME クラスには、NERタスクを実行するためのメソッドが含まれています。 このクラスは、最大エントロピーモデルを使用して、指定された生テキスト内の名前付きエンティティを検索します。
このクラスをインスタンス化し、以下に示すように前のステップで作成されたモデルオブジェクトを渡します-
//Instantiating the NameFinderME class
NameFinderME nameFinder = new NameFinderME(model);
ステップ3:文中の名前を見つける
*NameFinderME* クラスの* find()*メソッドは、渡された生テキストの名前を検出するために使用されます。 このメソッドは、パラメーターとしてString変数を受け入れます。
文の文字列形式をこのメソッドに渡すことにより、このメソッドを呼び出します。
//Finding the names in the sentence
Span nameSpans[] = nameFinder.find(sentence);
ステップ4:文中の名前のスパンを印刷する
*NameFinderME* クラスの* find()*メソッドは、Span型のオブジェクトの配列を返します。 *opennlp.tools.util* パッケージのSpanという名前のクラスは、セットの *start* および *end* 整数を格納するために使用されます。
次のコードブロックに示すように、* find()*メソッドによって返されたスパンをSpan配列に格納して印刷できます。
//Printing the sentences and their spans of a sentence
for (Span span : spans)
System.out.println(paragraph.substring(span);
- NERの例*
以下は、与えられた文を読み、その中の人の名前の範囲を認識するプログラムです。 このプログラムを NameFinderME_Example.java という名前のファイルに保存します。
import java.io.FileInputStream;
import java.io.InputStream;
import opennlp.tools.namefind.NameFinderME;
import opennlp.tools.namefind.TokenNameFinderModel;
import opennlp.tools.util.Span;
public class NameFinderME_Example {
public static void main(String args[]) throws Exception{
/Loading the NER - Person model InputStream inputStream = new
FileInputStream("C:/OpenNLP_models/en-ner-person.bin");
TokenNameFinderModel model = new TokenNameFinderModel(inputStream);
//Instantiating the NameFinder class
NameFinderME nameFinder = new NameFinderME(model);
//Getting the sentence in the form of String array
String [] sentence = new String[]{
"Mike",
"and",
"Smith",
"are",
"good",
"friends"
};
//Finding the names in the sentence
Span nameSpans[] = nameFinder.find(sentence);
//Printing the spans of the names in the sentence
for(Span s: nameSpans)
System.out.println(s.toString());
}
}
次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します-
javac NameFinderME_Example.java
java NameFinderME_Example
上記のプログラムは、実行時に、指定された文字列(生テキスト)を読み取り、その中の人物の名前を検出し、以下に示すようにその位置(スパン)を表示します。
[0..1) person
[2..3) person
名前とその位置
Stringクラスの* substring()メソッドは、 *begin および end offsets を受け入れ、それぞれの文字列を返します。 次のコードブロックに示すように、このメソッドを使用して、名前とそのスパン(位置)を一緒に印刷できます。
for(Span s: nameSpans)
System.out.println(s.toString()+" "+tokens[s.getStart()]);
以下は、指定された生のテキストから名前を検出し、それらをその位置とともに表示するプログラムです。 このプログラムを NameFinderSentences.java という名前のファイルに保存します。
import java.io.FileInputStream;
import java.io.InputStream;
import opennlp.tools.namefind.NameFinderME;
import opennlp.tools.namefind.TokenNameFinderModel;
import opennlp.tools.tokenize.TokenizerME;
import opennlp.tools.tokenize.TokenizerModel;
import opennlp.tools.util.Span;
public class NameFinderSentences {
public static void main(String args[]) throws Exception{
//Loading the tokenizer model
InputStream inputStreamTokenizer = new
FileInputStream("C:/OpenNLP_models/entoken.bin");
TokenizerModel tokenModel = new TokenizerModel(inputStreamTokenizer);
//Instantiating the TokenizerME class
TokenizerME tokenizer = new TokenizerME(tokenModel);
//Tokenizing the sentence in to a string array
String sentence = "Mike is senior programming
manager and Rama is a clerk both are working at
finddevguides";
String tokens[] = tokenizer.tokenize(sentence);
//Loading the NER-person model
InputStream inputStreamNameFinder = new
FileInputStream("C:/OpenNLP_models/enner-person.bin");
TokenNameFinderModel model = new TokenNameFinderModel(inputStreamNameFinder);
//Instantiating the NameFinderME class
NameFinderME nameFinder = new NameFinderME(model);
//Finding the names in the sentence
Span nameSpans[] = nameFinder.find(tokens);
//Printing the names and their spans in a sentence
for(Span s: nameSpans)
System.out.println(s.toString()+" "+tokens[s.getStart()]);
}
}
次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します-
javac NameFinderSentences.java
java NameFinderSentences
実行時に、上記のプログラムは指定された文字列(生テキスト)を読み取り、その中の人物の名前を検出し、以下に示すようにその位置(スパン)を表示します。
[0..1) person Mike
場所の名前を見つける
さまざまなモデルをロードすることにより、さまざまな名前のエンティティを検出できます。 以下は、 en-ner-location.bin モデルをロードし、指定された文の場所名を検出するJavaプログラムです。 このプログラムを LocationFinder.java という名前のファイルに保存します。
import java.io.FileInputStream;
import java.io.InputStream;
import opennlp.tools.namefind.NameFinderME;
import opennlp.tools.namefind.TokenNameFinderModel;
import opennlp.tools.tokenize.TokenizerME;
import opennlp.tools.tokenize.TokenizerModel;
import opennlp.tools.util.Span;
public class LocationFinder {
public static void main(String args[]) throws Exception{
InputStream inputStreamTokenizer = new
FileInputStream("C:/OpenNLP_models/entoken.bin");
TokenizerModel tokenModel = new TokenizerModel(inputStreamTokenizer);
//String paragraph = "Mike and Smith are classmates";
String paragraph = "finddevguides is located in Hyderabad";
//Instantiating the TokenizerME class
TokenizerME tokenizer = new TokenizerME(tokenModel);
String tokens[] = tokenizer.tokenize(paragraph);
//Loading the NER-location moodel
InputStream inputStreamNameFinder = new
FileInputStream("C:/OpenNLP_models/en- ner-location.bin");
TokenNameFinderModel model = new TokenNameFinderModel(inputStreamNameFinder);
//Instantiating the NameFinderME class
NameFinderME nameFinder = new NameFinderME(model);
//Finding the names of a location
Span nameSpans[] = nameFinder.find(tokens);
//Printing the spans of the locations in the sentence
for(Span s: nameSpans)
System.out.println(s.toString()+" "+tokens[s.getStart()]);
}
}
次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します-
javac LocationFinder.java
java LocationFinder
上記のプログラムは、実行時に、指定された文字列(生テキスト)を読み取り、その中の人物の名前を検出し、以下に示すようにその位置(スパン)を表示します。
[4..5) location Hyderabad
NameFinderの確率
*NameFinderME* クラスの** probs()**メソッドは、最後にデコードされたシーケンスの確率を取得するために使用されます。
double[] probs = nameFinder.probs();
以下は、確率を出力するプログラムです。 このプログラムを TokenizerMEProbs.java という名前のファイルに保存します。
import java.io.FileInputStream;
import java.io.InputStream;
import opennlp.tools.tokenize.TokenizerME;
import opennlp.tools.tokenize.TokenizerModel;
import opennlp.tools.util.Span;
public class TokenizerMEProbs {
public static void main(String args[]) throws Exception{
String sent = "Hello John how are you welcome to finddevguides";
//Loading the Tokenizer model
InputStream inputStream = new
FileInputStream("C:/OpenNLP_models/en-token.bin");
TokenizerModel tokenModel = new TokenizerModel(inputStream);
//Instantiating the TokenizerME class
TokenizerME tokenizer = new TokenizerME(tokenModel);
//Retrieving the positions of the tokens
Span tokens[] = tokenizer.tokenizePos(sent);
//Getting the probabilities of the recent calls to tokenizePos() method
double[] probs = tokenizer.getTokenProbabilities();
//Printing the spans of tokens
for( Span token : tokens)
System.out.println(token +"
"+sent.substring(token.getStart(), token.getEnd()));
System.out.println(" ");
for(int i = 0; i<probs.length; i++)
System.out.println(probs[i]);
}
}
次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します-
javac TokenizerMEProbs.java
java TokenizerMEProbs
実行時に、上記のプログラムは指定された文字列を読み取り、文をトークン化し、それらを出力します。 さらに、以下に示すように、最後にデコードされたシーケンスの確率も返します。
[0..5) Hello
[6..10) John
[11..14) how
[15..18) are
[19..22) you
[23..30) welcome
[31..33) to
[34..48) finddevguides
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0