【问题标题】:how to generate string of letters based on some parameters如何根据一些参数生成字母串
【发布时间】:2016-10-19 13:04:02
【问题描述】:

我有一组句子,每个句子中的单词数不同。我需要用一串字母替换每个单词,但字母串需要基于特定的标准。例如,字母“t”只能替换为字母“i”、“l”、“f”;对于字母表中的每个字母,字母“e”只能被“o”或“c”替换,依此类推。此外,单词之间的空格以及句号、撇号和其他标点符号需要保持完整。举个例子: 原文:他爱狗。 带有一串字母的句子:Fc tcwoz bcy。

有没有办法在 R 中自动化这个过程?谢谢。

添加:我需要替换大约 400 个句子。句子存储在数据框的变量(data$sentences)中。

【问题讨论】:

  • 那么,您需要为每个字符使用定义集中的随机字符吗?你都尝试了些什么?请分享。
  • 不妨看看strsplit。您可以使用 split="" 参数将每个字符作为单独的元素。然后你会建立一些映射。最后和paste(..., collapse="")放在一起。如果没有有关映射的更多详细信息,则无法提供进一步的帮助。

标签: r string replace words


【解决方案1】:

更新 2:一些代码重构,添加了一个简单的后备策略来处理丢失的字符(因此我们可以对给定字符串中的所有字符进行编码,即使我们没有确切的字符一对一映射),并在字符串向量上添加了示例循环。

# we define two different strings to be encode
mystrings <- c('bye', 'BYE')

# the dictionary with the replacements for each letter
# for the lowercase letters we are defining the exact entries
replacements <- {}
replacements['a'] <- 'xy'
replacements['b'] <- 'zp'
replacements['c'] <- '91'
# ... 
replacements['e'] <- 'xyv'
replacements['y'] <- 'opj'

# then we define a generic "fallback" entry
# to be used when we have no clues on how to encode a 'new' character
replacements['fallback'] <- '2345678'


# string, named vector -> character
# returns a single character chosen at random from the dictionary
get_random_entry <- function(entry, dictionary) {

  value <- dictionary[entry]

  # if we don't know how to encode it, use the fallback
  if (is.na(value)) {
    value <- dictionary['fallback']
  }

  # possible replacement for the current character
  possible.replacements <- strsplit(value[[1]], '')[[1]]

  # the actual replacement
  result <- sample(possible.replacements, 1)

  return(result)
}

# string, named vector -> string
# encode the given string, using the given named vector as dictionary
encode <- function(s, dictionary) {

  # get the actual subsitutions 
  substitutions <- sapply (strsplit(s,'')[[1]], function(ch) {

    # for each char in the string 's'
    # we collect the respective encoded version
    return(get_random_entry(ch, dictionary))

  }, USE.NAMES = F,simplify = T);

  # paste the resulting vector into a single string
  result <- paste(substitutions, collapse = '')

  # and return it
  return(result);
}

# we can use sapply to process all the strings defined in mystrings
# for 'bye' we know how to translate
# for 'BYE' we don't know; we'll use the fallback entry
encoded_strings <- sapply(mystrings, function(s) {
                                        # encode a single string
                                        encode(s, replacements)
                                     }, USE.NAMES =  F)

encoded_strings

【讨论】:

  • 它与您提供的字符串完美配合,谢谢!如果我需要在很多句子中使用它,我可能需要一个 for 循环开头?为了对数据框中我的变量的每一行(即每个句子)运行此过程。
  • 是的,基本上您可以将“# get the actual subsituations” 到脚本末尾的部分包装到一个函数中,然后调用它来对所有单独的字符串进行编码。如果您需要一个示例,请告诉我,在这种情况下,我会更新我的答案。谢谢
  • Leonardo Foderaro,如果你能提供一个例子,我将不胜感激。我想我应该使用 for(i in 1:nrow(data)) { } 但在设置要编码的字符串之前,然后将 s 定义为 data$sentences (但我不确定如何执行此操作),因为每一行 data$sentences 将是要编码的字符串。
  • 我要去参加一个工作会议,我不能写太多代码。我已经编辑了我的答案,将代码添加到一个函数中并在循环中调用它。可能这不是最有效的解决方案,但它应该可以工作。告诉我,谢谢。
  • 莱昂纳多,感谢您的更改。它现在可以正常工作了,只是它不保留单词之间的空格,它不会用大写字母替换大写字母,并且它忽略了标点符号。
猜你喜欢
  • 2022-12-18
  • 2013-06-05
  • 2016-12-21
  • 2023-03-21
  • 2022-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-09
相关资源
最近更新 更多