skip to Main Content

Background

I have created an application that queries the user for facts, then allows them to set parts of these facts as fill in the blanks. One of the key ideas is the user will be able to create multiple facts, e.g. George Washington was a president of the USA and Franklin D. Roosevelt was a president of the USA, and be able to know both George Washington and Franklin D. Roosevelt are presidents.

The challange

The problem comes in situations where one of these is plural and the other is singular. It could also occur when one ends with a vowel (meaning you’d have to use an instead of a), and the other Then, while answering a multiple choice question to fill in that blank, the question might ask Stackoverflow is a website and give the option, organization. Is there any way to fix these issues other than using a/an, and what algorithms could be used to achieve the desired result.

2

Answers


  1. you would want to create templates, with a list of possible answers

    such as

    questions = [
       [
            "__ was a president of the USA",
            [
                 "George Washington",
                 "Franklin D. Roosevelt",
                 "Franklin D Roosevelt",
                 "Franklin Delano Roosevelt",
                 "Franklin Roosevelt"
            ]
       ]
    ]
    

    before we start the code.
    you will need to generate these questions array from the facts

    store each fact, and then group them together so that:

    George Washington was a president of the USA
    Franklin D. Roosevelt was a president of the USA

    These are very similar, so use some logic to get the difference, as both share the string format of __ was a president of the USA

    with this data, you can now generate the questions array, and the code following will work. The generating from the facts might be tricky but its a great learning experience, good luck.

    Then just loop through each question, and check if the answer matches one of the possible answers. if so then just count that as +1 else if not a match, go to the next question or just repeat the question

    Regarding the an/a you would just have to check if the spot before or next to the __ starts with a vowel.

    score = 0
    questions = [
       [
            "__ was a president of the USA",
            [
                 "George Washington",
                 "Franklin D. Roosevelt",
                 "Franklin D Roosevelt",
                 "Franklin Delano Roosevelt",
                 "Franklin Roosevelt"
            ]
       ]
    ]
    
    total_questions = len(questions)
    index = 0
    
    while index < total_questions:  # Change the condition to prevent an IndexError
        question = questions[index]
        print("Q: " + question[0])
        answer = input("A: ")
        valid_answer = answer in question[1]
        if not valid_answer:  # You can simplify this condition
            print("Incorrect, try again")
        else:
            score += 1  # Increment score
        index += 1  # Increment index
    
    print("Test Compelted")
    print("Your score was: " + str(score))  # Convert score to a string before printing
    

    Example Output:

    enter image description here

    Login or Signup to reply.
  2. Handling the challenges of singular and plural forms and correctly choosing between "a" or "an" in various contexts can be addressed through a combination of rule-based techniques and NLP tools. so u can use example in Python that demonstrates how you can use the textblob library to handle some of these issues. Make sure to install the library 
    first using pip install textblob.
    
    
    `enter code here`from textblob import TextBlob
    
    def select_indefinite_article(noun_phrase):
        blob = TextBlob(noun_phrase)
        first_word = blob.words[0]
    
        if first_word[0] in 'aeiou':
            return 'an'
        else:
            return 'a'
    
    user_facts = ["George Washington was a president of the USA", "Franklin D. Roosevelt was a president of the USA"]
    
    nouns = []
    for fact in user_facts:
        blob = TextBlob(fact)
        for word, tag in blob.tags:
            if tag == 'NN':  # Consider only singular nouns
                nouns.append(word)
    
    sentences = ["Stackoverflow is a website", "Stackoverflow is an organization"]
    
    for sentence in sentences:
        blob = TextBlob(sentence)
        for word, tag in blob.tags:
            if tag == 'NN':  # Consider only singular nouns
                if word in nouns:
                    article = select_indefinite_article(word)
                    print(f"{sentence} and give the option, {article} {word}")
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search