Информационно-поисковый сборник документов Чтение - PullRequest
0 голосов
/ 08 ноября 2019

Итак, в моем коде я читаю файл, который содержит много документов, и я читаю эти документы и беру важные слова и печатаю. Тем не менее, я могу анализировать документы для чтения по одному, но когда дело доходит до чтения текста документов, у меня появляется «KeyError», я создал функцию, которая читает текст и вводит текст в список и создаетслово для его существования. Я не знаю, где в моем коде я ошибаюсь при индексации слов.

Вот коллекция документов, из которой она должна читать (только фрагмент первого):

<DOC>
<DOCNO> AP890101-0001 </DOCNO>
<FILEID>AP-NR-01-01-89 2358EST</FILEID>
<FIRST>r a PM-APArts:60sMovies     01-01 1073</FIRST>
<SECOND>PM-AP Arts: 60s Movies,1100</SECOND>
<HEAD>You Don't Need a Weatherman To Know '60s Films Are Here</HEAD>
<HEAD>Eds: Also in Monday AMs report.</HEAD>
<BYLINE>By HILLEL ITALIE</BYLINE>
<BYLINE>Associated Press Writer</BYLINE>
<DATELINE>NEW YORK (AP) </DATELINE>
<TEXT>
   The celluloid torch has been passed to a new
generation: filmmakers who grew up in the 1960s.

       #Part 1
        # 1) Each doc begins w < DOC > and ends w < /DOC >
        # 2) First few lines contain metadata, you should read
        #    the < DOCNO > field and use it as the ID of the doc
        # 3) Doc contents are between the tags < TEXT > and < /TEXT >
        # 4) All other file contents can be ignored.
        # 5) Remove all stop words, punct., and lowercase all words 
        def inv_idx(self, doc):
            print()
            print("Good job, we are in inv_idx() !")
            print()

            doc_num = 0
            doc_text = 0
            # count = 0
            ps = PorterStemmer()

            with open(doc, 'r') as fs:
                for line in fs:
                    if "<DOC>" in line:
                        doc_num += 1 
                    elif doc_num: #1 enters condition
                        #number the doc
                        if "<DOCNO>" in line:
                            print("in DOCNO tag")
                            #split line and return first item, which is the docID 
                            doc_name = line.split()[1] 
                            print(doc_name)

                        elif "<TEXT>" in line:
                            print("in TEXT tag")
                            doc_text += 1

                        #parse doc's text
                        elif doc_text: 
                            if "</TEXT>" in line:
                                print("in /TEXT tag")
                                doc_text = 1
                            #tokenize all words + see if they are in the main list (aka if they exist in the doc and are not stop words)    
                            else: 
                                tokens = word_tokenize(line)
                                for token in tokens:
                                    token = ps.stem(token.lower())

                                    if token not in self.stopWords and token.isalpha():
                                        if token not in self.postings:#only add if word appears in doc
                                            self.postings[token][doc_name]= 1 # Why am I getting this error?
                                            #print("line82") OK SO WE KNOW WE HIT 82 
                                        else: 
                                            if doc_text not in self.postings[token]:
                                                self.postings[token][doc_name] = 1 #first appearance
                                            else: 
                                                self.postings[token][doc_name] += 1 #increment appearance

                                                #print(self.postings[token])

                                        if token not in self.termFreq:               #add_docfreq
                                             # only add if word apepars in doc
                                            self.termFreq[token] = [doc_name]        #add_docfreq
                                        else:                                        #add_docfreq
                                            if doc_name not in self.termFreq[token]: #add_docfreq
                                                self.termFreq[token].append(doc_name)#add_docfreq

                                for word in line.split():                            #total_terms 
                                    if word not in self.stopWords and word.isalpha():#total_terms 
                                        count += 1                                   #total_terms 

                        #move on to next document
                        elif "</DOC>" in line:
                            # self.totalTerms[doc_name] = count                       #total_terms 
                            doc_num = 0
                            # count = 0                                               #total_terms 
            print("EOFFFF")

Вот вывод / ошибка:

Хорошая работа, мы в inv_idx ()! В теге DOCNO AP890101-0001 в теге TEXT Traceback (последний вызов последний): Файл "ps2.py", строка 151, в newPS2.inv_idx ("data / ap89_collection") Файл "ps2.py", строка 74, в inv_idx self.postings [токен] [doc_name] = 1

KeyError: 'celluloid

...