-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbm25_search.py
151 lines (118 loc) · 4.1 KB
/
bm25_search.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import ctypes
# import bm25
# # Define the necessary C++ data types
class Text(ctypes.Structure):
_fields_ = [
('content', ctypes.c_char_p),
('tokens', ctypes.POINTER(ctypes.c_char_p)),
('length', ctypes.c_size_t)
]
class Document(ctypes.Structure):
_fields_ = [
('content', ctypes.c_char_p),
('tokens', ctypes.POINTER(ctypes.c_char_p)),
('length', ctypes.c_size_t),
('score', ctypes.c_double)
]
# class BM25(ctypes.Structure):
# pass
# # Define the function prototypes
# bm25.BM25_new.argtypes = [ctypes.POINTER(Text), ctypes.c_size_t, ctypes.POINTER(Document), ctypes.c_size_t]
# bm25.BM25_new.restype = ctypes.POINTER(BM25)
# bm25.BM25_delete.argtypes = [ctypes.POINTER(BM25)]
# bm25.BM25_delete.restype = None
# bm25.BM25_score.argtypes = [ctypes.POINTER(BM25), ctypes.POINTER(Document), ctypes.POINTER(Text), ctypes.c_size_t]
# bm25.BM25_score.restype = ctypes.c_double
# # Utility functions
# bm25.remove_vn_tone.argtypes = [ctypes.c_char_p]
# bm25.remove_vn_tone.restype = ctypes.c_char_p
# bm25.remove_meaningless_char.argtypes = [ctypes.c_char_p]
# bm25.remove_meaningless_char.restype = ctypes.c_char_p
# def create_text(content):
# text = Text()
# text.content = content.encode('utf-8')
# return text
# def create_document(content):
# doc = Document()
# doc.content = content.encode('utf-8')
# return doc
# def bm25_search(queries, docs):
# query_array = (Text * len(queries))()
# for i, query in enumerate(queries):
# query_array[i] = create_text(query)
# doc_array = (Document * len(docs))()
# for i, doc in enumerate(docs):
# doc_array[i] = create_document(doc)
# bm25_obj = bm25.BM25_new(query_array, len(queries), doc_array, len(docs))
# scores = []
# for doc in doc_array:
# score = bm25.BM25_score(bm25_obj, ctypes.byref(doc), query_array, len(queries))
# scores.append(score)
# bm25.BM25_delete(bm25_obj)
# return scores
# def remove_vn_tone(text):
# return bm25.remove_vn_tone(text.encode('utf-8')).decode('utf-8')
# def remove_meaningless_char(text):
# return bm25.remove_meaningless_char(text.encode('utf-8')).decode('utf-8')
# # Example usage
# if __name__ == '__main__':
# queries = ['Smooth as sandpaper', 'Rock Feeling']
# docs = [
# 'The Twist Feeling',
# 'Smooth Rock',
# 'Smooth Party Rock Anthem mix',
# 'I Gotta Feeling',
# 'Macarena (Bayside Boys mix)'
# ]
# scores = bm25_search(queries, docs)
# print(scores)
# import bm25
# def bm25_search(queries, docs):
# bm25_obj = bm25.BM25(queries, docs)
# scores = []
# for doc in docs:
# score = bm25_obj.score(doc, queries[0], len(docs))
# scores.append(score)
# return scores
# def remove_vn_tone(text):
# return bm25.remove_vn_tone(text)
# def remove_meaningless_char(text):
# return bm25.remove_meaningless_char(text)
# # Example usage
# if __name__ == '__main__':
# queries = ['Smooth as sandpaper', 'Rock Feeling']
# docs = [
# 'The Twist Feeling',
# 'Smooth Rock',
# 'Smooth Party Rock Anthem mix',
# 'I Gotta Feeling',
# 'Macarena (Bayside Boys mix)'
# ]
# scores = bm25_search(queries, docs)
# print(scores)
import bm25
def bm25_search(queries, docs):
query_texts = [bm25.Text(query) for query in queries]
doc_texts = [bm25.Document(doc) for doc in docs]
bm25_obj = bm25.BM25(query_texts, doc_texts)
scores = []
for doc in doc_texts:
score = bm25_obj.score(doc, query_texts[0], len(doc_texts))
scores.append(score)
return scores
def remove_vn_tone(text):
return bm25.remove_vn_tone(text)
def remove_meaningless_char(text):
return bm25.remove_meaningless_char(text)
# Example usage
if __name__ == '__main__':
queries = ['Smooth as sandpaper', 'Rock Feeling']
docs = [
'The Twist Feeling',
'Smooth Rock',
'Smooth Party Rock Anthem mix',
'I Gotta Feeling',
'Macarena (Bayside Boys mix)'
]
scores = bm25_search(queries, docs)
print(scores)