Tabulating characters with diacritics in R

十年热恋 提交于 2019-12-23 07:12:39

问题


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 


来源:https://stackoverflow.com/questions/30551549/tabulating-characters-with-diacritics-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!