Question: Hello! I am writing an algorithm in python3 to tag certain words according to a particular ontology (the TRIPS ontology) found here: http://www.cs.rochester.edu/research/trips/lexicon/ The algorithm

Hello! I am writing an algorithm in python3 to tag certain words according to a particular ontology (the TRIPS ontology) found here: http://www.cs.rochester.edu/research/trips/lexicon/

The algorithm takes in a text file as input and writes to an output file. the input file looks like this (One word per line):

CONSUME LOCATION GOVERNING FORMAL

At the moment, when I run the tagging algorithm it will tag up to three words fine but breaks on the fourth word. This is the error I get, including some debugging prints:

python3 -c "from code.test import tagging_tests; tagging_tests('input/tagging.txt','output/tagging.txt');"

currWord: CONSUME

RESULT[ABSTRACT-OBJECT], AGENT[ORGANISM], AFFECTED[FOOD, WATER, MEDICATION]

currWord: LOCATION

FIGURE[SITUATION-ROOT, PHYS-OBJECT]

currWord: GOVERNING

FORMAL[ABSTRACT-OBJECT, SITUATION-ROOT, PHYS-OBJECT], RESULT[ABSTRACT-OBJECT], AGENT[PHYS-OBJECT], AFFECTED[DEVICE, LOCATED-EVENT, MOLECULAR-PART]

Traceback (most recent call last):

File "", line 1, in

File "/home/fparnes/CSC247/project1-master/code/test.py", line 89, in tagging_tests

currArguments = [entry["arguments"] for entry in tripsOntology if entry["name"] == currWord][0]

IndexError: list index out of range

note the command on top is the comand to run the function. This is the code for the tagging algorithm:

def tagging_tests(input_file, output_file): """this function should load a file containing a list of words, one word per line.  Each line of the output file will contain a comma separated list of ontology types derived from the ontology only for the corresponding line in the input.  """   tripsOntology = load_data("data/ontology.json") #open input file, "r" means we are reading the file taggingInputFile = open(input_file, "r") taggingOutputFile = open(output_file, "w") #Loop through rows on input file/output to same line for currWord in taggingInputFile: #Find arguments of word from input row in ontology.json currWord = currWord.upper().strip() if len(currWord) > 0: currArguments = [entry["arguments"] for entry in tripsOntology if entry["name"] == currWord][0] print("currWord: " + currWord) #For each argument role write the role and all of its restrictions in the form ROLE[REST1, REST2, REST3] to output file roles = "" for i in currArguments: roles += i["role"] + "[" if len(i["restrictions"]) > 0: #Make sure there are actually restrictions for restr in i["restrictions"]: if type(restr) is list: # if the list is not empty and the first element is a list restr = restr[2:] # take the argument list starting from index 2 for j in restr: roles += j + ", " else: roles += restr + ", " roles = roles[:-2] roles += "], " roles = roles[:-2] + " " print(roles) taggingOutputFile.write(roles) pass

Can anyone help find the reason this goes out of bounds with 4 words?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!