Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions es.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import csv
from typing import Dict, List, Tuple
from collections import OrderedDict
import pandas as pd
#when you load this pacakge these global variables are defined
#es = Elasticsearch('http://localhost:9200')
# es = Elasticsearch(
Expand Down Expand Up @@ -89,7 +90,7 @@ def add_to_index(filepath:str) -> None:
filepath (str): a filepath to a txt file general plan
"""

i = get_max_index()
i = get_max_index()

try:
filename = os.path.basename(filepath)
Expand Down Expand Up @@ -118,6 +119,7 @@ def index_everything():
"""Adds all of the txt files in the data directory to the elasticsearch index
"""
global es
global index_to_info_map
wd = os.getcwd()
data_dir = os.path.join(wd, 'static', 'data', 'places')
filepaths = glob.glob(data_dir+'/*.txt')
Expand All @@ -138,9 +140,22 @@ def index_everything():
hash_to_prop_mapping[keyhash] = parsed_filename
es.index(index='test_4', id=keyhash, body={'text': txt, 'filename': filename}, )
i += 1

with open('key_hash_mapping.json', 'w') as fp:
json.dump(hash_to_prop_mapping, fp)
create_csv()
index_to_info_map = None

def create_csv():
with open('key_hash_mapping.json', 'r') as key_hash:
json_df = pd.read_json(key_hash, orient='index')

json_df = json_df.drop(json_df.columns[[0, 1, 5]], axis=1)
json_df.rename(columns={"is_city": "city/county"}, inplace=True)
json_df = json_df.replace({'city/county':{True:'city', False:'county'}})

path_for_csv = 'static/data/names-and-years-in-database.csv'
json_df.to_csv(path_for_csv)
print(".csv file saved!")

def elastic_search(query) -> Tuple[List[int], List[float]]:
"""Puts a query into elasticsearch and returns the ids and score
Expand Down Expand Up @@ -211,6 +226,7 @@ def map_index_to_vals(search_result_indices, key_to_hash_path='key_hash_mapping.
else:
my_dict = index_to_info_map

# print(index_to_info_map)
return list(map(lambda x:my_dict[str(x)], search_result_indices))

def elastic_search_highlight(query):
Expand Down
6 changes: 5 additions & 1 deletion templates/upload_index.html
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,11 @@ <h2>Upload <em>file</em></h2>
<input style="width:80px;background: orangered;color: white;font-size: 2.5vh;"type = "submit"/>
</div>
</form>
</center>
</center>
<div id="download_csv" style="color:white;">
Download a .csv file with all city/county names and associated years listed in our database.</br>
<a href="../static/data/names-and-years-in-database.csv" download style="width:80px;background: orangered;color: white;font-size: 2.5vh;">Download</a>
</div>
</div>
<div class="caption" id="delete" >
<div class="grow">
Expand Down
2 changes: 1 addition & 1 deletion textsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ def index_search_box():
resultsDiv = Div(text = """
<span class='darker-text-color'>{} </span><span class='white-text-color'>out of </span><span class='darker-text-color'>{} </span><span class='white-text-color'>cities mention </span><span class='darker-text-color'>'{}'</span><br/><br/>
<span class='darker-text-color'>{} </span><span class='white-text-color'>out of </span><span class='darker-text-color'>{} </span><span class='white-text-color'>counties mention </span><span class='darker-text-color'>'{}'</span>
""".format(uniqueCities, numCities, wordinput, uniqueCounties, numCounties, wordinput),
""".format(uniqueCities, numCities, twitQuery, uniqueCounties, numCounties, twitQuery),
margin = (40, 0, 20, 30),
css_classes=["results-div"])

Expand Down