|
| 1 | +from mmkfeatures.fusion.mm_features_lib import MMFeaturesLib |
| 2 | +import time |
| 3 | + |
| 4 | +mmf_file=f"../datasets/birds.mmf" |
| 5 | + |
| 6 | +def get_exact_match_ratio(lib,result): |
| 7 | + num_match = 0 |
| 8 | + for key in result: |
| 9 | + content = lib.get_content_by_id(key) |
| 10 | + text = content["text"][()].decode("utf-8", "ignore") |
| 11 | + # print(key, text) |
| 12 | + if query_str in text: |
| 13 | + num_match += 1 |
| 14 | + print("p = ", round(num_match * 1.0 / len(result), 4)) |
| 15 | + return round(num_match * 1.0 / len(result), 4) |
| 16 | + |
| 17 | +list_result=[] |
| 18 | +start_time=time.time() |
| 19 | + |
| 20 | + |
| 21 | +print("loading mmf files...") |
| 22 | +birds_lib=MMFeaturesLib(file_path=mmf_file) |
| 23 | + |
| 24 | +time_load=time.time()-start_time |
| 25 | +list_result.append(("load",time_load)) |
| 26 | + |
| 27 | +print("creating plain text index...") |
| 28 | +start_time=time.time() |
| 29 | +birds_lib.to_index_file("text","../datasets/text.index",index_type="brutal_force") |
| 30 | +time_brutal_force=time.time()-start_time |
| 31 | + |
| 32 | + |
| 33 | +list_result.append(("brutal force indexing",time_brutal_force)) |
| 34 | + |
| 35 | +print("creating inverted index....") |
| 36 | +start_time=time.time() |
| 37 | +birds_lib.to_index_file("text","../datasets/text_inverted.index",index_type="inverted_index") |
| 38 | +time_inverted=time.time()-start_time |
| 39 | +list_result.append(("inverted indexing",time_inverted)) |
| 40 | + |
| 41 | +print("creating positional text...") |
| 42 | +start_time=time.time() |
| 43 | +birds_lib.to_index_file("text","../datasets/text_positional.index",index_type="positional_index") |
| 44 | +time_positional=time.time()-start_time |
| 45 | +list_result.append(("positional indexing",time_positional)) |
| 46 | + |
| 47 | +# start to perform search test |
| 48 | +query_str="large brown wings" |
| 49 | + |
| 50 | +print("searching plain index test....") |
| 51 | +start_time=time.time() |
| 52 | +result_bf=birds_lib.search_index(index_file_path="../datasets/text.index",query=query_str,search_type="brutal_force") |
| 53 | +print(result_bf) |
| 54 | +search_time_brutal=time.time()-start_time |
| 55 | +list_result.append(("brutal force searching",search_time_brutal,get_exact_match_ratio(birds_lib,result_bf))) |
| 56 | + |
| 57 | +print("searching inverted index test....") |
| 58 | +start_time=time.time() |
| 59 | +result_bf=birds_lib.search_index(index_file_path="../datasets/text_inverted.index",query=query_str,search_type="inverted_index") |
| 60 | +print(result_bf) |
| 61 | +search_time_inverted=time.time()-start_time |
| 62 | +list_result.append(("inverted index searching",search_time_inverted,get_exact_match_ratio(birds_lib,result_bf))) |
| 63 | + |
| 64 | +print("searching positional index test....") |
| 65 | +start_time=time.time() |
| 66 | +result_bf=birds_lib.search_index(index_file_path="../datasets/text_positional.index",query=query_str,search_type="positional_index") |
| 67 | +print(result_bf) |
| 68 | +search_time_positional=time.time()-start_time |
| 69 | +list_result.append(("positional index searching",search_time_positional,get_exact_match_ratio(birds_lib,result_bf))) |
| 70 | +print() |
| 71 | +print("Indexing method\tTime cost\tExact match ratio") |
| 72 | +for result in list_result: |
| 73 | + if len(result)==3: |
| 74 | + print(f"{result[0]}\t{result[1]}\t{result[2]}") |
| 75 | + |
| 76 | +print() |
| 77 | +print("Loading method\tTime cost") |
| 78 | +for result in list_result: |
| 79 | + if len(result)==2: |
| 80 | + print(f"{result[0]}\t{result[1]}") |
| 81 | + |
| 82 | + |
0 commit comments