Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite welsh_letters to handle splitting Welsh digraphs rh/ng #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__
25 changes: 25 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import re
import unittest

import welsh_letters

RH_TEST_CASES = ['a rh y th m i a', 'rh a n n u', 'a n rh e g', 'a r h o l i', 'rh a g f y r h a u', 'b a r - rh o l i o']
NG_TEST_CASES = ['m a n g o', 'a n g i o p l a s t y', 'll u n g o p i', 't a n g y f l o g i', 't a ng i a d', 'Ll a n g o ll e n', 'b r o n g o ch', 'p e n f l i ng o', 'rh y ng w y n e b', 'll e n g a r', 'a ng h y f e i ll g a r']

class TestDigraphs(unittest.TestCase):
def test_split_word(self):
for test_case in RH_TEST_CASES + NG_TEST_CASES:
word = test_case.replace(' ', '')
actual = welsh_letters.split_word(word)
expected = test_case.split(' ')
self.assertEqual(actual, expected, word)

def test_split_word_with_lemma(self):
self.assertEqual(
welsh_letters.split_word('nghwyngar', 'cwyngar'),
['ng', 'h', 'w', 'y', 'n', 'g', 'a', 'r'],
'nghwyngar < cwyngar',
)

if __name__ == '__main__':
unittest.main()
66 changes: 47 additions & 19 deletions welsh_letters.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,53 @@
welsh_singletons = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "l", "m",
"n", "o", "p", "r", "s", "t", "u", "w", "y"]
import re, sys

welsh_digraphs = ["ch", "dd", "ff", "ng", "ll", "ph", "rh", "th"]
# POSSIBLE TYPOS IN THE LECSICON
# annrhigiadwy => anhrigiadwy
# cyfarhos => cyfaros?
# LIanrhystud => Llanrhystud
# lianrhystud => Llanrhystud
# sbringar => sbring-gar

welsh_alphabet = ["a", "b", "c", "ch", "d", "dd", "e", "f", "ff", "g", "ng",
"h", "i", "j", "l", "ll", "m", "n", "o", "p", "ph", "r", "rh",
"s", "t", "th", "u", "w", "y"]
DONT_SPLIT_RH = 'Caerhos Cilrhedyn Cwmyrhiwdre Nantyrhynnau Porthyrhyd Trerhedyn Trerhingyll Troedyrhiw arhythmia arhythmig coleorhisa ewrhythmeg gonorrhoea isorhythmig mycorhisa pyorhea yrhawg'.split()

supplemental_letters = ["k", "q", "v", "x", "z"]
# TODO: may need to include some of the following:
# Angefin Bodringallt Brengain Ingli Langro Llandingad Llanddingad Pinged Tangwen Tangwyn *ffaryngeal
SPLIT_NG = 'Abergwyngregyn Angliad Anglican Anglicanaidd Angola Bangladesh Bangor Bengal Blaengarw Blaengwrach Blaengwynfi Brongest Bronglais Bryngarn Bryngwran Bryngwyn Carngowil Carnguwch Carngwcw Cefngorwydd Cilmaengwyn Congo Cryngae Felinganol Ffynnongroyw Garthbrengi Glangors Grongaer Hengastell Hengoed Hengwm Hengwrt Hwngaraidd Hwngareg Hwngari Lingoed Llanengan Llanfairpwllgwyngyll Llwyngroes Llwyngwair Llwyngwern Maengwyn Melingriffith Mongolia Myngul Pengelli Penglais Pengorffwysfa Pengrynwr Pengwern Penybenglog Singrug Tafarngelyn Tanganyika Tongwynlais Tringarth Ynysymaengwyn amcangyfrifyn arlwyngig arweingi bangaw bangorwaith bechingalw bingo brongoch browngoch bryngaer cangarŵ conga congren cringoch cwango cwangoaidd dychangerdd engram genglo glingam gwerngoedwig gwyngoch gylfingroes hunglwyf hwiangerdd ingot jingo jingoistiaeth jwngl jyngl lingri llieingant llengig llinengrafiad llinganol llinglwm llongyfarch llongyfarchiad llwyngwril llyfngrwn manganîs manglo mango mangrof melyngoch mingam mingamu mwnglawdd mwngrel plaengan prynhawngwaith rhangor rhangymeriad rhieingerdd safnglo safngloi sbangl swyngan torlengig torllengig tudalengipio yngymaint ysgafngalon'.split()

letters_in_welsh = welsh_alphabet + supplemental_letters

exceptions = {"arholiad":"rh", "arholi":"rh","bangor":"ng", "llongyfarch":"ng"}

test_words = ["llaeth", "achwyn", "deddf", "chynghorwyr", "llyncom", "llumanu",
"iâr", "ci", "gafr", "cath", "mwnci", "ceffylau", "hwyaid", "ceiliog",
"angor", "bangor", "arholi", "rhwyfo", "llechoch", "llongyfarch"]
def split_word(word, lemma=None):
if lemma is None:
lemma = word
# DO NOT split rh (nor ng) if the lemma is one of the DONT_SPLIT_RH lemmas listed above.
if lemma in DONT_SPLIT_RH:
return re.findall(r'ch|dd|ff|ng|ll|ph|rh|th|.', word, re.IGNORECASE)
# Otherwise, DO split rh, unless it's at the start of a word, or after d/l/m/n/t
#
# DO split ng if the lemma is one of the SPLIT_NG lemmas listed above.
# Otherwise, DO NOT split ng, unless the lemma starts with one of these:
#
# angio bwngler byngalo dyngar dyngas gwangalon mening tang (except if it starts with tangiad)
#
# or one of these then a 'g':
#
# Llan blaen bon bron brown bryn calon cefn gwahan gwyn hunan llun mein mewn mwyn pan pen sein swyn teyrn un union
#
# or ends with 'n' then one of these:
#
# groen gar garwch gyfrif
#
# But even when splitting ng, don't split if it's at a word boundary
if lemma in SPLIT_NG or re.search(r'\b(angio|bwngler|byngalo|dyngar|dyngas|gwangalon|mening|tang(?!iad)|(Llan|blaen|bon|bron|brown|bryn|calon|cefn|gwahan|gwyn|hunan|llun|mein|mewn|mwyn|pan|pen|sein|swyn|teyrn|un|union)g)|n(groen|gar|garwch|gyfrif)\b', lemma, re.IGNORECASE):
return re.findall(r'ch|dd|ff|\bng|ng\b|ll|ph|\brh|(?<=[dlmnt])rh|th|.', word, re.IGNORECASE)
return re.findall(r'ch|dd|ff|ng|ll|ph|\brh|(?<=[dlmnt])rh|th|.', word, re.IGNORECASE)

def get_welsh_letter_count(word):
word = word.lower()
for welsh_digraph in welsh_digraphs:
if welsh_digraph is not exceptions.get(word):
word = word.replace(welsh_digraph, "@")
return len(word)
welsh_letters = split_word(word)
return len(welsh_letters)

if __name__ == '__main__':
while True:
word = sys.stdin.readline().strip()
if word == '':
break
print(word)
print(split_word(word))
print(get_welsh_letter_count(word))