Monday, January 1, 2024

Problem of using the punctuation character class in regex with Burmese text


I managed somehow to create a corpus from the dump file of the Myanmar language Wikipedia back in 2019 using the “xml2” package and other tools of the R programming enivironment. Four years on and feeling a little more confident, I am trying again with the Wikipedia dump file of 2023.

Looking around for resources on the internet, I was lucky to find out here that Lindemann had created multilingual corpus from Wikipedia files including that for the Burmese Wikipedia.

Lindemann’s set of “fully cleaned text corpus (110 MB)” was downloaded from here.

After getting that file and having extracted the Burmese corpus, in just the first few lines of text, I noticed “၎” missing from what was to be “၎င်း”.
Then I located the corresponding article in the Burmese Wikipedia and found that “၍” was also missing.

Extract from Lindemann’s corpus
Extract from Lindemann’s corpus


Wikipedia article extract
Wikipedia article extract


My hunch was that Lindemann might be using the puntuation character class “[[:punct:]]” in regex syntax to remove all the punctuation. Unfortunately in my work I found that using “[[:punct:]]” would remove the Burmese punctuation “၊” and “။” as well as those that are not, namely, any of “၌၍၎၏”.

So, I tried searching for all those characters in his Burmese corpus. And I could find none!

Sunday, December 17, 2023

Syllabification of Myanmar Unicode text with Regex - 3

 
My syllabification code so far

In my last test text string, I spelled သုသာန် wrongly with ဿ. So I have to create the test string with correct instances of the latter.

ttt <- "ဝင်းဦး၏ - စတီရီယိုသီချင်းကြီး၊ဪအတ္တအနိစ္စဒုက္ခ။ကျွန်ုပ်၏ရွှေတပဲသားကြောင့်ပြဿနာတက်သည့်မနုဿ၏ဒဿနနှင့်၎င်းသည်အင်္ကျီလုံချည်မပါဘဲသွား၍ဤသုသာန်၌သေလတ္တံ့၊ Farewell to Arms (သွားလေရော့လက်ရုံး)၂၊ဣန္ဒာနွယ်ပါးချိုင့်ကလေးချစ်စရာပင်။"
ttt
[1] "ဝင်းဦး၏ - စတီရီယိုသီချင်းကြီး၊ဪအတ္တအနိစ္စဒုက္ခ။ကျွန်ုပ်၏ရွှေတပဲသားကြောင့်ပြဿနာတက်သည့်မနုဿ၏ဒဿနနှင့်၎င်းသည်အင်္ကျီလုံချည်မပါဘဲသွား၍ဤသုသာန်၌သေလတ္တံ့၊ Farewell to Arms (သွားလေရော့လက်ရုံး)၂၊ဣန္ဒာနွယ်ပါးချိုင့်ကလေးချစ်စရာပင်။"

My syllabification code is in R. It is modular with features that could be modified or added as necessary. The following shows how the different features were added successively.

library(stringr)
# segment consonants, independant vowels, symbols
str_replace_all(ttt, "([က-အဣ-ဧဩဪဿ၌-၏])", "-\\1") %>% 
  str_replace_all(., "-", " ")
[1] " ဝ င်း ဦး ၏    စ တီ ရီ ယို သီ ချ င်း ကြီး၊ ဪ အ တ္ တ အ နိ စ္ စ ဒု က္ ခ။ ကျွ န်ု ပ် ၏ ရွှေ တ ပဲ သား ကြော င့် ပြ ဿ နာ တ က် သ ည့် မ နု ဿ ၏ ဒ ဿ န နှ င့် ၎ င်း သ ည် အ င်္ ကျီ လုံ ချ ည် မ ပါ ဘဲ သွား ၍ ဤ သု သာ န် ၌ သေ လ တ္ တံ့၊ Farewell to Arms ( သွား လေ ရော့ လ က် ရုံး)၂၊ ဣ န္ ဒာ နွ ယ် ပါး ချို င့် က လေး ချ စ် စ ရာ ပ င်။"
# + fix athat, aukkamyin, wutsapauk with final consonant
str_replace_all(ttt, "([က-အဣ-ဧဩဪဿ၌-၏])", "-\\1") %>% 
  str_replace_all(., "-", " ") %>% 
  str_replace_all(., "\\s([က-အ][့်း]\\s|[က-အ][့်း])", "\\1")
