Search code examples
pythonpython-3.xnlp

How to find a word - First letter will be capital & other will be lower


Problem Statement: Filter those words from the complete set of text6, having first letter in upper case and all other letters in lower case. Store the result in variable title_words. print the number of words present in title_words.

I have tried every possible ways to find the answer but don't know where I am lagging.

import nltk
from nltk.book import text6
title_words = 0
for item in set(text6):
    if item[0].isupper() and item[1:].islower():
        title_words += 1
print(title_words)

I have tried in this way as well:

title_words = 0
for item in text6:
    if item[0].isupper() and item[1:].islower():
        title_words += 1
print(title_words)

I am not sure how many count its required, whatever the count is coming its not allowing me to pass the challenge. Please let me know if I am doing anything wrong in this code


Solution

  • I think the problem is with set(text6). I suggest you iterate over text6.tokens.

    Update, explanation

    The code you've provided is correct.

    The issues is that the text can contain same words multiple times. Doing a set(words) will reduce the total available words, so you start with an incomplete data set.

    The other responses are not necessary wrong in checking the validity of a word, but they are iterating over the same wrong data set.