-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2291 from laws-africa/accented-terms
Sentence caser / accented terms
- Loading branch information
Showing
12 changed files
with
335 additions
and
1 deletion.
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,53 @@ | ||
import re | ||
|
||
import unicodedata | ||
from lxml import etree | ||
|
||
from indigo.plugins import LocaleBasedMatcher, plugins | ||
|
||
|
||
@plugins.register('sentence-caser') | ||
class BaseSentenceCaser(LocaleBasedMatcher): | ||
""" Sentence cases headings in a document. | ||
""" | ||
terms = None | ||
normalized_terms = None | ||
|
||
def sentence_case_headings_in_document(self, document): | ||
accented_terms = document.language.accented_terms.first() | ||
# allow tests to specify self.terms | ||
if not self.terms: | ||
self.terms = accented_terms.terms if accented_terms else [] | ||
self.terms.sort(key=lambda x: len(x), reverse=True) | ||
self.normalized_terms = [''.join(c for c in unicodedata.normalize('NFD', t) if unicodedata.category(c) != 'Mn').lower() | ||
for t in self.terms] | ||
root = etree.fromstring(document.content.encode('utf-8')) | ||
nsmap = {'a': root.nsmap[None]} | ||
for heading in root.xpath('//a:heading', namespaces=nsmap): | ||
self.capitalized = False | ||
skip_elements = heading.xpath(".//a:*[ancestor::a:authorialNote]", namespaces=nsmap) | ||
for elem in heading.iter(): | ||
if elem in skip_elements: | ||
continue | ||
elem.text = self.adjust_heading_text(elem.text) | ||
elem.tail = self.adjust_heading_text(elem.tail) | ||
|
||
document.content = etree.tostring(root, encoding='unicode') | ||
|
||
def adjust_heading_text(self, text): | ||
# text may be None or ' ', for example -- ignore in those cases | ||
if text and text.strip(): | ||
text = self.apply_terms(text.lower()) | ||
if not self.capitalized: | ||
# don't use capitalize() on the whole of `text`, as this interferes with capitalised terms | ||
# either way, lstrip if capitalizing here since the first letter would be missed otherwise | ||
text = text.lstrip()[0].upper() + (text.lstrip()[1:] if len(text.lstrip()) > 1 else '') | ||
self.capitalized = True | ||
return text | ||
|
||
def apply_terms(self, text): | ||
# save a tiny bit of time by checking for any matches first | ||
if any(t in text for t in self.normalized_terms): | ||
for i, term in enumerate(self.normalized_terms): | ||
text = re.sub(rf'\b{term}\b', self.terms[i], text) | ||
return text |
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,165 @@ | ||
from django.test import TestCase | ||
from lxml import etree | ||
|
||
from indigo.analysis.sentence_caser import BaseSentenceCaser | ||
from indigo_api.models import Document, Work | ||
from indigo_api.tests.fixtures import document_fixture | ||
|
||
|
||
class SentenceCaserTestCase(TestCase): | ||
fixtures = ['languages_data', 'countries'] | ||
|
||
def setUp(self): | ||
self.work = Work(frbr_uri='/za/act/1991/1') | ||
self.sentence_caser = BaseSentenceCaser() | ||
self.sentence_caser.terms = ['Tâx', 'táxation', 'in the Höme'] | ||
self.maxDiff = None | ||
|
||
def test_sentence_case_headings_in_document(self): | ||
document = Document( | ||
work=self.work, | ||
language_id=1, | ||
document_xml=document_fixture( | ||
xml=""" | ||
<section eId="sec_0"> | ||
<num>0</num> | ||
<heading><i> TAXATION </i>IN<sup><authorialNote marker="1" placement="bottom" eId="sec_0__authorialNote_1"><p eId="sec_0__authorialNote_1__p_1">OR ON</p></authorialNote></sup> THE HOME<sup><authorialNote marker="1A" placement="bottom" eId="sec_0__authorialNote_2"><p eId="sec_0__authorialNote_2__p_1">FN</p></authorialNote></sup></heading> | ||
<content> | ||
<p eId="sec_0__p_1">Text</p> | ||
</content> | ||
</section> | ||
<section eId="sec_1"> | ||
<num>1</num> | ||
<heading><i>HELLO</i> NO TAXATION <i>WITHOUT REPRESENTATION</i> <b>(OTHER STUFF)</b></heading> | ||
<content> | ||
<p eId="sec_1__p_1">Text</p> | ||
</content> | ||
</section> | ||
<section eId="sec_1A"> | ||
<num>1A</num> | ||
<heading><i> </i>HELLO NO TAXATION <i>WITHOUT REPRESENTATION</i> <b>(OTHER STUFF)</b></heading> | ||
<content> | ||
<p eId="sec_1A__p_1">Text</p> | ||
</content> | ||
</section> | ||
<section eId="sec_2"> | ||
<num>2</num> | ||
<heading>TAXATION AND TAX, AND TAXONOMIES TOO</heading> | ||
<content> | ||
<p eId="sec_2__p_1">Text</p> | ||
</content> | ||
</section> | ||
<section eId="sec_2A"> | ||
<num>2A</num> | ||
<heading>TAXONOMIES ALL ALONE</heading> | ||
<content> | ||
<p eId="sec_2A__p_1">Text</p> | ||
</content> | ||
</section> | ||
<section eId="sec_3"> | ||
<num>3</num> | ||
<heading>TAXATION <i>IN THE HOME</i></heading> | ||
<content> | ||
<p eId="sec_3__p_1">Text</p> | ||
</content> | ||
</section> | ||
<section eId="sec_4"> | ||
<num>4</num> | ||
<heading><i> TAXATION </i>IN THE HOME<sup><authorialNote marker="1" placement="bottom" eId="sec_4__authorialNote_1"><p eId="sec_4__authorialNote_1__p_1">FN</p></authorialNote></sup></heading> | ||
<content> | ||
<p eId="sec_4__p_1">Text</p> | ||
</content> | ||
</section> | ||
<section eId="sec_5"> | ||
<num>5</num> | ||
<heading>DOUBLE <b>TAXATION</b></heading> | ||
<content> | ||
<p eId="sec_5__p_1">Text</p> | ||
</content> | ||
</section> | ||
<section eId="sec_6"> | ||
<num>6</num> | ||
<heading>SINGLE <i>TAXATION</i></heading> | ||
<content> | ||
<p eId="sec_6__p_1">Text</p> | ||
</content> | ||
</section> | ||
""" | ||
) | ||
) | ||
|
||
expected = Document( | ||
work=self.work, | ||
document_xml=document_fixture( | ||
xml=""" | ||
<section eId="sec_0"> | ||
<num>0</num> | ||
<heading><i>Táxation </i>in<sup><authorialNote marker="1" placement="bottom" eId="sec_0__authorialNote_1"><p eId="sec_0__authorialNote_1__p_1">OR ON</p></authorialNote></sup> the home<sup><authorialNote marker="1A" placement="bottom" eId="sec_0__authorialNote_2"><p eId="sec_0__authorialNote_2__p_1">FN</p></authorialNote></sup></heading> | ||
<content> | ||
<p eId="sec_0__p_1">Text</p> | ||
</content> | ||
</section> | ||
<section eId="sec_1"> | ||
<num>1</num> | ||
<heading><i>Hello</i> no táxation <i>without representation</i> <b>(other stuff)</b></heading> | ||
<content> | ||
<p eId="sec_1__p_1">Text</p> | ||
</content> | ||
</section> | ||
<section eId="sec_1A"> | ||
<num>1A</num> | ||
<heading><i> </i>Hello no táxation <i>without representation</i> <b>(other stuff)</b></heading> | ||
<content> | ||
<p eId="sec_1A__p_1">Text</p> | ||
</content> | ||
</section> | ||
<section eId="sec_2"> | ||
<num>2</num> | ||
<heading>Táxation and Tâx, and taxonomies too</heading> | ||
<content> | ||
<p eId="sec_2__p_1">Text</p> | ||
</content> | ||
</section> | ||
<section eId="sec_2A"> | ||
<num>2A</num> | ||
<heading>Taxonomies all alone</heading> | ||
<content> | ||
<p eId="sec_2A__p_1">Text</p> | ||
</content> | ||
</section> | ||
<section eId="sec_3"> | ||
<num>3</num> | ||
<heading>Táxation <i>in the Höme</i></heading> | ||
<content> | ||
<p eId="sec_3__p_1">Text</p> | ||
</content> | ||
</section> | ||
<section eId="sec_4"> | ||
<num>4</num> | ||
<heading><i>Táxation </i>in the Höme<sup><authorialNote marker="1" placement="bottom" eId="sec_4__authorialNote_1"><p eId="sec_4__authorialNote_1__p_1">FN</p></authorialNote></sup></heading> | ||
<content> | ||
<p eId="sec_4__p_1">Text</p> | ||
</content> | ||
</section> | ||
<section eId="sec_5"> | ||
<num>5</num> | ||
<heading>Double <b>táxation</b></heading> | ||
<content> | ||
<p eId="sec_5__p_1">Text</p> | ||
</content> | ||
</section> | ||
<section eId="sec_6"> | ||
<num>6</num> | ||
<heading>Single <i>táxation</i></heading> | ||
<content> | ||
<p eId="sec_6__p_1">Text</p> | ||
</content> | ||
</section> | ||
""" | ||
) | ||
) | ||
|
||
self.sentence_caser.sentence_case_headings_in_document(document) | ||
root = etree.fromstring(expected.content.encode('utf-8')) | ||
expected.content = etree.tostring(root, encoding='utf-8').decode('utf-8') | ||
self.assertEqual(expected.content, document.content) |
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,27 @@ | ||
# Generated by Django 4.2.15 on 2024-11-18 13:09 | ||
|
||
import django.contrib.postgres.fields | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('indigo_api', '0048_amendment_verb'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='AccentedTerms', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('terms', django.contrib.postgres.fields.ArrayField(base_field=models.CharField(max_length=1024, verbose_name='terms'), blank=True, null=True, size=None)), | ||
('language', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='accented_terms', to='indigo_api.language', unique=True, verbose_name='language')), | ||
], | ||
options={ | ||
'verbose_name': 'accented terms', | ||
'verbose_name_plural': 'accented terms', | ||
}, | ||
), | ||
] |
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
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
33 changes: 33 additions & 0 deletions
33
indigo_app/static/javascript/indigo/views/document_sentences_caser.js
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,33 @@ | ||
(function(exports) { | ||
"use strict"; | ||
|
||
if (!exports.Indigo) exports.Indigo = {}; | ||
Indigo = exports.Indigo; | ||
|
||
/** | ||
* Handle the sentence caser view. | ||
*/ | ||
Indigo.DocumentSentenceCaseView = Backbone.View.extend({ | ||
el: '#sentence-caser', | ||
events: { | ||
'click .sentence-case-headings': 'sentenceCaseHeadings' | ||
}, | ||
|
||
sentenceCaseHeadings: function(e) { | ||
let self = this, | ||
data = {'document': this.model.document.toJSON()}; | ||
|
||
data.document.content = this.model.toXml(); | ||
|
||
$.ajax({ | ||
url: this.model.document.url() + '/analysis/sentence-case-headings', | ||
type: "POST", | ||
data: JSON.stringify(data), | ||
contentType: "application/json; charset=utf-8", | ||
dataType: "json"}) | ||
.then(function(response) { | ||
self.model.set('content', response.document.content); | ||
}); | ||
}, | ||
}); | ||
})(window); |
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
Oops, something went wrong.