[1] " ဝင်း ဦး ၏    စ တီ ရီ ယို သီ ချင်း ကြီး၊ ဪ အ တ္ တ အ နိ စ္ စ ဒု က္ ခ။ ကျွန်ုပ် ၏ ရွှေ တ ပဲ သား ကြောင့် ပြ ဿ နာ တက် သည့် မ နု ဿ ၏ ဒ ဿ န နှင့် ၎င်း သည် အင်္ ကျီ လုံ ချည် မ ပါ ဘဲ သွား ၍ ဤ သု သာန် ၌ သေ လ တ္ တံ့၊ Farewell to Arms ( သွား လေ ရော့ လက် ရုံး)၂၊ ဣ န္ ဒာ နွယ် ပါး ချိုင့် က လေး ချစ် စ ရာ ပင်။"
# + fix patsint (virama)
str_replace_all(ttt, "([က-အဣ-ဧဩဪဿ၌-၏])", "-\\1") %>% 
  str_replace_all(., "-", " ") %>% 
  str_replace_all(., "\\s([က-အ][့်း]\\s|[က-အ][့်း])", "\\1") %>% 
  str_replace_all(., "\\s([က-အ]္)\\s", "\\1")  
[1] " ဝင်း ဦး ၏    စ တီ ရီ ယို သီ ချင်း ကြီး၊ ဪ အတ္တ အ နိစ္စ ဒုက္ခ။ ကျွန်ုပ် ၏ ရွှေ တ ပဲ သား ကြောင့် ပြ ဿ နာ တက် သည့် မ နု ဿ ၏ ဒ ဿ န နှင့် ၎င်း သည် အင်္ ကျီ လုံ ချည် မ ပါ ဘဲ သွား ၍ ဤ သု သာန် ၌ သေ လတ္တံ့၊ Farewell to Arms ( သွား လေ ရော့ လက် ရုံး)၂၊ ဣန္ဒာ နွယ် ပါး ချိုင့် က လေး ချစ် စ ရာ ပင်။"
# + fix kinzi
str_replace_all(ttt, "([က-အဣ-ဧဩဪဿ၌-၏])", "-\\1") %>% 
  str_replace_all(., "-", " ") %>% 
  str_replace_all(., "\\s([က-အ][့်း]\\s|[က-အ][့်း])", "\\1") %>% 
  str_replace_all(., "\\s([က-အ]္)\\s", "\\1")  %>% 
  str_replace_all(., "(\\s[က-အ]င်္|\\င်္)\\s", "\\1") 
[1] " ဝင်း ဦး ၏    စ တီ ရီ ယို သီ ချင်း ကြီး၊ ဪ အတ္တ အ နိစ္စ ဒုက္ခ။ ကျွန်ုပ် ၏ ရွှေ တ ပဲ သား ကြောင့် ပြ ဿ နာ တက် သည့် မ နု ဿ ၏ ဒ ဿ န နှင့် ၎င်း သည် အင်္ကျီ လုံ ချည် မ ပါ ဘဲ သွား ၍ ဤ သု သာန် ၌ သေ လတ္တံ့၊ Farewell to Arms ( သွား လေ ရော့ လက် ရုံး)၂၊ ဣန္ဒာ နွယ် ပါး ချိုင့် က လေး ချစ် စ ရာ ပင်။"
# + fix ဿ
str_replace_all(ttt, "([က-အဣ-ဧဩဪဿ၌-၏])", "-\\1") %>% 
  str_replace_all(., "-", " ") %>% 
  str_replace_all(., "\\s([က-အ][့်း]\\s|[က-အ][့်း])", "\\1") %>% 
  str_replace_all(., "\\s([က-အ]္)\\s", "\\1")  %>% 
  str_replace_all(., "(\\s[က-အ]င်္|\\င်္)\\s", "\\1")  %>% 
  str_replace_all(., "\\s(ဿ)", "\\1")
