Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8870305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:41:25+00:00 2026-06-14T17:41:25+00:00

I am trying tokenize strings into ngrams. Strangely in the documentation for the NGramTokenizer

  • 0

I am trying tokenize strings into ngrams. Strangely in the documentation for the NGramTokenizer I do not see a method that will return the individual ngrams that were tokenized. In fact I only see two methods in the NGramTokenizer class that return String Objects.

Here is the code that I have:

Reader reader = new StringReader("This is a test string");
NGramTokenizer gramTokenizer = new NGramTokenizer(reader, 1, 3);
  1. Where are the ngrams that were tokenized?
  2. How can I get the output in Strings/Words?

I want my output to be like: This, is, a, test, string, This is, is a, a test, test string, This is a, is a test, a test string.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:41:27+00:00Added an answer on June 14, 2026 at 5:41 pm

    I don’t think you’ll find what you’re looking for trying to find methods returning String. You’ll need to deal with Attributes.

    Should work something like:

    Reader reader = new StringReader("This is a test string");
    NGramTokenizer gramTokenizer = new NGramTokenizer(reader, 1, 3);
    CharTermAttribute charTermAttribute = gramTokenizer.addAttribute(CharTermAttribute.class);
    gramTokenizer.reset();
    
    while (gramTokenizer.incrementToken()) {
        String token = charTermAttribute.toString();
        //Do something
    }
    gramTokenizer.end();
    gramTokenizer.close();
    

    Be sure to reset() the Tokenizer it if it needs to be reused after that, though.


    Tokenizing grouping of words, rather than chars, per comments:

    Reader reader = new StringReader("This is a test string");
    TokenStream tokenizer = new StandardTokenizer(Version.LUCENE_36, reader);
    tokenizer = new ShingleFilter(tokenizer, 1, 3);
    CharTermAttribute charTermAttribute = tokenizer.addAttribute(CharTermAttribute.class);
    
    while (tokenizer.incrementToken()) {
        String token = charTermAttribute.toString();
        //Do something
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I'm trying to tokenize a string by loading an entire file into a
I am trying to tokenize my data that I get from my text file.
We are trying to tokenize a line of input from a file into an
trying to take a line of text and tokenize it into a filename and
What i'm trying to do is create a template array class that will store
I am trying to write a program that will take a text file with
I'm trying to create a regex to tokenize a string. An example of the
Trying to build out an exception if move.UserId does not equal currentUserId then Redirect
Trying to make this jQuery filter that uses .find case-insensitive. For example, when the
I am trying to tokenize a string but I need to know exactly when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.