Skip to content

Commit

Permalink
Abstracted code out of member_party_list and search_results and sourc…
Browse files Browse the repository at this point in the history
…e_images templates.
  • Loading branch information
aaronrudkin committed Sep 21, 2018
1 parent 548f904 commit 6916f91
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 84 deletions.
22 changes: 22 additions & 0 deletions model/searchAssemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def assembleSearch(q, nextId, bottle):
elif party["count"] > 100:
party["scoreMatch"] += 10
party["seo_name"] = slugify(party["fullName"])
party["min_year"] = congressToYear(party["minCongress"], 0)
party["max_year"] = congressToYear(party["maxCongress"], 1)

resultParties.append(party)
resultParties.sort(key=lambda x: (-x["scoreMatch"], -x["maxCongress"]))

Expand Down Expand Up @@ -236,6 +239,12 @@ def assembleSearch(q, nextId, bottle):
member["bioImg"] = str(member["icpsr"]).zfill(6)+".jpg"
member["minElected"] = congressToYear(member["congresses"][0][0], 0)
member["seo_name"] = slugify(member["bioname"])

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]

member["state"] = member["state"].replace("(", "").replace(")", "")

resultMembers.append(member)

if needScore:
Expand All @@ -251,6 +260,10 @@ def assembleSearch(q, nextId, bottle):
bottle.response.headers["rollcall_number"] = 0
bottle.response.headers["member_number"] = len(resultMembers)
bottle.response.headers["party_number"] = 0

if len(resultMembers) > 50:
resultMembers = resultMembers[:50]

out = bottle.template("views/search_results", rollcalls = [], errormessage="", resultMembers=resultMembers, resultParties=[])
return out

Expand Down Expand Up @@ -363,6 +376,15 @@ def assembleSearch(q, nextId, bottle):
bottle.response.headers["rollcall_number"] = -999
bottle.response.headers["member_number"] = 0
bottle.response.headers["nextId"] = 0

remainder = 50
if len(resultParties) > 4:
resultParties = resultParties[:4]
remainder -= len(resultParties)

if len(resultMembers) > remainder:
resultMembers = resultMembers[:remainder]

out = bottle.template("views/search_results", rollcalls = [], errormessage=res["errormessage"], resultMembers=resultMembers, resultParties=resultParties)
else:
if "fulltextSearch" in res:
Expand Down
49 changes: 49 additions & 0 deletions model/search_results.py
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
25 changes: 4 additions & 21 deletions views/member_party_list.tpl
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
59 changes: 6 additions & 53 deletions views/search_results.tpl
Original file line number Diff line number Diff line change
@@ -1,53 +1,7 @@
% rcSuffix = lambda n: "%d%s" % (n,"tsnrhtdd"[(n/10%10!=1)*(n%10<4)*n%10::4])
% import re
% from stemming.porter2 import stem
% from model.search_results import do_highlight
% include('member_party_list.tpl', resultMembers=resultMembers, resultParties=resultParties)

% orgMapping = {"CQ": "Congressional Quarterly", "GOV": "Congress.gov", "VV": "Voteview Staff", "wikipedia": "Wikipedia"}

% def doHighlight(highlighter, text):
% stopwords = [x.strip() for x in open("model/stop_words.txt","r").read().split("\n")]
% if not len(highlighter):
% return text
% end
% words = highlighter.split()
% stemSet = []
% reSet = r"("+highlighter+")"
% for word in words:
% if len(word)>2:
% reSet += "|("+word+")"
% if stem(word)!=word:
% stemSet.append(stem(word))
% end
% end
% end
% for stemS in stemSet:
% if len(stemS)>2:
% reSet += "|("+stemS+")"
% end
% end
% spans = [m for m in re.finditer(reSet, text, re.I)]
% newS = ""
% last = 0
% for s in spans:
% if s.lastindex==1:
% ternary = ""
% elif s.lastindex<=1+len(stemSet):
% ternary = "2"
% else:
% ternary = "3"
% end
% if not text[s.start():s.end()].lower() in stopwords:
% newS += text[last:s.start()] + '<span class="searchHighlight'+(ternary)+'">'+text[s.start():s.end()]+'</span>'
% else:
% newS += text[last:s.start()] + ' '+text[s.start():s.end()]
% end
% last = s.end()
% end
% newS += text[last:]
% return newS
% end

% for rollcall in rollcalls:
<div class="panel panel-default">
<div class="panel-heading">
Expand All @@ -65,11 +19,12 @@
</strong>
on <abbr title="Date"><a href="/search/?fromDate={{rollcall["date"]}}&toDate={{rollcall["date"]}}">{{ rollcall["date"] }}</a></abbr>
<span class="pull-right">
<a href="/rollcall/{{ rollcall["id"] }}"><img src="/static/img/graph.png" class="viewVote"
data-toggle="tooltip" data-placement="bottom" title="View Vote"></a>
<a href="/rollcall/{{ rollcall["id"] }}"><img src="/static/img/graph.png" class="viewVote"
data-toggle="tooltip" data-placement="bottom" title="View Vote"></a>

<input type="checkbox" name="ids" value="{{ rollcall["id"] }}">
<img src="/static/img/export.png" class="exportVote" data-toggle="tooltip" data-placement="bottom" title="Export Vote" onclick="javascript:checkBox('{{rollcall["id"]}}');">
<img src="/static/img/export.png" class="exportVote"
data-toggle="tooltip" data-placement="bottom" title="Export Vote" onclick="javascript:checkBox('{{rollcall["id"]}}');">
</span>
</div>
<a href="/rollcall/{{rollcall["id"]}}" class="nohover">
Expand Down Expand Up @@ -105,9 +60,7 @@ data-toggle="tooltip" data-placement="bottom" title="View Vote"></a>
% if rollcall["question"]:
<p><strong>Question</strong>: {{ rollcall["question"] }}</p>
% end
<p><strong>Description</strong>: {{!doHighlight(highlighter, " ".join(rollcall["text"].split()[0:50])) }}</p>


<p><strong>Description</strong>: {{!do_highlight(highlighter, " ".join(rollcall["text"].split()[0:50])) }}</p>

% debug = False
% if "score" in rollcall and debug:
Expand Down
10 changes: 0 additions & 10 deletions views/source_images.tpl
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
% from __future__ import print_function

% rebase('base.tpl')
% import json
% include('header.tpl')



<div id='page-info'
data-publication="{{publication}}"
data-file-number="{{str(file_number).zfill(3)}}"
data-page-number="{{page_number}}"
data-num-leafs="{{num_leafs}}"
></div>






<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="apple-mobile-web-app-capable" content="yes">

Expand Down

0 comments on commit 6916f91

Please sign in to comment.