AttributeError при заполнении инвертированного индекса - PullRequest
0 голосов
/ 07 апреля 2019

Я пытаюсь заполнить инвертированный индекс, основанный на корпусе, который моя программа получает с данного веб-сайта. Я получаю

AttributeError: 'dict' object has no attribute 'encode'

но я не понимаю, почему, исходя из моего понимания, я правильно использую словарь для обхода перевернутого индекса и заполнения, поэтому мне нужна помощь! Ниже приведен код, из-за которого возникает ошибка. Я считаю, что это должно быть очень маленькое изменение, но я могу ошибаться ... Код:

def add(self, doc):
    for token in self.dict:
        token = token.encode("utf-8")
        if token in doc["title"].encode("utf-8") or token in doc["text"].encode("utf-8"):
            if doc["docId"] not in self.index[token]:
                self.index[token].append(doc["docId"])
                self.documents[doc["docId"]] = doc
def create_index(self):
     for doc in self.corpus:
         self.add(doc)

А вот пример формата корпуса:

    {
        "docId": 169,
        "title": "CSI 7901 Études dirigées / Directed Studies (3 crédits / 3 units)",
        "text": "Ce cours est équivalent à COMP 6901 à la Carleton University. / This course is equivalent to COMP 6901 at Carleton University."
    },

Edit: Вот класс, в котором мы сейчас находимся (инвертированный индекс):

class Index:
    def __init__(self):

        self.index = defaultdict(list)
        self.documents = {}
        self.__unique_id = 0
        with open("C:\Users\judyc\OneDrive\Documents\GitHub\MatteosMind\src\output\corpus.json",'rb') as dict_file:
            self.dict = json.load(dict_file)
        with open("C:\Users\judyc\OneDrive\Documents\GitHub\MatteosMind\src\output\corpus.json") as corpus_file:
            self.corpus = json.load(corpus_file)

В соответствии с запросом здесь приводится полное сообщение об ошибке:

Traceback (most recent call last):
  File "./src/main.py", line 39, in <module>
    main()
  File "./src/main.py", line 28, in main
    index.create_index()
  File ".\src\invertedindex.py", line 28, in create_index
    self.add(doc)
  File ".\src\invertedindex.py", line 20, in add
    token = token.encode("utf-8")
AttributeError: 'dict' object has no attribute 'encode'
...