NLP4J [1006-034] 100 Sprachverarbeitungsklopfen mit NLP4J # 34 "A B" codieren den Prozess direkt, um "A B" zu extrahieren. Ich schrieb in. Der folgende Teil.
//Finden Sie "A bis B"
String meishi_a = null;
String no = null;
for (Keyword kwd : kwds) {
if (meishi_a == null && kwd.getFacet().equals("Substantiv")) {
meishi_a = kwd.getLex();
} //
else if (meishi_a != null && no == null && kwd.getLex().equals("von")) {
no = kwd.getLex();
} //
else if (meishi_a != null && no != null && kwd.getFacet().equals("Substantiv")) {
System.err.println(meishi_a + no + kwd.getLex());
meishi_a = null;
no = null;
} //
else {
meishi_a = null;
no = null;
}
}
Logik kann mit dieser Art der Schlüsselwortextraktion (Annotation) nicht wiederverwendet werden.
Annotator
Daher bietet NLP4J Annotator, einen Mechanismus zum unabhängigen Hinzufügen von Annotation. Der Mechanismus ist einfach: Implementieren Sie die Schnittstelle nlp4j.DocumentAnnotator.
Wenn Sie die obige Logik als Annotator-Code vorbereiten, ist dies wie folgt. "B of A" wird einfach als Zeichenfolge ausgegeben und als neues Schlüsselwort hinzugefügt, nicht als Ende. Ein Bezeichner (= facet: facet) mit dem Namen "word_nn_no_nn" wird festgelegt, damit der Typ des Schlüsselworts identifiziert werden kann.
package nlp4j.annotator;
import java.util.ArrayList;
import nlp4j.AbstractDocumentAnnotator;
import nlp4j.Document;
import nlp4j.DocumentAnnotator;
import nlp4j.Keyword;
import nlp4j.impl.DefaultKeyword;
/**
*"Nomenklatur" wird in "Wort" geändert_nn_no_Als Schlüsselwort "nn" extrahieren.
* @author Hiroki Oya
*/
public class Nokku34Annotator extends AbstractDocumentAnnotator implements DocumentAnnotator {
@Override
public void annotate(Document doc) throws Exception {
ArrayList<Keyword> newkwds = new ArrayList<>();
Keyword meishi_a = null;
Keyword no = null;
for (Keyword kwd : doc.getKeywords()) {
if (meishi_a == null && kwd.getFacet().equals("Substantiv")) {
meishi_a = kwd;
} //
else if (meishi_a != null && no == null && kwd.getLex().equals("von")) {
no = kwd;
} //
else if (meishi_a != null && no != null && kwd.getFacet().equals("Substantiv")) {
Keyword kw = new DefaultKeyword();
kwd.setLex(meishi_a.getLex() + no.getLex() + kwd.getLex());
kwd.setFacet("word_nn_no_nn");
kwd.setBegin(meishi_a.getBegin());
kwd.setEnd(kwd.getEnd());
kwd.setStr(meishi_a.getStr() + no.getStr() + kwd.getStr());
kwd.setReading(meishi_a.getReading() + no.getReading() + kwd.getReading());
newkwds.add(kw);
meishi_a = null;
no = null;
} //
else {
meishi_a = null;
no = null;
}
}
doc.addKeywords(newkwds);
}
}
Jetzt haben wir die Logik zum Extrahieren des Schlüsselworts "A bis B" getrennt und definiert.
package nlp4j.nokku.chap4;
import java.util.List;
import nlp4j.Document;
import nlp4j.DocumentAnnotator;
import nlp4j.DocumentAnnotatorPipeline;
import nlp4j.Keyword;
import nlp4j.crawler.Crawler;
import nlp4j.crawler.TextFileLineSeparatedCrawler;
import nlp4j.impl.DefaultDocumentAnnotatorPipeline;
import nlp4j.annotator.Nokku34Annotator;
public class Nokku34b {
public static void main(String[] args) throws Exception {
//Verwenden Sie den von NLP4J bereitgestellten Textdatei-Crawler
Crawler crawler = new TextFileLineSeparatedCrawler();
crawler.setProperty("file", "src/test/resources/nlp4j.crawler/neko_short_utf8.txt");
crawler.setProperty("encoding", "UTF-8");
crawler.setProperty("target", "text");
//Dokumentcrawl
List<Document> docs = crawler.crawlDocuments();
//Definition der NLP-Pipeline (Prozess durch Verbinden mehrerer Prozesse als Pipeline)
DocumentAnnotatorPipeline pipeline = new DefaultDocumentAnnotatorPipeline();
{
// Yahoo!Annotator mit Japans API für morphologische Analyse
DocumentAnnotator annotator = new YJpMaAnnotator();
pipeline.add(annotator);
}
{
//"Nomenklatur" wird in "Wort" geändert_nn_no_Als Schlüsselwort "nn" extrahieren.
Nokku34Annotator annotator = new Nokku34Annotator(); //← Ausgabe 34 ist nur hier
pipeline.add(annotator); //← Ausgabe 34 ist nur hier
}
//Ausführung der Annotationsverarbeitung
pipeline.annotate(docs);
for (Document doc : docs) {
for (Keyword kwd : doc.getKeywords("word_nn_no_nn")) {
System.err.println(kwd.getStr());
}
}
}
}
Der Prozess des Extrahierens von "B von A" besteht jetzt nur noch aus zwei Zeilen!
//"Nomenklatur" wird in "Wort" geändert_nn_no_Als Schlüsselwort "nn" extrahieren.
Nokku34Annotator annotator = new Nokku34Annotator();
pipeline.add(annotator);
Auf diese Weise können Sie viele Ihrer eigenen Annotatoren definieren, um Ihre Verarbeitung natürlicher Sprache weiter zu erweitern.
Seine Handfläche
Auf der Handfläche
Studentengesicht
Sollte Gesicht
Mitten im Gesicht
In dem Loch
Mit NLP4J können Sie ganz einfach die Verarbeitung natürlicher Sprache in Java durchführen!
https://www.nlp4j.org/
Recommended Posts