【发布时间】:2017-03-16 21:30:38
【问题描述】:
我正在尝试编写一个函数,该函数使用发音词典生成单词的所有可能发音排列。
# Dictionary
sounddef <- t(matrix(strsplit('c,k,c,s,ou,uh,n,n,t,t,r,r,y,ee,w,w,o,oh,o,uh,th,th,s,s,m,m',",")[[1]], nrow = 2))
# The first column is the written letter and the second is a possible pronunciation.
match_rec <- function(x, sounddef) {
if (!nzchar(x)) return("")
returner <- NULL
for (i in 1:nrow(sounddef)) {
v <- sounddef[i,]
char <- paste0("^",v[1])
if (grepl(char, x))
returner <- c(returner, paste0(v[1],'->',v[2], ",",
match_rec(gsub(char, "", x), sounddef), collapse=""))
}
returner
}
# Unfortunately this does not return the right values
match_rec("country", sounddef)
[1] "c->k,ou->uh,n->n,t->t,r->r,y->ee,c->k,o->oh,c->k,o->uh,"
[2] "c->s,ou->uh,n->n,t->t,r->r,y->ee,c->s,o->oh,c->s,o->uh,"
它应该返回的值是:
[1] "c->k,ou->uh,n->n,t->t,r->r,y->ee"
[2] "c->s,ou->uh,n->n,t->t,r->r,y->ee"
因为在字典中有两种可能的 c 发音方式。
【问题讨论】:
标签: r permutation