[1] " ဝင်း ဦး ၏    စ တီ ရီ ယို သီ ချင်း ကြီး၊ ဪ အတ္တ အ နိစ္စ ဒုက္ခ။ ကျွန်ုပ် ၏ ရွှေ တ ပဲ သား ကြောင့် ပြဿ နာ တက် သည့် မ နုဿ ၏ ဒဿ န နှင့် ၎င်း သည် အင်္ကျီ လုံ ချည် မ ပါ ဘဲ သွား ၍ ဤ သု သာန် ၌ သေ လတ္တံ့၊ Farewell to Arms ( သွား လေ ရော့ လက် ရုံး)၂၊ ဣန္ဒာ နွယ် ပါး ချိုင့် က လေး ချစ် စ ရာ ပင်။"
# + remove English text, numerals, punctuations, and space at the begining of the string
str_replace_all(ttt, "([က-အဣ-ဧဩဪဿ၌-၏])", "-\\1") %>% 
  str_replace_all(., "-", " ") %>% 
  str_replace_all(., "\\s([က-အ][့်း]\\s|[က-အ][့်း])", "\\1") %>% 
  str_replace_all(., "\\s([က-အ]္)\\s", "\\1")  %>% 
  str_replace_all(., "(\\s[က-အ]င်္|\\င်္)\\s", "\\1")  %>% 
  str_replace_all(., "\\s(ဿ)", "\\1") %>%
  str_remove_all(., "[a-zA-Z0-9၀-၉၊။\\[\\]\\(\\)]|^\\s")
[1] "ဝင်း ဦး ၏    စ တီ ရီ ယို သီ ချင်း ကြီး ဪ အတ္တ အ နိစ္စ ဒုက္ခ ကျွန်ုပ် ၏ ရွှေ တ ပဲ သား ကြောင့် ပြဿ နာ တက် သည့် မ နုဿ ၏ ဒဿ န နှင့် ၎င်း သည် အင်္ကျီ လုံ ချည် မ ပါ ဘဲ သွား ၍ ဤ သု သာန် ၌ သေ လတ္တံ့     သွား လေ ရော့ လက် ရုံး ဣန္ဒာ နွယ် ပါး ချိုင့် က လေး ချစ် စ ရာ ပင်"
# + reduce multiple space to single
str_replace_all(ttt, "([က-အဣ-ဧဩဪဿ၌-၏])", "-\\1") %>% 
  str_replace_all(., "-", " ") %>% 
  str_replace_all(., "\\s([က-အ][့်း]\\s|[က-အ][့်း])", "\\1") %>% 
  str_replace_all(., "\\s([က-အ]္)\\s", "\\1")  %>% 
  str_replace_all(., "(\\s[က-အ]င်္|\\င်္)\\s", "\\1")  %>% 
  str_replace_all(., "\\s(ဿ)", "\\1") %>%
  str_remove_all(., "[a-zA-Z0-9၀-၉၊။\\[\\]\\(\\)]|^\\s") %>%
  str_squish(.)

[1] "ဝင်း ဦး ၏ စ တီ ရီ ယို သီ ချင်း ကြီး ဪ အတ္တ အ နိစ္စ ဒုက္ခ ကျွန်ုပ် ၏ ရွှေ တ ပဲ သား ကြောင့် ပြဿ နာ တက် သည့် မ နုဿ ၏ ဒဿ န နှင့် ၎င်း သည် အင်္ကျီ လုံ ချည် မ ပါ ဘဲ သွား ၍ ဤ သု သာန် ၌ သေ လတ္တံ့ သွား လေ ရော့ လက် ရုံး ဣန္ဒာ နွယ် ပါး ချိုင့် က လေး ချစ် စ ရာ ပင်"

