prince_corpus route

develop
gwen 2 years ago
parent 0aadc12ee7
commit 8e5d857d09

@ -48,10 +48,27 @@ def extract_princes_in_houses():
princes_in_houses = dict() princes_in_houses = dict()
for house in housecol.find(): for house in housecol.find():
housename = house['name'].lower() housename = house['name'].lower()
query = list(actecol.aggregate([{"$match": {"house": housename}}, {'$group': {'_id': {'prince_name': '$prince_name', 'prince_code': '$prince_code'}}}])) query = list(actecol.aggregate([{
"$match": {"house": housename}},
{'$group': {'_id': {'prince_name': '$prince_name', 'prince_code': '$prince_code'}}
}]))
princes_in_houses[housename] = [pr['_id'] for pr in query] princes_in_houses[housename] = [pr['_id'] for pr in query]
return princes_in_houses return princes_in_houses
def extract_prince_in_house(house, prince_code):
"""
:param house: sample: 'bourbon'
:param prince_code: sample: 'lo_i'
:result: sample: {'prince_name': "Louis Ier d'Anjou", 'prince_code': 'lo_i'}
"""
princes_in_houses = extract_princes_in_houses()
princes_in_house = princes_in_houses[house]
for pr in princes_in_house:
if pr['prince_code'] == prince_code:
return pr
def normalize_trigrams(trigram): def normalize_trigrams(trigram):
"""normalizes names, usefull for the uris routes """normalizes names, usefull for the uris routes
@ -62,37 +79,23 @@ def normalize_trigrams(trigram):
return {unidecode(value):key for key, value in trigram.items()} return {unidecode(value):key for key, value in trigram.items()}
# ______________________________________________________________________________ # ______________________________________________________________________________
# in memory storage extracted meta informations on the database # in memory storage extracted 'meta' informations upon the database
# TODO: if it takes too much time at launch, put it in something like # TODO: if it takes too much time at launch, put it in something like
# a `flask init` procedure # a `flask init` procedure
helpers_dicts = helpers.find_one() helpers_dicts = helpers.find_one()
house_trigram = helpers_dicts["house_trigram"] house_trigram = helpers_dicts["house_trigram"]
prince_bigram = helpers_dicts["prince_bigram"] prince_bigram = helpers_dicts["prince_bigram"]
princes_in_houses = extract_princes_in_houses() princes_in_houses = extract_princes_in_houses()
# normalized_trigrams
"""
house_trigram sample:
{'Alencon': 'alb', 'Anjou': 'anj', 'Armagnac': 'arm', 'Bourbon': 'brb', 'Bretagne': 'bre', 'Bourgogne': 'brg', 'Berry': 'bry', 'Orleans': 'orl', 'Savoie': 'sav'}
"""
house_trigram = normalize_trigrams(house_trigram) house_trigram = normalize_trigrams(house_trigram)
"""
prince_bigram sample:
{'Agnes': 'agn', 'Alain': 'al', 'Amedee': 'am', 'Anne': 'ann', 'Arthur': 'ar', 'Bernard': 'be', 'Beatrice': 'bea', 'Blanche': 'bla', 'Bonne': 'bon', 'Catherine': 'cat', 'Charles': 'ch', 'Edouard': 'ed', 'Francois': 'fr', 'Francoise': 'fra', 'Jean': 'je', 'Jeanne': 'jea', 'Louis': 'lo', 'Marguerite': 'mag', 'Marie': 'mar', 'Philippe': 'ph', 'Pierre': 'pi', 'Rene': 're', 'Isabelle': 'isa', 'Yolande': 'yol'}
"""
prince_bigram = normalize_trigrams(prince_bigram) prince_bigram = normalize_trigrams(prince_bigram)
def bigram_prince(prince):
"Translates Charles_i -> ch_i"
name, number = prince.split("_")
return prince_bigram[name] + "_" + number
# TODO: write tests in the datascience project `datascience/tests`
#print(bigram_prince("Agnes"))
#print(bigram_prince("Arthur"))
#print(bigram_prince("Bernard"))
#print(trigram_house("Anjou"))
#print(trigram_house("Orleans"))
def trigram_house(house):
return house_trigram[house]
def make_acteid_from_route(house=None, prince=None, date_and_item=None):
"/acte/Anjou/Isabelle_i/1441_08_05a -> anj_isa_i_1441_08_05a"
return "_".join([trigram_house(house), bigram_prince(prince), date_and_item])

