Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
741 views
in Technique[技术] by (71.8m points)

python-3.x - 如何在“ for”循环中的每个错字旁边添加正确的信息?(How do I add correct information next to each typo in the “for” loop?)

I have a typo detecting tool.

(我有一个错字检测工具。)

Here is my code:

(这是我的代码:)

from functools import partial

def x_in_y(word, inner):
    return inner in word

wrong = ['mann','connaction','tee','rigt','putt']
sentence=['this mann is my son','the connaction is unstable','my tee is getting cold','put your hands down','rigt now','right behind my back']

for i in wrong:
    print(f"Wrong: {i}")
    filtered_names = filter(partial(x_in_y, inner=i), sentence)
    for name in filtered_names:
        print(name)

Output:

(输出:)

Wrong: mann
this mann is my son
Wrong: connaction
the connaction is unstable
Wrong: tee
my tee is getting cold
Wrong: rigt
rigt now
Wrong: putt

Although it could help me detect typo in labels(mann,connaction,tee....), but how could I add a correct word next to each labels?

(尽管它可以帮助我检测标签中的拼写错误(mann,connaction,tee ....),但是如何在每个标签旁边添加正确的单词?)

Like:

(喜欢:)

Wrong: mann "should be man"
this mann is my son
Wrong: connaction "should be connection"
the connaction is unstable
Wrong: tee "should be tea"
my tee is getting cold
Wrong: rigt "should be right"
rigt now
Wrong: putt

Also, I want

(我也要)

"Wrong:putt"

(“错:放”)

disappears if it doesn't really detect this kind of typo.

(如果没有真正检测到这种错字,则消失。)

How could I make it?

(我该怎么做?)

Please help

(请帮忙)

  ask by briiipo translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With tuples including the wrong and the correct word, it's quite easy to fix your code.

(使用包含错误和正确单词的元组,可以很容易地修复您的代码。)

See this first version, where you just have to fix the format of what you want to display:

(请参阅第一个版本,您只需要在其中固定要显示的格式即可:)

from functools import partial

def x_in_y(word, inner):
    return inner in word

wrong = [('mann','man'),('rigt','right')]
sentence=['this mann is my son','the connaction is unstable','my tee is getting cold','put your hands down','rigt now','right behind my back']

for i in wrong:
    print(f"Wrong: {i[0]}")
    filtered_names = filter(partial(x_in_y, inner=i[0]), sentence)
    for name in filtered_names:
        print(name)
        print(i[1])

I changed the content of wrong, and added the [0] after i where needed.

(我更改了错误的内容,并在需要的地方添加了[0]。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...