使用 R 检测字符串中的国际音标 (IPA) 符号/字符块

问题描述 投票:0回答:1

我正在尝试找出使用 R 将字符串(单词)拆分为单个电话的最佳方法,但我一直无法想出一个好的解决方案。我知道一种解决方案是使用 gruut-ipa 模块,但我无法动摇这种感觉,即有一种简单的方法可以用 R 做到这一点,但我无法弄清楚。

IPA 符号由多个组合字符和非组合字符组成。 IPA symbol structure(图片来自gruut-ipa github.

我正在使用 Panphon 数据 作为 ipa 字符的基础。完整列表包含 6,487 个条目。

example_sample <- c("ʔpoɣʔe","mtoto","nukapːiaʁaq","boobal","tamaru")
example_ipa <- c("ḁː","b͡dːˤ","k","k͡pˠ","ʁ","o","ʔ","pː","p")

我们的目标是识别单词并将其拆分为单独的音素,因此在这些示例中,“nukapːiaʁaq”应该变成“n_u_k_a_pː_i_a_ʁ_a_q”而不是 n_u_k_a_p_ː_i_a_ʁ_a_q”(因此不仅仅识别一个字符)。

我一直在用 purrr、stringr 和 stringi 进行测试,但还没有找到一种能产生良好结果的方法。

r purrr stringr stringi
1个回答
0
投票

不确定这是否解决了任务 - 不幸的是,我注意到 IPA 符号的特殊性。

# for convienience of pipeing/cueing the function calls
library(dplyr)
# subtitute everyting with an underline
gsub(pattern = "*", replacement = "_", example_ipa) %>% 
    # remove trailing and leading underlines
    gsub(pattern = "^_|_$", replacement = "") %>% 
    # solve the _ before special symbol ː by replacement
    gsub(pattern = "_ː", replacement = "ː")

[1] "ʔ_p_o_ɣ_ʔ_e"          "m_t_o_t_o"            "n_u_k_a_pː_i_a_ʁ_a_q" "b_o_o_b_a_l"          "t_a_m_a_r_u"   

现在您正在使用的字符集中可能还有其他“特殊符号”(我怀疑是这种情况,因为您有两套),您可能希望在最后一步中包含这些符号(您需要一个 caputre 组来调用更换零件):

gsub(pattern = "*", replacement = "_", example_ipa) %>% 
    gsub(pattern = "^_|_$", replacement = "") %>% 
    # with the or | you can chain symbols and the pharenthis are used for the caputre group \\1
    # I had to introduce a space after the second special symbol as it is needed to show properly - be sure to remove if it shows up 
    gsub(pattern = "_(ː|͡ )", replacement = "\\1")

[1] "a_̥ː"    "b͡ _dː_ˤ" "k"      "k͡ _p_ˠ"  "ʁ"      "o"      "ʔ"      "pː"     "p" 

IPA 字符集有一个特定的正则表达式 符号:\b 虽然它不会产生您要查找的结果,因为 ː 在那里被列为单个字符,所以它与中的“*”基本相同管道的第一次调用。

© www.soinside.com 2019 - 2024. All rights reserved.