@ -1,26 +1,56 @@
"helper functions" """helper functions (is it really usefull?)
from flask import abort TODO: maybe all these calculations are to be put in the db storage
"""
def find_one_or_404(collection, **kwargs):
"""Find a single document or raise a 404. def inverted_prince_bigram(bigram):
"Translates ch -> Charles"
This is like :meth:`~pymongo.collection.Collection.find_one`, but inverted_prince_bigram = {value: key for key, value in prince_bigram.items()}
rather than returning ``None``, cause a 404 Not Found HTTP status return inverted_prince_bigram[bigram]
on the request.
.. code-block:: python def bigram_prince(prince):
"Translates Charles_i -> ch_i"
usercollection = mydb['usercollection'] name, number = prince.split("_")
return prince_bigram[name] + "_" + number
@app.route("/user/<username>")
def user_profile(username):
userfound = find_one_or_404(usercollection, {"_id": username}) def trigram_house(house):
return render_template("user.html", return house_trigram[house]
user=userfound)
"""
found = collection.find_one(**kwargs) def make_acteid_from_route(house=None, prince=None, date_and_item=None):
if found is None: "/acte/Anjou/Isabelle_i/1441_08_05a -> anj_isa_i_1441_08_05a"
abort(404) return "_".join([trigram_house(house), bigram_prince(prince), date_and_item])
return found
def make_timeitem_from_filename(filename):
"""
"anj_isa_i_1441_08_05a" -> "1441_08_05a"
"""
trs_fname = filename.split('_')
return "_".join(trs_fname[3:])
#from flask import abort
#
#def find_one_or_404(collection, **kwargs):
# """Find a single document or raise a 404.
#
# This is like :meth:`~pymongo.collection.Collection.find_one`, but
# rather than returning ``None``, cause a 404 Not Found HTTP status
# on the request.
#
# .. code-block:: python
#
# usercollection = mydb['usercollection']
#
# @app.route("/user/<username>")
# def user_profile(username):
# userfound = find_one_or_404(usercollection, {"_id": username})
# return render_template("user.html",
# user=userfound)
# """
# found = collection.find_one(**kwargs)
# if found is None:
# abort(404)
# return found

@ -1,6 +1,6 @@
"""main flask app routes """main flask app routes handlers
There are two types of routes, first the static routes : There are two types of routes, first the 'static' routes :
- / - /
- /about - /about
@ -9,7 +9,7 @@ There are two types of routes, first the static routes :
- /privacy - /privacy
- /termsofservice - /termsofservice
Then the dynamic routes : Then the 'dynamic' (calculated) routes :
- /actes - /actes
- /actes/<house> - /actes/<house>
@ -23,8 +23,8 @@ import typing as t
from flask import Blueprint, abort, render_template, request, send_from_directory from flask import Blueprint, abort, render_template, request, send_from_directory
import folium import folium
from pymongo import ASCENDING
from .helper import find_one_or_404 from .helper import *
from .dbinit import * from .dbinit import *
main = Blueprint("main", __name__, url_prefix="/") main = Blueprint("main", __name__, url_prefix="/")
@ -67,13 +67,6 @@ def actes(house):
""" """
# house in the store shall be in lower case, but let's force it, just in case # house in the store shall be in lower case, but let's force it, just in case
house = house.lower() house = house.lower()
# the nosql query below is equivalent to this code, which is more readable but slower:
#princes = []
#for act in actecol.find({"house":house}):
# prince_name = act['prince_name'].capitalize()
# prince_code = act['prince_code'].capitalize()
# if (prince_name, prince_code) not in princes:
# princes.append((prince_name, prince_code))
# sample result: # sample result:
# [('Louis II de Bourbon', 'lo_ii'), ('Anne Dauphine', 'ann_i'), ('Agnès de Bourgogne', 'agn_i'), ('Charles Ier de Bourbon', 'ch_i')] # [('Louis II de Bourbon', 'lo_ii'), ('Anne Dauphine', 'ann_i'), ('Agnès de Bourgogne', 'agn_i'), ('Charles Ier de Bourbon', 'ch_i')]
# [('Agnès de Bourgogne', 'agn_i'), ('Anne Dauphine', 'ann_i'), ('Charles Ier de Bourbon', 'ch_i'), ('Louis II de Bourbon', 'lo_ii') # [('Agnès de Bourgogne', 'agn_i'), ('Anne Dauphine', 'ann_i'), ('Charles Ier de Bourbon', 'ch_i'), ('Louis II de Bourbon', 'lo_ii')
@ -86,22 +79,40 @@ def actes(house):
@main.route("/actes/<house>/<prince>") # don't put a slash at the end @main.route("/actes/<house>/<prince>") # don't put a slash at the end
def prince_corpus(house=None, prince=None): def prince_corpus(house=None, prince=None):
"""copora prince, **timeline view**""" """copora prince, *timeline* view
"""
prince_code = prince.lower()
house = house.lower() house = house.lower()
# prince bigram -> prince_code prince_long_name = extract_prince_in_house(house, prince_code)['prince_name']
# sample uri: /actes/Anjou/lo_i -> Louis_i -> Louis Ier d'Anjou query = list(actecol.aggregate([
prc_big, prc_num = prince.split("_") {
prince_code = prince_bigram[prc_big] + "_" + prc_num "$match": {"house": house, "prince_code": prince_code}
# for item in prince_acte: },
# print("\n\n", item) {
#info = [(t.date_time, t.date, t.filename, t.analysis, t.prod_place_acte, "$sort": {"date_time": ASCENDING}
# t.diplo_type_acte, t.state_doc) },
# ['1418-12-20', '1418, 20 décembre', 'anj_yo_i_1418_12_20a', "Donation à Antoine de la Salle d'une maison à Arles", "Château d'Anger", 'Lettres patentes', 'Copie'] {
# ['1421-06-28', '1421, 28 juin', 'anj_yo_i_1421_06_28a', "Confirmation par Yolande, duchesse d'Anjou, du douaire assigné à sa belle-fille, Isabelle de Lorraine", 'NS', 'Lettres patentes', 'Original'] '$group': {'_id': {'prince_name': '$prince_name',
# ['1442-02-24', '1442 (n. st.), 24 février', 'anj_yo_i_1442_02_24a', 'Pierre Throvan, secrétaire de la reine, nommé trésorier général de Provence et de Languedoc du 16 juillet au 31 octobre 1441, puis pendant trois ans à partir du 1er novembre 1441', 'Château de Saumur', 'Lettres patentes', 'Copie'] 'prince_code': '$prince_code',
return render_template("prince_corpus.html", houseS=house, duke_name=prince_name, 'filename': '$filename',
lst_id=prince_acte) 'date': '$date',
"date_time": "$date_time",
"analysis": '$analysis',
"place": "$place.name",
#"ref_acte": "$ref_acte"
"diplo_state": "$diplo_state",
"diplo_type": "$diplo_type"
}
}
}
]))
transformed_query = [pr['_id'] for pr in query]
# constructing the dateitem
for trs in transformed_query:
trs['timeitem'] = make_timeitem_from_filename(trs['filename'])
return render_template("prince_corpus.html", house=house, duke_name=prince_long_name,
prince_code=prince_code.capitalize(), actes=transformed_query)
@main.route("/acte/<house>/<prince>/<dateitem>") # don't put a slash at the end @main.route("/acte/<house>/<prince>/<dateitem>") # don't put a slash at the end
def acte(house=None, prince=None, dateitem=None): def acte(house=None, prince=None, dateitem=None):

