0

So I am making my own MadLibs game, and have come across a problem. When a user is requested to enter a noun, for example, but instead enters a verb or adverb etc. I want my program to pick up on this, and ask them to enter a different word as this word does not match criteria. How do I do this? This is what I have so far:

while True:
    name1 = input("A male name (protagonist): ")
    if name1.endswith (('ly', 's')):
        print("Sorry mate, this doesn't seem to be a proper noun. Try it again.")
        continue
    break

But I would like it to come out along the lines of this:

A male name (protagonist): sandwich
Sorry mate, this doesn't seem to be a proper noun. Try it again.
A male name (protagonist): Bob

How do I make it recognise nouns, adverbs etc. without me manually typing it in?

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
Cooper Timewell
  • 45
  • 1
  • 1
  • 9
  • Welcome to Stack Overflow! It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, stack traces, compiler errors - whatever is applicable). The more detail you provide, the more answers you are likely to receive – Avinash Raj Feb 16 '15 at 04:32
  • Ok, this is what I have so far then: while True: name1 = input("A male name (protagonist): ") if name1.endswith (('ly', 's')): print("Sorry mate, this doesn't seem to be a proper noun. Try it again.") continue break – Cooper Timewell Feb 16 '15 at 04:34
  • possible duplicate of [How to loop on Python 3.4.2?](http://stackoverflow.com/questions/28533622/how-to-loop-on-python-3-4-2) – Avinash Raj Feb 16 '15 at 04:35
  • No, I am trying a new approach this time, it is a different question. – Cooper Timewell Feb 16 '15 at 04:38
  • but the code you tried on both the questions looks same. – Avinash Raj Feb 16 '15 at 04:39
  • It is the same but it is an entirely different question, I just used that as the sample code to represent what my question is about. – Cooper Timewell Feb 16 '15 at 04:47
  • I think the problem is how do you differentiate between a noun and a verb? are there any universal rules for this in, I assume, English language? If not, than only dictionary search would solve this. – Marcin Feb 16 '15 at 04:50
  • 1
    Reference: http://stackoverflow.com/questions/17669952/finding-proper-nouns-using-nltk-wordnet – planet260 Feb 16 '15 at 10:33
  • IMHO it is **really** hard to identify correct names. Your example is particurarly interesting because Sandwich is actually the name of a British statesman John Montagu, 4th Earl of Sandwich, (13 November 1718 – 30 April 1792). And Micheal Connel**ly** (emphasis mine) (born July 21, 1956) is an American author of detective novels. (references from wikipedia ...) – Serge Ballesta Feb 16 '15 at 10:48

1 Answers1

0

What you are looking for is Natural Language Processing mate. You have to identify which part of speech is the word & then you can tag it. NLP field is vast & complex so try researching on your own & you might come up with some solution. But I think there is no direct way to do that in Python. Python is programming language. Though you can use some tools that might help you tag POS, such as Tree tagger & try integrating them in your application.

harshad
  • 410
  • 4
  • 17