Я пытаюсь синхронизировать свою программу согласования слов с моим BinarySearchTree, чтобы увидеть время выполнения, которое требуется. Но я получаю указанную выше ошибку.
def concordance():
wordConcordanceDict = BinarySearchTree()
with open('stop_words.txt') as sw:
words = (line.strip() for line in sw)
stop_words = set(words)
with open('WarAndPeace.txt') as f:
for line_number, line in enumerate(f, 1):
words = (re.sub(r'[^\w\s]','',word).lower() for word in line.split())
good_words = (word for word in words if word not in stop_words)
for word in good_words:
# it doesn't work as defaultdict. So we have to check
# if key is present in dictionary or not
try:
# key is present
wordConcordanceDict[word].append(line_number)
except:
# key is not present, so we create a new key
wordConcordanceDict[word] = [line_number]
# the given implementation of dictionary don't have a method to get all keys/ items as there in python dictionaries.
# instead, there's a function to get all key, value pairs as a string
# we call that method and convert it to a dictionary
wordConcordanceDict = re.sub(r', ',',',wordConcordanceDict.__str__()).split()[1:-1]
wordConcordanceDict = dict(x.split(":") for x in wordConcordanceDict) # getting error here
# import regex
import re
from binary_search_tree import BinarySearchTree
# import time module
import time
# start time
start = time.time()
# given function call
concordance()
# end time
end = time.time()
# display time taken
print(end - start)
Я не знаю, что означает ошибка и как ее решить. Пожалуйста, дайте мне знать.