Tabulating characters with diacritics in R

2019-06-15 10:40发布

问题:

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?

回答1:

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