Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tabulating characters with diacritics in R

I'm trying to tabulate phones (characters) occurrences in a string, but diacritics are tabulated as characters on their own. Ideally, I have a wordlist in International Phonetic Alphabet, with a fair amount of diacritics and several combinations of them with base characters. I give here a MWE with just one word, but the same goes with list of words and more types of combinations.

> word <- "n̥ana" # word constituted by 4 phones: [n̥],[a],[n],[a]
> table(strsplit(word, ""))
 ̥ a n 
1 2 2

But the wanted result is:

a n n̥
2 1 1

How can I manage to get this kind of result?

like image 712
Stefano Avatar asked May 30 '15 21:05

Stefano


1 Answers

Try

library(stringi)
table(stri_split_boundaries(word, type='character'))
#a n n̥ 
#2 1 1 

Or

 table(strsplit(word, '(?<=\\P{Ll}|\\w)(?=\\w)', perl=TRUE))
 #a n  n̥ 
 #2 1 1 
like image 163
akrun Avatar answered Nov 15 '22 15:11

akrun