-
Notifications
You must be signed in to change notification settings - Fork 4
/
search_province.py
67 lines (57 loc) · 1.98 KB
/
search_province.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
from flask import Flask, request, make_response, render_template, flash, redirect, url_for
import pandas as pd
import json
class Node:
def __init__(self):
self.next = {}
self.depth = 0
self.words = set()
self.last = False
class WordTrie:
def __init__(self, words):
self.root = Node()
for word in words:
self.insert(word)
def insert(self, word):
n = self.root
for w in word:
if w not in n.next:
n.next[w] = Node()
n.next[w].depth = n.depth + 1
n.words.add(word[n.depth:])
n = n.next[w]
n.last = True
class AutoComplete(WordTrie):
def __init__(self, words, data):
super().__init__(words)
self.data = data
def suggest(self, keyword):
n = self.root
for k in keyword:
if k not in n.next: return []
n = n.next[k]
suggestions = []
if n.last:
pos = self.data[self.data.name == keyword]['pos'].squeeze()
suggestions.append([keyword,pos])
for word in n.words:
query = keyword + word
pos = self.data[self.data.name == query]['pos'].squeeze()
if len(suggestions)< 10:
suggestions.append([query,pos])
else:
break
output = dict()
for i in range(len(suggestions)):
data_one = { 'name' : suggestions[i][0], # 장소이름
'lat' : float(suggestions[i][1].split(',')[0][1:].strip()), # 위도
'lng' : float(suggestions[i][1].split(',')[1][:-1].strip())} # 경도
output[i] = data_one
return output
class Search():
def __init__(self):
self.data = pd.read_csv('db/data/province_info_final.csv')
self.places = self.data.name
self.search = AutoComplete(self.places, self.data)
def suggest(self, keyword):
return self.search.suggest(keyword)