OpenNLP - Chunking Sentences
Suddividere una frase si riferisce alla rottura / divisione di una frase in parti di parole come gruppi di parole e gruppi di verbi.
Suddivisione di una frase utilizzando OpenNLP
Per rilevare le frasi, OpenNLP utilizza un modello, un file denominato en-chunker.bin. Questo è un modello predefinito che è addestrato a dividere le frasi nel testo grezzo dato.
Il opennlp.tools.chunker Il pacchetto contiene le classi e le interfacce che vengono utilizzate per trovare annotazioni sintattiche non ricorsive come blocchi di frasi nominali.
Puoi dividere una frase usando il metodo chunk() del ChunkerMEclasse. Questo metodo accetta i token di una frase e tag POS come parametri. Pertanto, prima di iniziare il processo di chunking, prima di tutto è necessario tokenizzare la frase e generare i tag POS delle parti di essa.
Per suddividere una frase utilizzando la libreria OpenNLP, è necessario:
Tokenizza la frase.
Genera tag POS per questo.
Carica il file en-chunker.bin modello utilizzando il ChunkerModel classe
Istanziare il file ChunkerME classe.
Suddividi le frasi usando il chunk() metodo di questa classe.
Di seguito sono riportati i passaggi da seguire per scrivere un programma in blocchi di frasi dal testo grezzo specificato.
Passaggio 1: tokenizzazione della frase
Tokenizza le frasi usando il tokenize() metodo del whitespaceTokenizer class, come mostrato nel seguente blocco di codice.
//Tokenizing the sentence
String sentence = "Hi welcome to Tutorialspoint";
WhitespaceTokenizer whitespaceTokenizer= WhitespaceTokenizer.INSTANCE;
String[] tokens = whitespaceTokenizer.tokenize(sentence);
Passaggio 2: generazione dei tag POS
Genera i tag POS della frase utilizzando il tag() metodo del POSTaggerME class, come mostrato nel seguente blocco di codice.
//Generating the POS tags
File file = new File("C:/OpenNLP_models/en-pos-maxent.bin");
POSModel model = new POSModelLoader().load(file);
//Constructing the tagger
POSTaggerME tagger = new POSTaggerME(model);
//Generating tags from the tokens
String[] tags = tagger.tag(tokens);
Passaggio 3: caricamento del modello
Il modello per suddividere una frase è rappresentato dalla classe denominata ChunkerModel, che appartiene al pacchetto opennlp.tools.chunker.
Per caricare un modello di rilevamento della frase:
Creare un InputStream oggetto del modello (creare un'istanza di FileInputStream e passare il percorso del modello in formato String al suo costruttore).
Istanziare il file ChunkerModel class e passare il InputStream (oggetto) del modello come parametro del suo costruttore, come mostrato nel seguente blocco di codice:
//Loading the chunker model
InputStream inputStream = new FileInputStream("C:/OpenNLP_models/en-chunker.bin");
ChunkerModel chunkerModel = new ChunkerModel(inputStream);
Passaggio 4: creazione di un'istanza della classe chunkerME
Il chunkerME classe del pacchetto opennlp.tools.chunkercontiene metodi per suddividere le frasi. Questo è un chunker basato sulla massima entropia.
Crea un'istanza di questa classe e passa l'oggetto modello creato nel passaggio precedente.
//Instantiate the ChunkerME class
ChunkerME chunkerME = new ChunkerME(chunkerModel);
Passaggio 5: suddivisione della frase
Il chunk() metodo del ChunkerMEclass viene utilizzata per suddividere le frasi nel testo grezzo che gli è stato passato. Questo metodo accetta due array di stringhe che rappresentano token e tag, come parametri.
Richiamare questo metodo passando come parametri l'array di token e l'array di tag creati nei passaggi precedenti.
//Generating the chunks
String result[] = chunkerME.chunk(tokens, tags);
Example
Di seguito è riportato il programma per suddividere le frasi nel testo grezzo fornito. Salva questo programma in un file con il nomeChunkerExample.java.
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import opennlp.tools.chunker.ChunkerME;
import opennlp.tools.chunker.ChunkerModel;
import opennlp.tools.cmdline.postag.POSModelLoader;
import opennlp.tools.postag.POSModel;
import opennlp.tools.postag.POSTaggerME;
import opennlp.tools.tokenize.WhitespaceTokenizer;
public class ChunkerExample{
public static void main(String args[]) throws IOException {
//Tokenizing the sentence
String sentence = "Hi welcome to Tutorialspoint";
WhitespaceTokenizer whitespaceTokenizer= WhitespaceTokenizer.INSTANCE;
String[] tokens = whitespaceTokenizer.tokenize(sentence);
//Generating the POS tags
//Load the parts of speech model
File file = new File("C:/OpenNLP_models/en-pos-maxent.bin");
POSModel model = new POSModelLoader().load(file);
//Constructing the tagger
POSTaggerME tagger = new POSTaggerME(model);
//Generating tags from the tokens
String[] tags = tagger.tag(tokens);
//Loading the chunker model
InputStream inputStream = new
FileInputStream("C:/OpenNLP_models/en-chunker.bin");
ChunkerModel chunkerModel = new ChunkerModel(inputStream);
//Instantiate the ChunkerME class
ChunkerME chunkerME = new ChunkerME(chunkerModel);
//Generating the chunks
String result[] = chunkerME.chunk(tokens, tags);
for (String s : result)
System.out.println(s);
}
}
Compilare ed eseguire il file Java salvato dal prompt dei comandi utilizzando il seguente comando:
javac ChunkerExample.java
java ChunkerExample
All'esecuzione, il programma precedente legge la stringa data e suddivide le frasi al suo interno e le visualizza come mostrato di seguito.
Loading POS Tagger model ... done (1.040s)
B-NP
I-NP
B-VP
I-VP
Rilevamento delle posizioni dei token
Possiamo anche rilevare le posizioni o gli intervalli dei blocchi utilizzando chunkAsSpans() metodo del ChunkerMEclasse. Questo metodo restituisce una matrice di oggetti del tipo Span. La classe denominata Span diopennlp.tools.util pacchetto viene utilizzato per memorizzare il file start e end numero intero di insiemi.
È possibile memorizzare le campate restituite da chunkAsSpans() nell'array Span e stamparli, come mostrato nel seguente blocco di codice.
//Generating the tagged chunk spans
Span[] span = chunkerME.chunkAsSpans(tokens, tags);
for (Span s : span)
System.out.println(s.toString());
Example
Di seguito è riportato il programma che rileva le frasi nel testo grezzo fornito. Salva questo programma in un file con il nomeChunkerSpansEample.java.
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import opennlp.tools.chunker.ChunkerME;
import opennlp.tools.chunker.ChunkerModel;
import opennlp.tools.cmdline.postag.POSModelLoader;
import opennlp.tools.postag.POSModel;
import opennlp.tools.postag.POSTaggerME;
import opennlp.tools.tokenize.WhitespaceTokenizer;
import opennlp.tools.util.Span;
public class ChunkerSpansEample{
public static void main(String args[]) throws IOException {
//Load the parts of speech model
File file = new File("C:/OpenNLP_models/en-pos-maxent.bin");
POSModel model = new POSModelLoader().load(file);
//Constructing the tagger
POSTaggerME tagger = new POSTaggerME(model);
//Tokenizing the sentence
String sentence = "Hi welcome to Tutorialspoint";
WhitespaceTokenizer whitespaceTokenizer= WhitespaceTokenizer.INSTANCE;
String[] tokens = whitespaceTokenizer.tokenize(sentence);
//Generating tags from the tokens
String[] tags = tagger.tag(tokens);
//Loading the chunker model
InputStream inputStream = new
FileInputStream("C:/OpenNLP_models/en-chunker.bin");
ChunkerModel chunkerModel = new ChunkerModel(inputStream);
ChunkerME chunkerME = new ChunkerME(chunkerModel);
//Generating the tagged chunk spans
Span[] span = chunkerME.chunkAsSpans(tokens, tags);
for (Span s : span)
System.out.println(s.toString());
}
}
Compilare ed eseguire il file Java salvato dal prompt dei comandi utilizzando i seguenti comandi:
javac ChunkerSpansEample.java
java ChunkerSpansEample
All'esecuzione, il programma precedente legge la stringa data e gli intervalli dei blocchi in essa contenuti e visualizza il seguente output:
Loading POS Tagger model ... done (1.059s)
[0..2) NP
[2..4) VP
Chunker Probability Detection
Il probs() metodo del ChunkerME class restituisce le probabilità dell'ultima sequenza decodificata.
//Getting the probabilities of the last decoded sequence
double[] probs = chunkerME.probs();
Di seguito è riportato il programma per stampare le probabilità dell'ultima sequenza decodificata dal chunker. Salva questo programma in un file con il nomeChunkerProbsExample.java.
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import opennlp.tools.chunker.ChunkerME;
import opennlp.tools.chunker.ChunkerModel;
import opennlp.tools.cmdline.postag.POSModelLoader;
import opennlp.tools.postag.POSModel;
import opennlp.tools.postag.POSTaggerME;
import opennlp.tools.tokenize.WhitespaceTokenizer;
public class ChunkerProbsExample{
public static void main(String args[]) throws IOException {
//Load the parts of speech model
File file = new File("C:/OpenNLP_models/en-pos-maxent.bin");
POSModel model = new POSModelLoader().load(file);
//Constructing the tagger
POSTaggerME tagger = new POSTaggerME(model);
//Tokenizing the sentence
String sentence = "Hi welcome to Tutorialspoint";
WhitespaceTokenizer whitespaceTokenizer= WhitespaceTokenizer.INSTANCE;
String[] tokens = whitespaceTokenizer.tokenize(sentence);
//Generating tags from the tokens
String[] tags = tagger.tag(tokens);
//Loading the chunker model
InputStream inputStream = new
FileInputStream("C:/OpenNLP_models/en-chunker.bin");
ChunkerModel cModel = new ChunkerModel(inputStream);
ChunkerME chunkerME = new ChunkerME(cModel);
//Generating the chunk tags
chunkerME.chunk(tokens, tags);
//Getting the probabilities of the last decoded sequence
double[] probs = chunkerME.probs();
for(int i = 0; i<probs.length; i++)
System.out.println(probs[i]);
}
}
Compilare ed eseguire il file Java salvato dal prompt dei comandi utilizzando i seguenti comandi:
javac ChunkerProbsExample.java
java ChunkerProbsExample
In esecuzione, il programma precedente legge la stringa data, la suddivide in blocchi e stampa le probabilità dell'ultima sequenza decodificata.
0.9592746040797778
0.6883933131241501
0.8830563473996004
0.8951150529746051