The problem with “၊ ။ ၌ ၍ ၎ ၏” in Regex

When I tried using [:punct:] in removing all the English punctuation characters as well as ၊ and ။ for Myanmar language, I found that all of the “၊ ။ ၌ ၍ ၎ ၏” were also removed! Is that a bug in R? So I have to specify each of the punctuation in the Regex syntax.

# + remove English text, numerals, punctuations (using [:punct:]), and space at the begining of the string
str_replace_all(ttt, "([က-အဣ-ဧဩဪဿ၌-၏])", "-\\1") %>% 
  str_replace_all(., "-", " ") %>% 
  str_replace_all(., "\\s([က-အ][့်း]\\s|[က-အ][့်း])", "\\1") %>% 
  str_replace_all(., "\\s([က-အ]္)\\s", "\\1")  %>% 
  str_replace_all(., "(\\s[က-အ]င်္)\\s", "\\1")  %>% 
  str_replace_all(., "\\s(ဿ)", "\\1") %>%
  str_remove_all(., "[a-zA-Z0-9၀-၉[:punct:]]|^\\s")  # using[:punct:]
[1] "ဝင်း ဦး     စ တီ ရီ ယို သီ ချင်း ကြီး ဪ အတ္တ အ နိစ္စ ဒုက္ခ ကျွန်ုပ်  ရွှေ တ ပဲ သား ကြောင့် ပြဿ နာ တက် သည့် မ နုဿ  ဒဿ န နှင့် င်း သည် အင်္ကျီ လုံ ချည် မ ပါ ဘဲ သွား  ဤ သု သာန်  သေ လတ္တံ့     သွား လေ ရော့ လက် ရုံး ဣန္ဒာ နွယ် ပါး ချိုင့် က လေး ချစ် စ ရာ ပင်"

Earlier when I compiled the Myanmar Wikipedia corpus I didn’t know about this effect. Luckily I did not use [:punct:] at that time. How frequently ၎င်း occurs in that corpus? How much damage would have been done using [:punct:]?

I tried to find out.

length(grep("၎င်း", x100_itN_Sen.5)) %>% paste("No. of sentences with complete syllable '၎င်း' =", .)
[1] "No. of sentences with complete syllable '၎င်း' = 8470"
length(grep("၎", x100_itN_Sen.5)) %>% paste("No. of sentences with complete/incomplete syllable '၎င်း' =", .)
[1] "No. of sentences with complete/incomplete syllable '၎င်း' = 8485"
length(grep("၎", x100_itN_Sen.5, invert = TRUE)) %>% paste("No. of sentences without complete/incomplete syllable '၎င်း' =", .)
[1] "No. of sentences without complete/incomplete syllable '၎င်း' = 297920"
length(x100_itN_Sen.5) %>% paste("Total No. of sentences = ", .)
[1] "Total No. of sentences =  306405"

Additionally there would surely be a lot of “၊ ။ ၌ ၍ ၏” in the corpus and all of them removed as well from the corpus if [:punct:] were used in the regex syntax.

Wednesday, December 13, 2023

Syllabification of Myanmar Unicode text with Regex - 2


