home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (PosUimaTokenizer.java)

This example Java source code file (PosUimaTokenizer.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

a-z, analysisengine, arraylist, cas, collection, english, list, none, override, posuimatokenizer, runtimeexception, string, tokenpreprocess, util

The PosUimaTokenizer.java Java example source code

 *  * Copyright 2015 Skymind,Inc.
 *  *
 *  *    Licensed under the Apache License, Version 2.0 (the "License");
 *  *    you may not use this file except in compliance with the License.
 *  *    You may obtain a copy of the License at
 *  *
 *  *        http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  *    Unless required by applicable law or agreed to in writing, software
 *  *    distributed under the License is distributed on an "AS IS" BASIS,
 *  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  *    See the License for the specific language governing permissions and
 *  *    limitations under the License.

package org.deeplearning4j.text.tokenization.tokenizer;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import lombok.NonNull;
import org.apache.uima.analysis_engine.AnalysisEngine;
import org.apache.uima.cas.CAS;
import org.apache.uima.fit.factory.AnalysisEngineFactory;
import org.apache.uima.fit.util.JCasUtil;
import org.cleartk.token.type.Sentence;
import org.cleartk.token.type.Token;
import org.deeplearning4j.text.annotator.PoStagger;
import org.deeplearning4j.text.annotator.SentenceAnnotator;
import org.deeplearning4j.text.annotator.StemmerAnnotator;
import org.deeplearning4j.text.annotator.TokenizerAnnotator;

 * Filter by part of speech tag.
 * Any not valid part of speech tags
 * become NONE
 * @author Adam Gibson
public class PosUimaTokenizer  implements Tokenizer {

    private static AnalysisEngine engine;
    private List<String> tokens;
    private Collection<String> allowedPosTags;
    private int index;
    private static CAS cas;
    private TokenPreProcess preProcessor;
    private boolean stripNones = false;

    public PosUimaTokenizer(String tokens,AnalysisEngine engine,Collection<String> allowedPosTags) {
        this(tokens, engine, allowedPosTags, false);

    public PosUimaTokenizer(String tokens,AnalysisEngine engine,Collection<String> allowedPosTags, boolean stripNones) {
        if(PosUimaTokenizer.engine == null)
            PosUimaTokenizer.engine = engine;
        this.allowedPosTags = allowedPosTags;
        this.tokens = new ArrayList<String>();
        this.stripNones = stripNones;
        try {
            if(cas == null)
                cas = engine.newCAS();

            for(Sentence s : JCasUtil.select(cas.getJCas(), Sentence.class)) {
                for(Token t : JCasUtil.selectCovered(Token.class,s)) {
                    //add NONE for each invalid token
                        if(t.getLemma() != null)
                        else if(t.getStem() != null)

        } catch (Exception e) {
            throw new RuntimeException(e);


    private boolean valid(Token token) {
        String check = token.getCoveredText();
        if(check.matches("<[A-Z]+>") || check.matches(""))
            return false;
        else if(token.getPos() != null && !this.allowedPosTags.contains(token.getPos()))
            return false;
        return true;

    public boolean hasMoreTokens() {
        return index < tokens.size();

    public int countTokens() {
        return tokens.size();

    public String nextToken() {
        String ret = tokens.get(index); // preProcessor != null ? preProcessor.preProcess(tokens.get(index)) : tokens.get(index);
        return ret;

    public List<String> getTokens() {
        List<String> tokens = new ArrayList();
        while(hasMoreTokens()) {
            String nextT = nextToken();
            if (stripNones && nextT.equals("NONE"))
            tokens.add(preProcessor != null ? preProcessor.preProcess(nextT) : nextT);
        return tokens;

    public static AnalysisEngine defaultAnalysisEngine()  {
        try {
            return AnalysisEngineFactory.createEngine(AnalysisEngineFactory.createEngineDescription(SentenceAnnotator.getDescription(), TokenizerAnnotator.getDescription(), PoStagger.getDescription("en"), StemmerAnnotator.getDescription("English")));
        }catch(Exception e) {
            throw new RuntimeException(e);

	public void setTokenPreProcessor(@NonNull TokenPreProcess tokenPreProcessor) {
		this.preProcessor = tokenPreProcessor;


Other Java examples (source code examples)

Here is a short list of links related to this Java PosUimaTokenizer.java source code file:

my book on functional programming


new blog posts


Copyright 1998-2019 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.