Earlier, I was looking up questions about Pset6 to see if there are any answers that can help me with my code, however, it looks like I completely missed the memo for using set
with similarities. My new question is: do I have to use set
for my code, and if not, how do I fix my errors such as "AttributeError: '_io.TextIOWrapper' object has no attribute 'sent_tokenize'".
My code is as follows:
from nltk.tokenize import sent_tokenize
def lines(a, b):
"""Return lines in both a and b"""
a_file = open("a", "r+")
b_file = open("b", "r+")
a_list = a_file.readlines()
b_list = b_file.readlines()
matching_lines = []
for i in range(len(a_list)):
if a_list[i] == b_list[i]:
if a_list[i] not in matching_lines:
matching_lines.append(a_list[i])
return matching_lines
def sentences(a, b):
"""Return sentences in both a and b"""
a_file = open("a", "r+")
b_file = open("b", "r+")
a_list = a_file.readlines()
b_list = b_file.readlines()
matching_sent = []
for i in range(len(sent_tokenize(a_list))):
if sent_tokenize(a_list)[i] == sent_tokenize(b_list)[i]:
if sent_tokenize(a_list)[i] not in matching_sent:
matching_sent.append(sent_tokenize(a_list)[i])
return matching_sent
def substrings(a, b, n):
"""Return substrings of length n in both a and b"""
matching_subs = []
a_file = open("a", "r+")
b_file = open("b", "r+")
for i in range(len(a_file)):
a_list = a_file[i:n+i]
for i in range(len(b_file)):
b_list = b_file[i:n+i]
if a_list[i] == b_list[i]:
if a_list[i] not in matching_subs:
matching_subs.append(a_list[i])
return matching_subs
I realize that my code for sentences is very different compared to the rest of my functions, I was playing around with tokenize because I don't think I fully understand how it should be implemented. For instance, is it new_variable.sent_tokenize(a_list)
, a_list.sent_tokenize()
, or neither? Or if I have to use set
, does that even matter?
Thank you for your help!