Computational linguistic program - Need some help - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Computational linguistic program - Need some help (/thread-17967.html) |
Computational linguistic program - Need some help - Antoine - May-01-2019 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_blanks_program/tree/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. RE: Computational linguistic program - Need some help - ichabod801 - May-01-2019 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. RE: Computational linguistic program - Need some help - Antoine - May-01-2019 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 For the error message, when the while loop is like this : while i<=len(questions) and o<=len(words):
RE: Computational linguistic program - Need some help - ichabod801 - May-01-2019 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") RE: Computational linguistic program - Need some help - Antoine - May-01-2019 Indeed, I did not know zip but it is way more practical compare to what I did. Thanks a lot, that works perfectly I mark this thread as solved. |