-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearcher.py
35 lines (24 loc) · 906 Bytes
/
searcher.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
import numpy as np
import csv
class Searcher:
def __init__(self, indexPath):
# Store index.csv path
self.indexPath = indexPath
def search(self, queryFeature, limit = 10):
# initialize directory of results
results = {}
# open the index file for reading
with open(self.indexPath) as f:
#initiate the CSV reader
reader = csv.reader(f)
for row in reader:
features = [float(x) for x in row[1:]]
d = self.chi2_distance(features, queryFeature)
results[row[0]] = d
f.close()
results = sorted([(v, k) for (k, v) in results.items()])
return results[:limit]
def chi2_distance(self, histA, histB, eps = 1e-10):
d = 0.5 * np.sum([((a - b) ** 2) / (a + b + eps)
for (a,b) in zip(histA, histB)])
return d