@ -4,25 +4,25 @@
<nav aria-label="breadcrumb"> <nav aria-label="breadcrumb">
<ol class="breadcrumb"> <ol class="breadcrumb">
<li class="breadcrumb-item"><a href="{{url_for('main.corpora_all')}}">Corpus</a></li> <li class="breadcrumb-item"><a href="{{url_for('main.corpora_all')}}">Corpus</a></li>
<li class="breadcrumb-item"><a href="{{url_for('main.actes', house=houseS)}}">{{houseS}}</a></li> <li class="breadcrumb-item"><a href="{{url_for('main.actes', house=house)}}">{{house | capitalize}}</a></li>
<li class="breadcrumb-item active" aria-current="page">{{duke_name[0][0]}}</li> <li class="breadcrumb-item active" aria-current="page">{{duke_name}}</li>
</ol> </ol>
</nav> </nav>
<h1 style="text-align: center; padding-bottom: 5%;">Actes de {{duke_name[0][0]}}</h1> <h1 style="text-align: center; padding-bottom: 5%;">Actes de {{duke_name}}</h1>
<div class="timeline-princier"> <div class="timeline-princier">
{% for id in lst_id %} {% for acte in actes %}
<div class="entry-princier"> <div class="entry-princier">
<div class="title-princier"> <div class="title-princier">
<h3>{{id[1]}}</h3> <h3>{{ acte['date'] }}</h3>
</div> </div>
<div class="pboly"> <div class="pboly">
<p class="chrono-p font-weight-bold"><a href="{{url_for('main.acte', prince=duke_name[0][1], acte_id=id[2])}}">{{id[3]}}.</a></p> <p class="chrono-p font-weight-bold"><a href="{{url_for('main.acte', house=house, prince=prince_code, dateitem=dateitem)}}">{{ acte['analysis'] }}.</a></p>
<p style="margin-bottom: 5%"> <p style="margin-bottom: 5%">
{% if id[4] != 'NS' %} {% if acte['place'] != 'NS' %}
<span class="badge badge-pill badge" style="background-color: #284AA7; color: white; font-size: small;">{{id[4]}}</span> <span class="badge badge-pill badge" style="background-color: #284AA7; color: white; font-size: small;">{{ acte['place'] }}</span>
{% endif %} {% endif %}
<span class="badge badge-pill badge" style="background-color: #A7288A; color: white; font-size: small;">{{id[6]}}</span> <span class="badge badge-pill badge" style="background-color: #A7288A; color: white; font-size: small;">{{ acte['diplo_state'] }}</span>
<span class="badge badge-pill badge" style="background-color: #A78528; color: white; font-size: small;">{{id[5]}}</span> <span class="badge badge-pill badge" style="background-color: #A78528; color: white; font-size: small;">{{ acte['diplo_type'] }}</span>
</p> </p>
</div> </div>
</div> </div>
@ -30,4 +30,4 @@
{% endfor %} {% endfor %}
</div> </div>
</div><!-- /.container --> </div><!-- /.container -->
{% endblock %} {% endblock %}

Loading…
Cancel
Save