-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Abstracted code out of member_party_list and search_results and sourc…
…e_images templates.
- Loading branch information
1 parent
548f904
commit 6916f91
Showing
5 changed files
with
81 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import re | ||
from stemming.porter2 import stem | ||
|
||
def do_highlight(highlighter, text): | ||
""" Takes a string and words to highlight. """ | ||
|
||
# Load stopwords to ignore. | ||
stopwords = [x.strip() for x in open("model/stop_words.txt","r").read().split("\n")] | ||
|
||
# Nothing to highlight, return text unchanged | ||
if not len(highlighter): | ||
return text | ||
|
||
# Split words | ||
words = highlighter.split() | ||
stem_list = [] | ||
re_query = r"(%s)" % highlighter | ||
for word in words: | ||
if len(word) > 2: | ||
re_query += "|(%s)" % word | ||
if stem(word) != word: | ||
stem_list.append(stem(word)) | ||
|
||
# Add stemmed versions of words. | ||
for stem_word in stem_list: | ||
if len(stem_word)>2: | ||
re_query += "|(%s)" % stem_word | ||
|
||
# Find all the matches | ||
spans = [m for m in re.finditer(re_query, text, re.I)] | ||
new_string = "" | ||
last = 0 | ||
|
||
# Run through the results and highlight them accordingly. | ||
for s in spans: | ||
ternary = "" if s.lastindex == 1 else "2" if s.lastindex <= 1 + len(stem_list) else "3" | ||
|
||
if not text[s.start():s.end()].lower() in stopwords: | ||
new_string += text[last:s.start()] + '<span class="searchHighlight%s">%s</span>' % (ternary, text[s.start():s.end()]) | ||
else: | ||
new_string += text[last:s.start()] + ' ' + text[s.start():s.end()] | ||
|
||
last = s.end() | ||
|
||
# Add the text after the last result. | ||
new_string += text[last:] | ||
|
||
# Return the modified string. | ||
return new_string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,29 @@ | ||
% if len(resultMembers) or len(resultParties): | ||
<div class="row"> | ||
|
||
% def congressToYear(cong): | ||
% return 1787+(2*cong) | ||
% end | ||
|
||
% i=0 | ||
% for party in resultParties: | ||
<div class="col-md-3 memberResultBox {{party["colorScheme"]}}" onClick="javascript:window.location='/parties/{{party["id"]}}/{{party["seo_name"]}}';"> | ||
% if "logo" in party or (party["id"]==100 or party["id"]==200): | ||
<img src="/static/img/parties/{{party["id"]}}.png" class="pull-left party_logo"> | ||
% end | ||
<div class="party_box"> | ||
<strong>{{party["fullName"]}}</strong><br/> | ||
Active from {{congressToYear(party["minCongress"])}} to {{congressToYear(party["maxCongress"])+1}}<br/> | ||
Active from {{party["min_year"]}} to {{party["max_year"]}}<br/> | ||
</div> | ||
</div> | ||
% i=i+1 | ||
% if i>=4: | ||
% break | ||
% end | ||
% end | ||
|
||
% for member in resultMembers: | ||
% if "bioname" in member and len(member["bioname"])>20 and "(" in member["bioname"]: | ||
% member["bioname"] = member["bioname"].split(",")[0] + ", " + member["bioname"].split("(")[1].split(")")[0] | ||
% end | ||
<a href="/person/{{member["icpsr"]}}/{{member["seo_name"]}}" class="nohover"> | ||
<div class="col-md-3 memberResultBox"> | ||
<img class="bio member_image pull-left" src="/static/img/bios/{{member["bioImg"]}}"> | ||
<div class="member_bio"> | ||
% if "bioname" in member and member["bioname"] is not None: | ||
<strong>{{member["bioname"]}}</strong> ({{member["party_name"][0:1]}})<br/> | ||
% end | ||
<img src="/static/img/states/{{member["state_abbrev"]}}.png" class="member_flag" /> {{member["state"].replace("(","").replace(")","")}}<br/> | ||
<img src="/static/img/states/{{member["state_abbrev"]}}.png" class="member_flag" /> {{member["state"]}}<br/> | ||
Elected {{member["minElected"]}} | ||
</div> | ||
</div> | ||
</a> | ||
% i=i+1 | ||
% if i>=50: | ||
% break | ||
% end | ||
% end | ||
|
||
</div><br/> | ||
% end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters