本文整理汇总了Java中edu.stanford.nlp.util.logging.Redwood类的典型用法代码示例。如果您正苦于以下问题:Java Redwood类的具体用法?Java Redwood怎么用?Java Redwood使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Redwood类属于edu.stanford.nlp.util.logging包,在下文中一共展示了Redwood类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: trainModel
import edu.stanford.nlp.util.logging.Redwood; //导入依赖的package包/类
public static void trainModel() throws IOException {
forceTrack("Training data");
List<Pair<KBPInput, String>> trainExamples = DatasetUtils.readDataset(TRAIN_FILE);
log.info("Read " + trainExamples.size() + " examples");
log.info("" + trainExamples.stream().map(Pair::second).filter(NO_RELATION::equals).count() + " are " + NO_RELATION);
endTrack("Training data");
// Featurize + create the dataset
forceTrack("Creating dataset");
RVFDataset<String, String> dataset = new RVFDataset<>();
final AtomicInteger i = new AtomicInteger(0);
long beginTime = System.currentTimeMillis();
trainExamples.stream().parallel().forEach(example -> {
if (i.incrementAndGet() % 1000 == 0) {
log.info("[" + Redwood.formatTimeDifference(System.currentTimeMillis() - beginTime) +
"] Featurized " + i.get() + " / " + trainExamples.size() + " examples");
}
Counter<String> features = features(example.first); // This takes a while per example
synchronized (dataset) {
dataset.add(new RVFDatum<>(features, example.second));
}
});
trainExamples.clear(); // Free up some memory
endTrack("Creating dataset");
// Train the classifier
log.info("Training classifier:");
Classifier<String, String> classifier = trainMultinomialClassifier(dataset, FEATURE_THRESHOLD, SIGMA);
dataset.clear(); // Free up some memory
// Save the classifier
IOUtils.writeObjectToFile(new IntelKBPStatisticalExtractor(classifier), MODEL_FILE);
}
示例2: prettyLog
import edu.stanford.nlp.util.logging.Redwood; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
@SuppressWarnings("unchecked")
public void prettyLog(RedwoodChannels channels, String description) {
Redwood.startTrack(description);
// sort keys by class name
List<Class> sortedKeys = new ArrayList<Class>(this.keySet());
Collections.sort(sortedKeys,
new Comparator<Class>(){
@Override
public int compare(Class a, Class b) {
return a.getCanonicalName().compareTo(b.getCanonicalName());
}
});
// log key/value pairs
for (Class key : sortedKeys) {
String keyName = key.getCanonicalName().replace("class ", "");
Object value = this.get(key);
if (PrettyLogger.dispatchable(value)) {
PrettyLogger.log(channels, keyName, value);
} else {
channels.logf("%s = %s", keyName, value);
}
}
Redwood.endTrack(description);
}
示例3: annotate
import edu.stanford.nlp.util.logging.Redwood; //导入依赖的package包/类
public void annotate(Annotation annotation) {
if (verbose) {
timer.start();
Redwood.log(Redwood.DBG, "Adding TokensRegexAnnotator annotation...");
}
if (options.setTokenOffsets) {
addTokenOffsets(annotation);
}
List<CoreMap> allMatched;
if (annotation.containsKey(CoreAnnotations.SentencesAnnotation.class)) {
allMatched = new ArrayList<CoreMap>();
List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);
for (CoreMap sentence : sentences) {
List<CoreMap> matched = extract(sentence);
if (matched != null && options.matchedExpressionsAnnotationKey != null) {
allMatched.addAll(matched);
sentence.set(options.matchedExpressionsAnnotationKey, matched);
for (CoreMap cm:matched) {
cm.set(CoreAnnotations.SentenceIndexAnnotation.class, sentence.get(CoreAnnotations.SentenceIndexAnnotation.class));
}
}
}
} else {
allMatched = extract(annotation);
}
if (options.matchedExpressionsAnnotationKey != null) {
annotation.set(options.matchedExpressionsAnnotationKey, allMatched);
}
if (verbose)
timer.stop("done.");
}
示例4: annotate
import edu.stanford.nlp.util.logging.Redwood; //导入依赖的package包/类
/**
* Annotate a collection of input annotations IN PARALLEL, making use of threads given in numThreads
*
* @param annotations
* The input annotations to process
* @param numThreads
* The number of threads to run on
* @param callback
* A function to be called when an annotation finishes. The return value of the callback is ignored.
*/
public void annotate(final Iterable<Annotation> annotations, int numThreads, final Function<Annotation, Object> callback) {
// case: single thread (no point in spawning threads)
if (numThreads == 1) {
for (Annotation ann : annotations) {
annotate(ann);
callback.apply(ann);
}
}
// Java's equivalent to ".map{ lambda(annotation) => annotate(annotation) }
Iterable<Runnable> threads = new Iterable<Runnable>() {
@Override
public Iterator<Runnable> iterator() {
final Iterator<Annotation> iter = annotations.iterator();
return new Iterator<Runnable>() {
@Override
public boolean hasNext() {
return iter.hasNext();
}
@Override
public Runnable next() {
if (!iter.hasNext()) {
throw new NoSuchElementException();
}
final Annotation input = iter.next();
return new Runnable() {
@Override
public void run() {
// (logging)
String beginningOfDocument = input.toString().substring(0, Math.min(50, input.toString().length()));
Redwood.startTrack("Annotating \"" + beginningOfDocument + "...\"");
// (annotate)
annotate(input);
// (callback)
callback.apply(input);
// (logging again)
Redwood.endTrack("Annotating \"" + beginningOfDocument + "...\"");
}
};
}
@Override
public void remove() {
iter.remove();
}
};
}
};
// Thread
Redwood.Util.threadAndRun(this.getClass().getSimpleName(), threads, numThreads);
}
示例5: annotate
import edu.stanford.nlp.util.logging.Redwood; //导入依赖的package包/类
/**
* Annotate a collection of input annotations IN PARALLEL, making use of
* threads given in numThreads
* @param annotations The input annotations to process
* @param numThreads The number of threads to run on
* @param callback A function to be called when an annotation finishes.
* The return value of the callback is ignored.
*/
public void annotate(final Iterable<Annotation> annotations, int numThreads, final Function<Annotation,Object> callback){
// case: single thread (no point in spawning threads)
if(numThreads == 1){
for(Annotation ann : annotations){
annotate(ann);
callback.apply(ann);
}
}
// Java's equivalent to ".map{ lambda(annotation) => annotate(annotation) }
Iterable<Runnable> threads = new Iterable<Runnable>(){
public Iterator<Runnable> iterator() {
final Iterator<Annotation> iter = annotations.iterator();
return new Iterator<Runnable>(){
public boolean hasNext() {
return iter.hasNext();
}
public Runnable next() {
final Annotation input = iter.next();
return new Runnable(){
public void run(){
//Jesus Christ, finally the body of the code
//(logging)
String beginningOfDocument = input.toString().substring(0,Math.min(50,input.toString().length()));
Redwood.startTrack("Annotating \"" + beginningOfDocument + "...\"");
//(annotate)
annotate(input);
//(callback)
callback.apply(input);
//(logging again)
Redwood.endTrack("Annotating \"" + beginningOfDocument + "...\"");
}
};
}
public void remove() {
iter.remove();
}
};
}
};
// Thread
Redwood.Util.threadAndRun(this.getClass().getSimpleName(), threads, numThreads );
}
示例6: annotate
import edu.stanford.nlp.util.logging.Redwood; //导入依赖的package包/类
public void annotate(Annotation annotation) {
TimeIndex timeIndex = new TimeIndex();
String docDate = annotation.get(CoreAnnotations.DocDateAnnotation.class);
if(docDate == null){
Calendar cal = annotation.get(CoreAnnotations.CalendarAnnotation.class);
if(cal == null){
Redwood.log(Redwood.WARN, "No document date specified");
} else {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd:hh:mm:ss");
docDate = dateFormat.format(cal.getTime());
}
}
List<CoreMap> allTimeExpressions; // initialized below = null;
List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);
if (sentences != null) {
allTimeExpressions = new ArrayList<CoreMap>();
List<CoreMap> allNumerics = new ArrayList<CoreMap>();
for (CoreMap sentence: sentences) {
// make sure that token character offsets align with the actual sentence text
// They may not align due to token normalizations, such as "(" to "-LRB-".
CoreMap alignedSentence = NumberSequenceClassifier.alignSentence(sentence);
// uncomment the next line for verbose dumping of tokens....
// System.err.println("SENTENCE: " + ((ArrayCoreMap) sentence).toShorterString());
List<CoreMap> timeExpressionsForSentence =
timexExtractor.extractTimeExpressionCoreMaps(alignedSentence, docDate, timeIndex);
if (timeExpressionsForSentence != null) {
allTimeExpressions.addAll(timeExpressionsForSentence);
sentence.set(TimeAnnotations.TimexAnnotations.class, timeExpressionsForSentence);
for (CoreMap timeExpression:timeExpressionsForSentence) {
timeExpression.set(CoreAnnotations.SentenceIndexAnnotation.class, sentence.get(CoreAnnotations.SentenceIndexAnnotation.class));
}
}
List<CoreMap> numbers = alignedSentence.get(CoreAnnotations.NumerizedTokensAnnotation.class);
if(numbers != null){
sentence.set(CoreAnnotations.NumerizedTokensAnnotation.class, numbers);
allNumerics.addAll(numbers);
}
}
annotation.set(CoreAnnotations.NumerizedTokensAnnotation.class, allNumerics);
} else {
allTimeExpressions = annotateSingleSentence(annotation, docDate, timeIndex);
}
annotation.set(TimeAnnotations.TimexAnnotations.class, allTimeExpressions);
}