In the Ye Kyaw Thu’s post mentioned previously (
https://github.com/ye-kyaw-thu/sylbreak), he rejected a paper’s claim that his syllable segmentation method “… cannot correctly segment syllables that contain consonants, ‘်’ and ‘့’ …”. He asserted that his method works perfectly if the ‘်’ and ‘့’ are entered in correct order, that is ‘်’ first and then ‘့’.

To check this issue with our syllabification approach we created a text string t.1 containing “ရပ်တန့်” with correct order of “်” followed by “့” and t.2 with incorrect order of “့” followed by “်” in “န့်” using on-screen keyboard.

Check if our syllabification approach has issues with ‘်’ and ‘့’

# create data
t.1 <- "ရပ်တန့်" 
t.2 <- "ရပ်တန့်"
t.1
[1] "ရပ်တန့်"
t.2
[1] "ရပ်တန့်"
# Are they are exactly equal?
t.1 == t.2
[1] TRUE

They were exactly equal because the keyboard application changed the incorrect keying into correct order. We can split these two texts into characters to see that also.

strsplit(t.1, split = "")
[[1]]
[1] "ရ" "ပ" "်"  "တ" "န" "့"  "်" 
strsplit(t.2, split = "")
[[1]]
[1] "ရ" "ပ" "်"  "တ" "န" "့"  "်" 

Yes that is true. So to be able to get the second text string with incorrect order we programmetically enter the text with the Unicode codepoints. Then we again check if they are exactly equal.

tp.1 <- "ရပ်တန\u103a\u1037" 
tp.2 <- "ရပ်တန\u1037\u103a"
tp.1
[1] "ရပ်တန့်"
tp.2
[1] "ရပ်တန့်"
tp.1 == tp.2
[1] FALSE
strsplit(tp.1, split = "")
[[1]]
[1] "ရ" "ပ" "်"  "တ" "န" "်"  "့" 
strsplit(tp.2, split = "")
[[1]]
[1] "ရ" "ပ" "်"  "တ" "န" "့"  "်" 

They look the same in output. But the order of individual characters shows they really are different.

We now create a test string containing one correctly keyed text and two incorrectly keyed text.

d <- paste(tp.1,tp.2,tp.2, sep = "")
d
[1] "ရပ်တန့်ရပ်တန့်ရပ်တန့်"
# check: split into characters
strsplit(d, split = "")
[[1]]
 [1] "ရ" "ပ" "်"  "တ" "န" "်"  "့"  "ရ" "ပ" "်"  "တ" "န" "့"  "်"  "ရ" "ပ" "်"  "တ" "န"
[20] "့"  "်" 
# make syllables
tst <- str_replace_all(d, "([က-အဣ-ဧဩဪဿ၌-၏])", "-\\1") %>% str_replace_all(., "-", " ") %>% str_replace_all(., "\\s([က-အ][့်း]\\s|[က-အ][့်း])", "\\1") %>% str_replace_all(., "\\s([က-အ]္)\\s", "\\1")  %>% str_replace_all(., "(\\s[က-အ]င်္)\\s", "\\1") %>% str_remove_all(., "[a-zA-Z0-9၀-၉၊။\\[\\]\\(\\)]|^\\s") %>% str_squish(.)
tst
[1] "ရပ် တန့် ရပ် တန့် ရပ် တန့်"
# see if the original order of ‘်’ and ‘့’ retained in the syllables
strsplit(tst, split = "")
[[1]]
 [1] "ရ" "ပ" "်"  " " "တ" "န" "်"  "့"  " " "ရ" "ပ" "်"  " " "တ" "န" "့"  "်"  " " "ရ"
[20] "ပ" "်"  " " "တ" "န" "့"  "်" 

So the correct and incorrect keying orders are retained.

Any issue in using our syllables with “quanteda” R package

Here, since I have been learning to use the “quanteda” R package for quantitative text analysis, I am eager to find out if the keying order would affect it.

A simple test is to let the dfm() function of quanteda get the number of syllables created.

library(quanteda)
dfm(tokens(tst))
Document-feature matrix of: 1 document, 2 features (0.00% sparse) and 0 docvars.
       features
docs    ရပ် တန့်
  text1  3  3

The result shows that the keying order in the “တန့်” syllables is ignored.

Conclusion

  1. Our syllabification approach has no issues with ‘်’ and ‘့’.
  2. The quanteda R package seems to be insensitive to keying order in the composition of the syllables.