Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 信息检索文档集阅读_Python_Indexing_Compiler Errors_Information Retrieval - Fatal编程技术网

Python 信息检索文档集阅读

Python 信息检索文档集阅读,python,indexing,compiler-errors,information-retrieval,Python,Indexing,Compiler Errors,Information Retrieval,因此,在我的代码中,我正在阅读一个包含许多文档的文件,我正在阅读这些文档,并接受重要的单词和打印。然而,我能够解析文档,一次读取一个文档,但当涉及到读取文档文本时,我遇到了一个“KeyError”,我创建了一个函数,该函数读取文本并将文本输入到列表中,然后为其创建单词。我不知道在我的代码中,我在索引单词时哪里出错了 以下是它应该从中读取的文档集合(只是第一个文档的一个片段): AP890101-0001 AP-NR-01-01-89 2358EST r a PM公寓:60sMovies 01-

因此,在我的代码中,我正在阅读一个包含许多文档的文件,我正在阅读这些文档,并接受重要的单词和打印。然而,我能够解析文档,一次读取一个文档,但当涉及到读取文档文本时,我遇到了一个“KeyError”,我创建了一个函数,该函数读取文本并将文本输入到列表中,然后为其创建单词。我不知道在我的代码中,我在索引单词时哪里出错了

以下是它应该从中读取的文档集合(只是第一个文档的一个片段):


AP890101-0001
AP-NR-01-01-89 2358EST
r a PM公寓:60sMovies 01-01 1073
PM-AP艺术:60年代电影,1100年
你不需要气象员就能知道60年代的电影在这里
Eds:也在周一的AMs报告中。
希莱尔意大利
美联社记者
纽约(美联社)
赛璐珞火炬已经传递给了一家新公司
一代:成长于20世纪60年代的电影制片人。
#第一部分
#1)每个单据从w开始,到w结束
#2)前几行包含元数据,您应该阅读
#字段,并将其用作文档的ID
#3)文档内容位于标签
#4)可以忽略所有其他文件内容。
#5)删除所有停止字、点字,并将所有字小写
def inv_idx(自身,文件):
打印()
打印(“干得好,我们在inv_idx()!”)
打印()
doc_num=0
doc_text=0
#计数=0
ps=PorterStemmer()
以开放式(doc,'r')作为fs:
对于fs中的行:
如果行中有“”:
doc_num+=1
elif doc_num:#1进入条件
#给医生编号
如果行中有“”:
打印(“在DOCNO标签中”)
#拆分行并返回第一项,即docID
doc_name=line.split()[1]
打印(文件名)
行中的“elif”:
打印(“文本标记中”)
doc_text+=1
#解析文档的文本
elif文件文本:
如果行中有“”:
打印(“输入/文本标记”)
doc_text=1
#标记所有单词+查看它们是否在主列表中(如果它们存在于文档中且不是停止词,则称为aka)
其他:
标记=单词\标记化(行)
对于令牌中的令牌:
token=ps.stem(token.lower())
如果token不在self.stopWords和token.isalpha()中:
如果标记不在self.posting中:#仅当word出现在文档中时添加
self.postings[token][doc_name]=1#为什么我会遇到此错误?
#打印(“line82”)好的,这样我们就知道我们达到了82
其他:
如果文件文本不在self.posting[标记]中:
self.posting[token][doc_name]=1#首次亮相
其他:
self.posting[token][doc_name]+=1增量外观
#打印(self.posting[令牌])
如果令牌不在self.termFreq中:#添加_docfreq
#仅当文档中有单词apepar时添加
self.termFreq[token]=[doc_name]#add_docfreq
其他:#添加#docfreq
如果文件名不在self.termFreq[令牌]中:#添加文件频率
self.termFreq[token].append(doc_name)#add_docfreq
对于line.split()中的单词:#总计_项
如果单词不在self.stopWords和word.isalpha()中:#总计
计数+=1#总计_项
#转到下一个文档
行中的“elif”:
#self.totalTerms[doc_name]=count#total_terms
doc_num=0
#计数=0#总计_项
打印(“EOFFFF”)
以下是输出/错误:

干得好,我们在inv_idx()中!在DOCNO tag AP890101-0001中的文本标记回溯(最近一次调用):文件“ps2.py”,第151行,在newPS2.inv_idx(“数据/ap89_集合”)文件“ps2.py”,第74行,在inv_idx self中。过账[令牌][doc_名称]=1


KeyError:'赛璐珞

这是来自命令提示符的错误消息:干得好,我们在inv_idx()中!在DOCNO标记AP890101-0001文本标记回溯中(最后一次调用):文件“ps2.py”,第151行,在newPS2.inv_idx(“数据/ap89_集合”)文件“ps2.py”,第74行,在inv_idx self.postings[token][doc_name]=1#为什么会出现此错误?KeyError:“赛璐珞”欢迎使用StackOverflow!若要添加更多信息,请编辑问题itsef,而不是添加注释。您可以将错误消息添加到问题中吗?代码中的哪一行给出了此错误?
<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")