我是Elasticsearch的新手,我想知道是否可以这样做:
我有一堆地址字符串,我想对字符串中最重复的术语进行排序。
例如:
(int)(value * 1000 + 0.5)
我真正需要的是将它们放在字符串中最常见的术语上。
那么前面例子的排序输出应该是:
1. Shop no 1 ABC Lane City1 - Zipcode1
2. Shop no 2 EFG Lane City1 - Zipcode2
3. Shop no 1 XYZ Lane City2 - Zipcode3
4. Shop no 3 ABC Lane City1 - Zipcode1
我不知道如何去做。 我知道我可以触发每个字符串作为查询,以获得最接近被触发的查询的结果。 但是我有十万行,它似乎根本不是一个有效的选择。
如果我可以 1. Shop no 1 ABC Lane City1 - Zipcode1
4. Shop no 3 ABC Lane City1 - Zipcode1 # Because 1 and 2 have the most common words in them.
2. Shop no 2 EFG Lane City1 - Zipcode2 # Second most common words with 1 and 4.
3. Shop no 1 XYZ Lane City2 - Zipcode3 # Not all that many common terms amongst them.
和matchall()
使用sort
过滤器,每个字符串中包含最多的重复术语,那将非常有用。
可以对包含倒排索引中大多数相似单词的文档进行排序吗?
这是我的数据外观示例的示例: Sample Addresses
答案 0 :(得分:3)
<强>解决方案强>
我使用https://stackoverflow.com/a/15174569/61903来计算两个字符串的cosine similarity(@vpekar的信用)作为相似性的基本算法。通常我将所有字符串放入列表中。然后我将索引参数i设置为0并循环i,只要它在列表长度的范围内。在该循环中,我将位置p从i + 1迭代到长度(列表)。然后我找到list [i]和list [p]之间的最大余弦值。两个文本字符串都将被列入一个列表中,因此在以后的相似度计算中不会将它们考虑在内。两个文本字符串将与余弦值一起放入结果列表中,数据结构为VectorResult。
然后列表按余弦值排序。我们现在有唯一的字符串对,具有下降余弦,a.k.a。相似度值。 HTH。
import re
import math
import timeit
from collections import Counter
WORD = re.compile(r'\w+')
def get_cosine(vec1, vec2):
intersection = set(vec1.keys()) & set(vec2.keys())
numerator = sum([vec1[x] * vec2[x] for x in intersection])
sum1 = sum([vec1[x] ** 2 for x in vec1.keys()])
sum2 = sum([vec2[x] ** 2 for x in vec2.keys()])
denominator = math.sqrt(sum1) * math.sqrt(sum2)
if not denominator:
return 0.0
else:
return float(numerator) / denominator
def text_to_vector(text):
words = WORD.findall(text)
return Counter(words)
class VectorResult(object):
def __init__(self, cosine, text_1, text_2):
self.cosine = cosine
self.text_1 = text_1
self.text_2 = text_2
def __eq__(self, other):
if self.cosine == other.cosine:
return True
return False
def __le__(self, other):
if self.cosine <= other.cosine:
return True
return False
def __ge__(self, other):
if self.cosine >= other.cosine:
return True
return False
def __lt__(self, other):
if self.cosine < other.cosine:
return True
return False
def __gt__(self, other):
if self.cosine > other.cosine:
return True
return False
def main():
start = timeit.default_timer()
texts = []
with open('data.txt', 'r') as f:
texts = f.readlines()
cosmap = []
i = 0
out = []
while i < len(texts):
max_cosine = 0.0
current = None
for p in range(i + 1, len(texts)):
if texts[i] in out or texts[p] in out:
continue
vector1 = text_to_vector(texts[i])
vector2 = text_to_vector(texts[p])
cosine = get_cosine(vector1, vector2)
if cosine > max_cosine:
current = VectorResult(cosine, texts[i], texts[p])
max_cosine = cosine
if current:
out.extend([current.text_1, current.text_2])
cosmap.append(current)
i += 1
cosmap = sorted(cosmap)
for item in reversed(cosmap):
print(item.cosine, item.text_1, item.text_2)
end = timeit.default_timer()
print("Similarity Sorting of {} strings lasted {} s.".format(len(texts), end - start))
if __name__ == '__main__':
main()
<强>结果
我在http://pastebin.com/hySkZ4Pn使用了你的光盘地址作为测试数据:
1.0000000000000002 NO 15& 16 1ST FLOOR,2ND MAIN ROAD,KHB COLONY,GANDINAGAR YELAHANKA
NO 15& 16 1ST FLOOR,2ND MAIN ROAD,KHB COLONY,GANDINAGAR YELAHANKA
1.0 # 51/3 AGRAHARA YELAHANKA
#51/3 AGRAHARA YELAHANKA
0.9999999999999999 # C M C ROAD,YALAHANKA
# C M C ROAD,YALAHANKA
0.8728715609439696 # 1002/B B B ROAD,YELAHANKA
0,B B ROAD,YELAHANKA
0.8432740427115678 # LAKSHMI COMPLEX C M C ROAD,YALAHANKA
# SRI LAKSHMAN COMPLEX C M C ROAD,YALAHANKA
0.8333333333333335 # 85/1 B B M P OFFICE ROAD,KOGILU YELAHANKA
#85/1 B B M P OFFICE NEAR KOGILU YALAHANKA
0.8249579113843053 # 689 3RD A CROSS SHESHADRIPURAM CALLEGE OPP YELAHANKA
# 715 3RD CROSS A SECTUR SHESHADRIPURAM CALLEGE OPP YELAHANKA
0.8249579113843053 # 10 RAMAIAIA COMPLEX B B ROAD,YALAHANKA
# JAMATI COMPLEX B B ROAD,YALAHANKA
[ SNIPPED ]
Similarity Sorting of 702 strings lasted 8.955146235887025 s.
答案 1 :(得分:1)
余弦相似性绝对是要走的路。
Igor Motov创建了一个Elasticsearch本机脚本,用于计算跨多个文档的字段的相似度值。
您可以在script_score
或script-based sorting内使用此脚本。