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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:40:37+00:00 2026-05-31T15:40:37+00:00

I’ve just started working with nltk and am having trouble getting the concordance module

  • 0

I’ve just started working with nltk and am having trouble getting the concordance module to work with conditional variables. I would like to return a concordance for any given word in a Latin text, but since the language is inflected, I want to be able to specify the stem, identify any word in the corpus that contains the stem, and return a concordance for that. The code I’m using is:

book1 = open('Book1.txt', 'rU').read()
token1 = nltk.word_tokenize(book1)
text1 = nltk.Text(token1)

word = raw_input("What stem do you want to search?\n > ")

text1.concordance([w for w in text1 if w.startswith(word)])

Which returns the error:

    Traceback (most recent call last):
  File "C:\Users\admin\Desktop\start_nltk_horace.py", line 68, in <module>
    concordance()
  File "C:\Users\admin\Desktop\start_nltk_horace.py", line 49, in concordance
    text1.concordance([w for w in text1 if w.startswith(word)])
  File "C:\Python27\lib\site-packages\nltk\text.py", line 314, in concordance
    self._concordance_index.print_concordance(word, width, lines)
  File "C:\Python27\lib\site-packages\nltk\text.py", line 177, in print_concordance
    offsets = self.offsets(word)
  File "C:\Python27\lib\site-packages\nltk\text.py", line 156, in offsets
    word = self._key(word)
  File "C:\Python27\lib\site-packages\nltk\text.py", line 312, in <lambda>
    key=lambda s:s.lower())
AttributeError: 'list' object has no attribute 'lower'

Just specifying text1.concordance(word) returns what I’m looking for without any issues (providing I input the fully-declined word), but I would have to repeat the function six-ish times to get a concordance for all of the different declensions of a word.

  • 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-31T15:40:38+00:00Added an answer on May 31, 2026 at 3:40 pm

    I think the problem is that you’re trying to supply NLTK’s concordance() function with a list of words, when it only accepts a string. Try the following instead:

    my_concordances = []
    my_inputs = [elem for elem in text1 if elem.startswith(word)]
    for input in my_inputs:
        my_concordances.append(text1.concordance(input))
    

    Then, my_concordances should end up as a list where each entry is a concordance for a different word that started with the raw input string. You can also consider pre-allocating the space for my_concordances depending on what specific data type gets returned by the concordance() function, since you can just check the length of my_inputs. That might improve speed if it’s an issue.

    Note that this question might be of interest to you too. It goes into more detail on concordance().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.