本文整理汇总了Java中opennlp.tools.postag.POSDictionary类的典型用法代码示例。如果您正苦于以下问题:Java POSDictionary类的具体用法?Java POSDictionary怎么用?Java POSDictionary使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
POSDictionary类属于opennlp.tools.postag包,在下文中一共展示了POSDictionary类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: convertLemmaToPOSDict
import opennlp.tools.postag.POSDictionary; //导入依赖的package包/类
/**
* Convert a lemma dictionary (word lemma postag) into a
* {@code POSTaggerDictionary}. It saves the resulting file with the name of
* the original dictionary changing the extension to .xml.
*
* @param lemmaDict
* the input file
* @throws IOException
* if io problems
*/
public static void convertLemmaToPOSDict(Path lemmaDict) throws IOException {
// process one file
if (Files.isRegularFile(lemmaDict)) {
List<String> inputLines = Files.readAllLines(lemmaDict,
StandardCharsets.UTF_8);
Path outFile = Files.createFile(Paths.get(lemmaDict.toString() + ".xml"));
POSDictionary posTagDict = getPOSTaggerDict(inputLines);
OutputStream outputStream = Files.newOutputStream(outFile);
posTagDict.serialize(outputStream);
outputStream.close();
System.err.println(
">> Serialized Apache OpenNLP POSDictionary format to " + outFile);
} else {
System.out.println("Please choose a valid file as input.");
System.exit(1);
}
}
示例2: getPOSTaggerDict
import opennlp.tools.postag.POSDictionary; //导入依赖的package包/类
/**
* Generates {@code POSDictionary} from a list of monosemic words and its
* postag. form\tab\lemma\tabpostag
*
* @param inputLines
* the list of words and postag per line
* @return the POSDictionary
*/
private static POSDictionary getPOSTaggerDict(List<String> inputLines) {
POSDictionary posTaggerDict = new POSDictionary();
ListMultimap<String, String> dictMultiMap = ArrayListMultimap.create();
for (String line : inputLines) {
String[] lineArray = line.split("\t");
if (lineArray.length == 3) {
if (!lineArray[0].contains("<")) {
dictMultiMap.put(lineArray[0], lineArray[2]);
}
}
}
for (String token : dictMultiMap.keySet()) {
List<String> tags = dictMultiMap.get(token);
// add only monosemic words
if (tags.size() == 1) {
posTaggerDict.put(token, tags.toArray(new String[tags.size()]));
}
}
return posTaggerDict;
}
示例3: addLemmaToPOSDict
import opennlp.tools.postag.POSDictionary; //导入依赖的package包/类
/**
* Aggregates a lemma dictionary (word lemma postag) into a
* {@code POSTaggerDictionary}. It saves the resulting file with the name of
* the original lemma dictionary changing the extension to .xml.
*
* @param lemmaDict
* the input file
* @throws IOException
* if io problems
*/
public static void addLemmaToPOSDict(Path lemmaDict, Path posTaggerDict)
throws IOException {
// process one file
if (Files.isRegularFile(lemmaDict) && Files.isRegularFile(posTaggerDict)) {
InputStream posDictInputStream = Files.newInputStream(posTaggerDict);
POSDictionary posDict = POSDictionary.create(posDictInputStream);
List<String> inputLines = Files.readAllLines(lemmaDict);
Path outFile = Paths.get(lemmaDict.toString() + ".xml");
addPOSTaggerDict(inputLines, posDict);
OutputStream outputStream = Files.newOutputStream(outFile);
posDict.serialize(outputStream);
outputStream.close();
System.err.println(
">> Serialized Apache OpenNLP POSDictionary format to " + outFile);
} else {
System.out.println("Please choose a valid files as input.");
System.exit(1);
}
}
示例4: addPOSTaggerDict
import opennlp.tools.postag.POSDictionary; //导入依赖的package包/类
/**
* Aggregates {@code POSDictionary} from a list of words and its postag.
*
* @param inputLines
* the list of words and postag per line
* @param tagDict
* the POSDictionary to which the lemma dictionary will be added
*/
private static void addPOSTaggerDict(List<String> inputLines,
POSDictionary tagDict) {
ListMultimap<String, String> dictMultiMap = ArrayListMultimap.create();
for (String line : inputLines) {
String[] lineArray = line.split(" ");
if (lineArray.length == 2) {
dictMultiMap.put(lineArray[0], lineArray[1]);
}
}
for (String token : dictMultiMap.keySet()) {
List<String> tags = dictMultiMap.get(token);
if (tags.size() == 1) {
tagDict.put(token, tags.toArray(new String[tags.size()]));
}
}
}
示例5: createPosTagger
import opennlp.tools.postag.POSDictionary; //导入依赖的package包/类
/**
* Creates the part of speech tagger from a model file and a case sensitive
* tag dictionary.
*
* @param model model file
* @param tagdict case sensitive tag dictionary
* @return true, iff the POS tagger was created successfully
*/
public static boolean createPosTagger(String model, String tagdict) {
try {
// create POS tagger, use case sensitive tag dictionary
tagger = new PosTagger(model, new POSDictionary(tagdict, true));
} catch (IOException e) {
return false;
}
return true;
}
示例6: trainPOS
import opennlp.tools.postag.POSDictionary; //导入依赖的package包/类
public static void trainPOS(final String inResource, String outFile) throws IOException {
InputStreamFactory inputStreamFactory = new InputStreamFactory() {
@Override
public InputStream createInputStream() throws IOException {
return Trainer.class.getResourceAsStream(inResource);
}
};
WordTagSampleStream samples = new WordTagSampleStream(new PlainTextByLineStream(inputStreamFactory, StandardCharsets.UTF_8));
TrainingParameters trainingParameters = new TrainingParameters();
trainingParameters.put(TrainingParameters.ALGORITHM_PARAM, ModelType.MAXENT.name());
trainingParameters.put(TrainingParameters.ITERATIONS_PARAM, "100");
trainingParameters.put(TrainingParameters.CUTOFF_PARAM, "5");
Dictionary ngramDictionary = null;
POSDictionary posDictionary = null;
POSTaggerFactory posTaggerFactory = POSTaggerFactory.create(null, ngramDictionary, posDictionary);
POSModel model = POSTaggerME.train("en", samples, trainingParameters, posTaggerFactory);
//POSTaggerME.train("en", samples, ModelType.MAXENT, null, null, 5, 100);
samples.close();
FileOutputStream out = new FileOutputStream(outFile);
model.serialize(out);
out.close();
}