Python Forum
Computational linguistic program - Need some help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Computational linguistic program - Need some help
#1
Hello,
I am a student in linguistic from France. So excuse me if my english is perfectible.
I follow a course in computational linguistic and I have to create a program from a list of projects proposed by my teacher.
I chose to create one that open a text, delete a specific target language element (in this case, the articles from dutch, het and de) and submit to the user, line by line, the sentences with the missing article and asks for the answer. At the end, it gives the grade, out of the total number of the sentences.
Here it is : https://github.com/GangleriS/fill_in_the...ree/master
There is the program and the test text file.
I have a problem with it :
I do not know why is there two rounds of questioning as you will see. And when I try to adjust, like :
while i<=2 and o<=2:
or
while i<len(questions) and o<len(words):
that give this :
IndexError: list index out of range

Thanks for your help.
Reply
#2
You should post your code and the full text of the error message. But my guess is that questions is empty before the loop starts.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Here is the code :
import re
words=[]
questions=[]
text = open("dutch", mode="r")
for line in text:
    line=line.rstrip("\n")
    list=re.finditer(r"\b[dD]e\b|\b[hH]et\b", line)
    for correspondance in list:
        words.append(correspondance.group(0))
    list2 = re.sub(r"((\b[Hh]et\b)|(\b[Dd]e\b))", "?", line)
    if list2:
        questions.append(list2)
for line in questions:
    i = 0
    o = 0 
    u = 0
    while i<len(questions) and o<len(words):
        print(questions[i])
        answer = input("Which article? ")
        if answer == words[o]:
          print("Right answer")
          u = 0 + 1
        else:
            print("Wrong answer")
        i = i + 1
        o = o + 1
print ("Grade", u, "sur", i)
text.close()
and the sentences :
Ik heb het werk gedaan.
De man is rijk.
Well, there is no error messages when launch as it is here.
The problem is with the while loop, which instead of stop at the last sentence, give it another turn from the beginning and then stop.
Why questions would be empty? I am pretty sure having verified with print each time and that returned things Huh

For the error message, when the while loop is like this :
while i<=len(questions) and o<=len(words):
Error:
Traceback (most recent call last): Wrong answer File "/home/antoine/Programs/tao/texte_a_trou.py", line 25, in <module> print(questions[i]) IndexError: list index out of range Process finished with exit code 1
Reply
#4
I don't get an index error when I run your code with those two sentences. But looping through indexes causes just sort of these problems. It also seems that you are looping through questions twice, once with the for loop on 14, and once with the while loop and i on line 18. If you have a list of questions, and a list of the corresponding words that are the answers to those questions, you should do it with zip:

correct = 0
for question, word in zip(questions, words):
    print(question)
    answer = input("Which article? ")
    if answer == word:
        print("Right answer")
        correct += 1
    else:
        print("Wrong answer")
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Indeed, I did not know zip but it is way more practical compare to what I did.
Thanks a lot, that works perfectly Smile
I mark this thread as solved.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Advanced Algorithms and Computational Models hafedh 4 2,284 Aug-31-2020, 06:37 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020