如何夸大字符串中的某些单词

时间:2018-04-13 19:16:54

标签: python python-3.x

这是我要膨胀的清单:

https://i.stack.imgur.com/bX7Rn.png

这就是我要制作的程序的示例运行:

https://i.stack.imgur.com/AYDOG.png

aList = [ "zero", "none", "nil", "null" ]
bList = [ "one", "won", "juan" ]
cList = [ "two", "to", "too", "tu" ]
dList = [ "three" ]
eList = [ "four", "for", "fore" ]
fList = [ "five" ]
gList = [ "six" ]
hList = [ "seven" ]
iList = [ "eight", "ate" ]
jList = [ "nine" ]
kList = [ "ten" ]
lList = [ "eleven" ]
mList = [ "twelve", "dozen" ]
nList = [ "never" ]
oList = [ "half" ]
pList = [ "once" ]
qList = [ "twice" ]
rList = [ "single" ]
sList = [ "double" ]
tList = [ "first" ]
uList = [ "second" ]
vList = [ "third" ]
wList = [ "fourth", "forth" ]


userInput = input( "Enter your sentence to inflate: " )
userInput = userInput.lower()


for i in userInput.split():
    if i in aList:
        userInput = userInput.replace( i, "one" )
    elif i in bList:
        userInput = userInput.replace( i, "two" )

每当我运行此代码时,它都有效,但它与其他列表冲突 例如,这是我的代码的示例运行:

https://i.stack.imgur.com/xhDy4.png

关于如何使这个程序有效的任何想法?

5 个答案:

答案 0 :(得分:0)

你应该替换:

for i in aList:
    userInput = userInput.replace( i, "one" )
for i in bList:
    userInput = userInput.replace( i, "two" )

只有:

package.json

它只是遍历每个列表中的每个项目,并替换它。

答案 1 :(得分:0)

您应检查列表中的某个字是否在输入中,而不是检查每个输入字是否在您的某个列表中。例如

for word in aList: 
    if word in userInput: 
        userInput.replace(word, 'one')
...

而不是拥有大量带有大量复制粘贴的列表,您可以以不同的方式构建列表以便能够迭代它们。也许是这样的:

from collections import namedtuple
Replacement = namedtuple('Replacement', ['before', 'after'])
replacements = [
    Replacement(["zero", "none", "nil", "null"], "one"),        
    Replacement(["one", "won", "juan"],          "two"),
    ...
]
for replacement in replacements: 
    for word in replacement.before:
        userInput = userInput.replace(word, replacement.after)

答案 2 :(得分:0)

首先,不要为每个单词使用变量。使用一个大的嵌套列表,如下所示:

replacements = [
    [["zero", "none", "nil", "null"], "one"],
    [["one", "won", "juan"], "two"],
    [["two", "to", "too", "tu"], "three"],
    [["three"], ""],
    [["four", "for", "fore"], ""],
    [["five"], ""],
    [["six"], ""],
    [["seven"], ""],
    [["eight", "ate"], ""],
    [["nine"], ""],
    [["ten"], ""],
    [["eleven"], ""],
    [["twelve", "dozen"], ""],
    [["never"], ""],
    [["half"], ""],
    [["once"], ""],
    [["twice"], ""],
    [["single"], ""],
    [["double"], ""],
    [["first"], ""],
    [["second"], ""],
    [["third"], ""],
    [["fourth", "forth"], ""],
]

(你可以填写其余部分)

然后您可以遍历该列表来进行替换。其他答案已经涵盖了这一点。

要解决您的问题,因为one可以替换为twotwo可以替换为three等等,这意味着您需要替换所有{ {1}} 之前替换three,并在替换two之前替换所有two ...所以你需要遍历列表并且以特定顺序执行替换。我不想完全放弃它(尽管我已经很多),花一些时间来考虑它。

答案 3 :(得分:0)

如果您仔细构建数据,则可以取消两个循环:

lists = [
 { 
  "infl": "one", 
  "list": [ "zero", "none", "nil", "null" ] 
 },

 { "infl": "two",   "list": [ "one", "won", "juan" ] },
 { "infl": "three", "list": [ "two", "to", "too", "tu" ] },
 { "infl": "four",  "list": [ "three" ] },
 { "infl": "five",  "list": [ "four", "for", "fore" ] },
 # etc.... etc...
]

for lst in lists:
  for i in lst["list"]:
    userInput = userInput.replace(i, lst["infl"])

答案 4 :(得分:0)

我对此非常有趣。使用re.sub,您可以真正利用sub的函数参数来实现此目的。这非常有用,因为每个捕获的组只会被替换一次(通过查找非重叠的模式),因此不会重复扫描相同的字符串。

这个解决方案容易出现不同数字的替换问题,但只会将您的单词放在列表中的顺序,以便列表中的单词不会包含在连续的内容中同一个列表中的单词。所以要确保"到"在"" 之后"工具" 将替换为" threeol" 而不是" threel"

from re import sub, IGNORECASE
subs = {    #The new words are the keys. Respective values are the old words to replace
    "one": [ "zero", "none", "nil", "null" ],
    "two": [ "one", "won", "juan" ],
    "three": [ "two", "too", "to", "tu" ],
    "four": [ "three" ],
    "five": [ "four", "for", "fore" ],
    "six": [ "five" ],
    "seven": [ "six" ],
    "eight": [ "seven" ],
    "nine": [ "eight", "ate" ],
    "ten": [ "nine" ],
    "eleven": [ "ten" ],
    "twelve": [ "eleven" ],
    "thirteen": [ "twelve", "dozen" ],
    "once": [ "never" ],
    "one and a half": [ "half" ],
    "twice": [ "once" ],
    "thrice": [ "twice" ],
    "double": [ "single" ],
    "triple": [ "double" ],
    "second": [ "first" ],
    "third": [ "second" ],
    "fourth": [ "third" ],
    "fifth": [ "fourth", "forth" ]
}

#Flatten list of substitutes into regular expression with named capture groups
all_replacements = '|'.join('(?P<{}>{})'.format(new.replace(' ', '_'), '|'.join(olds)) for new, olds in subs.items())

def inc(match):     #increment 1 from the match
    return str(int(match.group()) + 1)

def inflate(match): #Get the next named capture group that exists and send new
    return next(new for new, old in match.groupdict().items() if old)

def replace(string):
    string = sub(r'\d+', inc, string)  #Increment all numbers
    string = sub(all_replacements, inflate, string, flags=IGNORECASE) #Inflate all words
    return string

print(replace("I wonder"))
print(replace(""))
print(replace("It was the best of times."))
print(replace("A Tale of 2 Cities"))
print(replace("A WoNder if 18 is bigger than 1919191"))

以下打印项目的结果

I twoder

It was the best of times.
A Tale of 3 Cities
A twoder if 19 is bigger than 1919192

最终,您的列表可以非常轻松地进行扩展,同时保持可读性,因为如果您决定添加到替换列表中,则不必更改任何代码。