class SearchEngineBase :
def __init__ ( self) :
pass
#搜索器
def add_corpus ( self, file_path) :
with open ( file_path, "rb" ) as fin:
text = fin. read ( ) . decode ( "utf-8" )
self. process_corpus ( file_path, text)
#索引器
def process_corpus ( self, id, text) :
raise Exception ( "process_corpus not implemented" )
#检索器
def serch ( self, query) :
raise Exception ( "search not implemented" )
#用户接口
def main ( search_engine) :
for file_path in [ '1.txt' , '2.txt' , '3.txt' , '4.txt' , '5.txt' ] :
search_engine. add_corpus ( file_path)
while True:
query= input ( )
results = search_engine. search ( query)
print ( 'found {} result(s): ' . format ( len ( results) ) )
for result in results:
print ( result)
class SimpleEngine ( SearchEngineBase) :
def __init__ ( self) :
super ( SimpleEngine, self) . __init__ ( )
self. __id_to_texts = { }
def process_corpus ( self, id, text) :
self. __id_to_texts[ id] = text
def search ( self, query) :
results= [ ]
for id, text in self. __id_to_texts. items ( ) :
if query in text:
results. append ( id)
return results
search_engine = SimpleEngine ( )
main ( search_engine)