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 6685065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:00:50+00:00 2026-05-26T05:00:50+00:00

I am using Stanford’s NLP parser (http://nlp.stanford.edu/software/lex-parser.shtml) to split a block of text into

  • 0

I am using Stanford’s NLP parser (http://nlp.stanford.edu/software/lex-parser.shtml) to split a block of text into sentences and then see which sentences contain a given word.

Here is my code so far:

import java.io.FileReader;
import java.io.IOException;
import java.util.List;

import edu.stanford.nlp.ling.*;
import edu.stanford.nlp.process.*;

public class TokenizerDemo {

    public static void main(String[] args) throws IOException {
        DocumentPreprocessor dp = new DocumentPreprocessor(args[0]);
        for (List sentence : dp) {
            for (Object word : sentence) {
                System.out.println(word);
                System.out.println(word.getClass().getName());
                if (word.equals(args[1])) {
                    System.out.println("yes!\n");
                }
            }
        }
    }
}

I run the code from the command line using “java TokenizerDemo testfile.txt wall”

The contents of testfile.txt is:

Humpty Dumpty sat on a wall. Humpty Dumpty had a great fall.

So I want the program to detect “wall” in the first sentence (“wall” entered as the second argument on the command line). But the program doesn’t detect “wall”, because it never prints “yes!”. The output of the program is:

Humpty
edu.stanford.nlp.ling.Word
Dumpty
edu.stanford.nlp.ling.Word
sat
edu.stanford.nlp.ling.Word
on
edu.stanford.nlp.ling.Word
a
edu.stanford.nlp.ling.Word
wall
edu.stanford.nlp.ling.Word
.
edu.stanford.nlp.ling.Word
Humpty
edu.stanford.nlp.ling.Word
Dumpty
edu.stanford.nlp.ling.Word
had
edu.stanford.nlp.ling.Word
a
edu.stanford.nlp.ling.Word
great
edu.stanford.nlp.ling.Word
fall
edu.stanford.nlp.ling.Word
.
edu.stanford.nlp.ling.Word

DocumentPreprocessor from the Stanford parser correctly splits the text into two sentences. The problem appears to be with the use of the equals method. Each word has type “edu.stanford.nlp.ling.Word”. I’ve tried accessing the underlying string of the word, so I can then check if the string equals “wall”, but I can’t figure out how to access it.

If I write the second for loop as “for (Word word : sentence) {” then I get an incompatible types error message on complilation.

  • 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-05-26T05:00:50+00:00Added an answer on May 26, 2026 at 5:00 am

    The String content can be accessed by calling the method: word() on edu.stanford.nlp.ling.Word; e.g.

    import edu.stanford.nlp.ling.Word;
    
    List<Word> words = ...
    for (Word word : words) {
      if (word.word().equals(args(1))) {
        System.err.println("Yes!");
      }
    }
    

    Also note that it is better to use generics when defining the List as it means the compiler or IDE will typically warn you if you attempt to compare classes of incompatible types (e.g. Word versus String).

    EDIT

    Turns out I was looking at an older version of the NLP API. Looking at the most recent DocumentPreprocessor documentation I see that it implements Iterable<List<HasWord>> whereby HasWord defines the word() method. Hence your code should look something like this:

    DocumentPreprocessor dp = ...
    for (HasWord hw : dp) {
      if (hw.word().equals(args[1])) {
        System.err.println("Yes!");
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the Stanford coreNLP ( http://nlp.stanford.edu/software/corenlp.shtml ) in order to parse sentences
Using the method presented here: http://cslibrary.stanford.edu/110/BinaryTrees.html#java 12. countTrees() Solution (Java) /** For the key
I'm building a NLP application and have been using the Stanford Parser for most
I'm trying to check spelling accuracy of text samples using the Stanford NLP. It's
I am trying to extract Arabic proper names from a text using Stanford Parser.
I want to create an event calendar application similar to the site http://events.stanford.edu/ using
I tried to parse a sentence using Stanford parser, but I get the exception.
I am using an NLP library (Stanford NER) that throws java.lang.OutOfMemoryError errors for rare
I am trying to parse out sentences from a huge amount of text. using
UPDATED: SEE BELOW I've been porting the code for this assignment: http://www.stanford.edu/class/cs221/progAssignments/PA1/search.html (the entire

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.