用JSON中的字符串列表替换单词中的多个相等字符串

时间:2019-03-13 13:13:44

标签: python json python-3.x for-loop

我在用脚本将普通字母替换为特殊字符以测试翻译系统时遇到麻烦,这是一个示例(cha-mate是chá-mate,但将与chã-mate/chã-máte等一起测试变体),但没有创建这种变体,而是将所有相同的字符切换为仅一个特殊字母,这是它的打印内容:

chá-máte
chã-mãte

以下是理论上应该印刷的内容:

cha-máte
cha-mãte
chá-mate
chã-mate
etc.

这是使用的代码和json:

def translation_tester(word):
    esp_chars = {
        'a': 'áã',
    }

    #words = [word]
    for esp_char in esp_chars:
        if esp_char in word:
            replacement_chars = esp_chars[esp_char]
            for i in range(len(replacement_chars)):
                print(word.replace(esp_char, replacement_chars[i]))

def main():
    words = ['cha-mate']
    for word in words:
        translation_tester(word)

main()

无论如何,感谢您的帮助,谢谢!

2 个答案:

答案 0 :(得分:1)

要处理任意数量的替换,您需要使用递归。这就是我的方法。

intword = 'cha-mate'
esp_chars = {'a': 'áã'}

def wpermute(word, i=0):
    for idx, c in enumerate(word[i:], i):
        if c in esp_chars:
            for s in esp_chars[c]:
                newword = word[0:idx] + s + word[idx + 1:]
                wpermute(newword, idx + 1)
        if idx == len(word) -1:
            print(word)

wpermute(intword)

提供了9种不同的单词写法输出。

chá-máte
chá-mãte
chá-mate
chã-máte
chã-mãte
chã-mate
cha-máte
cha-mãte
cha-mate

答案 1 :(得分:1)

也许有更好的方法,但是您可以执行以下操作(确保在替换字符列表中包含普通的'a')

import itertools
import re

def replace_at_indices(word, new_chars, indices):
  new_word = word
  for i, index in enumerate(indices):
    new_word = new_word[:index] + new_chars[i] + new_word[index+1:]
  return new_word

def translation_tester(word):
    esp_chars = {
        'a': 'aáã',
    }

    for esp_char in esp_chars:
      replacement_chars = list(esp_chars[esp_char])
      indices = [m.start() for m in re.finditer(esp_char, word)]
      product = list(itertools.product(replacement_chars, repeat=len(indices)))
      for p in product:
        new_word = replace_at_indices(word, p, indices)
        print(new_word)

def main():
    words = ['cha-mate']
    for word in words:
        translation_tester(word)

main()

以您的示例为例,这应该给您:

cha-mate
cha-máte
cha-mãte
chá-mate
chá-máte
chá-mãte
chã-mate
chã-máte
chã-mãte

另请参阅:

Find all occurrences of a substring in Python

generating permutations with repetitions in python

Replacing a character from a certain index