Skip to content

Commit

Permalink
Etymology sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
lmrodriguezr committed Jul 27, 2024
1 parent 1069bfa commit 45160f2
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 63 deletions.
11 changes: 11 additions & 0 deletions app/controllers/names_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,17 @@ def draft
render(:index)
end

# GET /names/etymology_sandbox
def etymology_sandbox
@name = Name.new(name: params[:name] || '')
end

# GET /names/syllabify?name=Abc
def syllabify
@name = Name.new(name: params[:name] || '')
@syllabification = @name.guess_syllabication
end

# GET /names/1
# GET /names/1.json
# GET /names/1.pdf
Expand Down
43 changes: 35 additions & 8 deletions app/javascript/packs/dictionary.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
const perseus = "https://www.perseus.tufts.edu/hopper/";

function flash_modal(dict_id, target) {
var modal = $("#" + dict_id);
const tgt_classes = "text-white bg-info";
target.addClass(tgt_classes);
modal.fadeOut(100, function() {
modal.fadeIn(800, function() { target.removeClass(tgt_classes); });
});
}

function dictionary_search(dict_id, comp) {
var select = "[data-behavior='dictionary'][data-id='" + dict_id + "']";
var modal_body = $("#" + dict_id + " div.modal-body");
Expand Down Expand Up @@ -43,7 +52,13 @@ function dictionary_search_grammar(dict_id, comp, modal_body) {
var list = $('<ul></ul>');
var lemmata = [];
var texts = [];
var help = $('<p>Click on a lemma (words in parenthesis) to see its ' +
'definition, or click "use" to copy the grammar to the ' +
'appropriate box in the etymology table</p>');
help.addClass("text-muted small border-left px-2 mx-2");

if ($(data).find("analyses analysis").length == 0) {
help = '';
list = $('<span class="text-danger ml-4">Term not found</span>');
} else {
$(data).find("analyses analysis").each(function() {
Expand All @@ -60,9 +75,8 @@ function dictionary_search_grammar(dict_id, comp, modal_body) {
if (i == "case" & val != "gen") return;
if (i == "pos" & val == "noun") val = "n";
text = text + " " + val;
if (i != "dialect") text = text + ".";
if (i != "dialect" & val != "verb") text = text + ".";
});
if (texts.includes(text)) return;
texts.push(text);
var item = $("<li></li>");
var lemma_anchor = $(
Expand All @@ -77,7 +91,9 @@ function dictionary_search_grammar(dict_id, comp, modal_body) {
var anchor = $("<span type=button class='badge badge-pill " +
"badge-primary ml-2'>use</span>");
anchor.on("click", function() {
$(select + " #name_etymology_" + comp + "_grammar").val(text);
var target = $(select + " #name_etymology_" + comp + "_grammar");
target.val(text);
flash_modal(dict_id, target);
});

// Build item and add to list
Expand All @@ -91,7 +107,8 @@ function dictionary_search_grammar(dict_id, comp, modal_body) {
});
}
modal_body.children('.analyses').html('<h2>Grammatical analyses</h2>');
modal_body.children('.analyses').append(list);
modal_body.children(".analyses").append(help);
modal_body.children(".analyses").append(list);
if (!lemmata.includes(part)) lemmata.push(part);
dictionary_search_definition(dict_id, comp, modal_body, lemmata[0], 1);
}).fail(function() {
Expand Down Expand Up @@ -139,6 +156,10 @@ function dictionary_search_definition(dict_id, comp, modal_body, part, src) {
'<label for="dictionary-title-src" class="input-group-text">' +
'<i class="fas fa-exchange-alt"> </i></label></div>'
);
var help = $('<p>Click on "use" to copy the definition to the appropriate ' +
'box in the etymology table, or see the full formatted ' +
'definition for the term below in the grey box</p>');
help.addClass("text-muted small border-left px-2 mx-2");

// Definition
$.ajax({
Expand All @@ -155,6 +176,7 @@ function dictionary_search_definition(dict_id, comp, modal_body, part, src) {
// Parse definitions
var cont = modal_body.children('.definition');
cont.html(title);
cont.append(help);
var list = $('<ul class="mb-3"></ul>');
result.find("div.lex_sense > i").each(function() {
var description = $(this).text();
Expand All @@ -163,7 +185,9 @@ function dictionary_search_definition(dict_id, comp, modal_body, part, src) {
var anchor = $("<span type=button class='badge badge-pill " +
"badge-primary ml-2'>use</span>");
anchor.on("click", function() {
$(select + " #name_etymology_" + comp + "_description").val(description);
var target = $(select + " #name_etymology_" + comp + "_description");
target.val(description);
flash_modal(dict_id, target);
});
item.append(description);
item.append(anchor);
Expand All @@ -189,10 +213,12 @@ function dictionary_search_button(dict_id, comp) {
var td = $(select + " #name_etymology_" + comp + "_dict");
td.html('');
var latin = [
"", "L", "L.", "Latin", "N.L.", "NL", "NL.", "Neolatin", "Neo Latin",
"L", "L.", "Latin", "N.L.", "NL", "NL.", "Neolatin", "Neo Latin",
"Neo-Latin",
// The dictionary includes Greek entries
"Greek", "Gr."
"Greek", "Gr.",
// We might as well try if the language is blank
""
];
var lang = $(select + " #name_etymology_" + comp + "_lang").val();
if (!latin.includes(lang)) return;
Expand All @@ -217,10 +243,11 @@ $(document).on("turbolinks:load", function() {
$("[data-behavior='dictionary']").each(function() {
console.log('Initializing Dictionary Lookup');
var dict_id = $(this).data("id");
var select = "[data-behavior='dictionary'][data-id='" + dict_id + "']";
components.forEach(function(comp_i) {
fields.forEach(function(field_i) {
var id = "name_etymology_" + comp_i + "_" + field_i;
$("#" + id).on("change", function() {
$(select + " #" + id).on("change", function() {
dictionary_search_button(dict_id, comp_i);
});
});
Expand Down
4 changes: 2 additions & 2 deletions app/models/name/etymology.rb
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,8 @@ def guess_syllabication(word = last_epithet)
end

# Propose emphasis
if syllables.size == 1
# No accent for monosyllabic words
if syllables.size <= 1
# Ignore empty words, and no accent for monosyllabic words
elsif word =~ /(aceae|ia|icus|iae|icola|ium)$/
syllables[-3] += "'"
else
Expand Down
38 changes: 38 additions & 0 deletions app/views/names/_etymology_table.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<% dict_id = modal('Dictionary lookup', size: 'lg') do %>
<% end %>
<table class="table table-hover table-responsive-lg my-4"
data-behavior="dictionary" data-id="<%= dict_id %>">
<thead>
<th>Component</th>
<th>Language</th>
<th>Grammar</th>
<th>Particle</th>
<th>Description or translation</th>
<th></th>
</thead>
<tbody>
<%
components = {
p1: '1st morpheme', p2: '2nd morpheme', p3: '3rd morpheme',
p4: '4th morpheme', p5: '5th morpheme', xx: 'Full word'
}
fields = { lang: 5, grammar: 15, particle: 15, description: 30 }
%>
<% components.each do |k, v| %>
<tr class='<%= 'table-active' if k == :xx %>'>
<td><b><%= v %></b></td>
<% fields.each do |i, size| %>
<% entry = :"etymology_#{ k }_#{ i }" %>
<td>
<% if i == :particle && k == :xx %>
<%= content_tag(:i, name.last_epithet, id: "name_#{entry}") %>
<% else %>
<%= form.input(entry, input_html: { size: size }, label: false) %>
<% end %>
</td>
<% end %>
<td id="name_etymology_<%= k %>_dict"></td>
</tr>
<% end %>
</tbody>
</table>
52 changes: 1 addition & 51 deletions app/views/names/edit_etymology.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -38,57 +38,7 @@
</div>
<% end %>
<br/>
<% dict_id = modal('Dictionary lookup', size: 'lg') do %>
<% end %>
<table class="table table-hover table-responsive-lg"
data-behavior="dictionary" data-id="<%= dict_id %>">
<thead>
<th>Component</th>
<th>Language</th>
<th>Grammar</th>
<th>Particle</th>
<th>Description or translation</th>
<th></th>
</thead>
<tbody>
<%
components = {
p1: '1st morpheme', p2: '2nd morpheme', p3: '3rd morpheme',
p4: '4th morpheme', p5: '5th morpheme', xx: 'Full word'
}
fields = { lang: 5, grammar: 15, particle: 15, description: 30 }
%>
<% components.each do |k, v| %>
<tr class='<%= 'table-active' if k == :xx %>'>
<td><b><%= v %></b></td>
<% fields.each do |i, size| %>
<% entry = :"etymology_#{ k }_#{ i }" %>
<td>
<% if i == :particle && k == :xx %>
<%= content_tag(:i, @name.last_epithet, id: "name_#{entry}") %>
<% else %>
<%= f.input entry, input_html: { size: size }, label: false %>
<% end %>
</td>
<% end %>
<td id="name_etymology_<%= k %>_dict"></td>
</tr>
<% end %>
</tbody>
</table>
<br/>

<!--
<hr/>
<%=
f.input(
:etymology_text,
as: :rich_text_area,
label: 'Alternatively, provide a formatted etymology text (optional)'
)
%>
-->
<%= render(partial: 'etymology_table', locals: { form: f, name: @name }) %>
<br/>

<% if @tutorial %>
Expand Down
45 changes: 45 additions & 0 deletions app/views/names/etymology_sandbox.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<h1>Etymology sandbox</h1>
<%= simple_form_for(@name) do |f| %>
<div class="rounded border p-3 my-4 bg-light">
<p>
The etymology sandbox is designed to provide all the tools available to
you in the etymology table of names, without having to create or modify
a name in the system.
</p>
<p>
To get started, simply write the Latin or Latinized name in the field
below, which should trigger an automated proposal of the syllabification.
Continue by filling up the etymology table below, including the usage of
the dictionary lookups.
</p>
<hr/>
<%= help_topic('etymology',
'How do I fill the etymology table?', size: 'xl') %>
<%= help_topic('dictionary',
'How do I use dictionary lookups?', size: 'xl') %>
</div>

<%= f.input(:name) %>
<%= f.input(:syllabification, label: 'Inferred syllabification') %>
<% @name.name = '...' %>
<%= render(partial: 'etymology_table', locals: { form: f, name: @name }) %>
<% end %>

<script>
$(document).on("turbolinks:load", function() {
$('#name_name').on("keyup", function() {
var name = $(this).val();
var part = $("#name_etymology_xx_particle");
var syll = $("#name_syllabification");

$.ajax({
url: "<%= syllabify_names_url(format: :json) %>", data: { name: name }
}).done(function(data) {
syll.val(data["syllabification"]);
part.html(data["last_epithet"]).val(data["last_epithet"]);
});
});
$("#name_name").trigger("keyup");
});
</script>

6 changes: 6 additions & 0 deletions app/views/names/syllabify.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
json.response do
json.status 'ok'
json.message_type 'syllabify'
end
json.syllabification(@syllabification)
json.(@name, :last_epithet)
5 changes: 4 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,11 @@
get :draft
get :user
get :observing
get :autocomplete
get :linkout
# --> User utilities
get :autocomplete
get :etymology_sandbox
get :syllabify
# --> Curator utilities
get :unranked
get :unknown_proposal
Expand Down
2 changes: 1 addition & 1 deletion documentation

0 comments on commit 45160f2

Please sign in to comment.