TransWikia.com

How to print specific words from a sentence

Stack Overflow Asked by ASTRONOMICAL on February 22, 2021

Im trying to work on my English class and i was trying to take out a specific pronoun from a sentence so i want to choose the pronoun like: demonstrative and use if statements to printout only the demonstrative pronouns from the sentence and i can only find this:

**# initializing list
test_list = ["There is a weekend tomorrow", "This is a simple String", "This is only for demonstration of what i got", "Please help"]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = "T"
res = [ele for temp in test_list for ele in temp.split() if ele[0].lower() == K.lower()]
# printing result
print("The filtered elements : " + str(res))**

It only prints the words starting with T but i want specific words like a pronoun so please help

One Answer

i found the answer when i made some simpler code that can get me the number of pronouns and i asked for an answer so someone helped me here is the code:

print("Noun Counter")
sentence = input("Write A sentence: ")
pronoun = (input("Choose a pronoun from Demonstrative, interrogative and indefinite: "))


if pronoun == "Demonstrative":
    source = sentence
    pronouns = ["this", "that", "these", "those"]
    print("Number of Demonstrative pronouns: ")
    count = dict(zip(pronouns, [0]*len(pronouns)))  # addition
    words = source.split()
    for w in words:
        if w.lower() in pronouns:
            count[w.lower()] += 1  # addition
    print(count)

elif pronoun == "Indefinite":
    source = sentence
    pronouns = ["Another", "anybody", "anyone", "anything", "each", "either", "enough", "everybody", "everyone",
                "everything", "little", "much", "neither", "nobody", "no one", "nothing", "one", "other", "somebody",
                "something", "Both", "few", "fewer", "many", "others", "several", "All", "any", "more", "most",
                "someone", "none", "some", "such"]  # download a list from somewhere
    print("Number of Indefinite pronouns: ")
    count = dict(zip(pronouns, [0]*len(pronouns)))  # addition
    words = source.split()
    for w in words:
        if w.lower() in pronouns:
            count[w.lower()] += 1  # addition
    print(count)

elif pronoun == "Interrogative":
    source = sentence
    pronouns = ["who", "whom", "whose", "what", "which"]
    print("Number of Interrogative pronouns: ")
    count = dict(zip(pronouns, [0]*len(pronouns)))  # addition
    words = source.split()
    for w in words:
        if w.lower() in pronouns:
            count[w.lower()] += 1  # addition
    print(count)

else:
    print("No pronoun found named: " + pronoun + ", please try again")

Thanks for all your help anyways. Wherever it says #addition thats where i got help.

Answered by ASTRONOMICAL on February 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP