如何更改正确单词的错误拼写? [英] How to change incorrect spelling for the correct word?

查看:43
本文介绍了如何更改正确单词的错误拼写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何正确更改单词的拼写:

How do I correctly change the spelling of the words:

示例使用 1:

?-correct([p,c,o,m,t,e,r,u],X)
X=[c,o,m,p,u,t,e,r]

示例使用 2 :

?-change([u,s,t,e,n,,t,d],X)
Y=[ s,t,u,d,e,n,t]

它将如何运作?这就是我为第一个示例所做的:

How will it work? This is what I did for the first example:

x([c,o,m,p,u,t,e,r]).
y([s,t,u,d,e,n,t]).

correct(Word, CorrectWord) :-
    select(_, Word, CorrectWord),
    x(CorrectWord).

推荐答案

你需要一个单词知识库:

You'll want a knowledge base of words:

word([c,o,m,p,u,t,e,r]).
word([s,t,u,d,e,n,t]).

现在我们需要考虑您要解决的问题,您想知道 WordCorrectWord 是否包含完全相同的字母但顺序不同.所以我建议在比较它们之前使顺序相同:

Now we need to think about the problem you wish to solve, you want to know if Word and CorrectWord both contain exactly the same letters but in a different order. So I suggest making the order the same before comparing them:

correct(Word, CorrectWord) :-
    word(CorrectWord),  % Search through the knowledge base of words
    sort(Word, S), sort(CorrectWord, S).  % Compare the two words

sort/2 是 SWI-Prolog 内置的,比较是通过要求 S 统一为同一个东西,即排序后的词来完成的.这种方法的缺点是不会向后运行,但你也不需要正确\2和更改\2.它还允许您提出这样的问题:

sort/2 is a built-in for SWI-Prolog, the comparison is done by asking for S to unify to the same thing, which is the sorted words. The disadvantage of this method is that it won't run backwards, but you also don't need correct\2 and change\2. It also allows you to ask questions like this:

?- correct([p, c, o, m, t, e, r, u], [c|T]).
T = [o, m, p, u, t, e, r].

对于新的示例案例,[o,o,n,p,u,t,e,r] 并处理奇怪的额外字母,您可以使用类似的度量方法杰卡德系数.intersection/3length/2append/3 都是 SWI-Prolog 内置的.我使用 append/3 作为联合.

For the new example case, [o,o,n,p,u,t,e,r] and to handle the odd extra letter, you can use a measure of similarity like the Jaccard Coefficient. intersection/3, length/2, append/3 are all built-in to SWI-Prolog. I'm using append/3 as union.

jaccard_coef(L1, L2, JC) :-
    intersection(L1, L2, I),
    length(I, LI),
    append(L1, L2, U),
    length(U, LU), 
    JC is LI/LU.

现在你的正确子句变成:

Now your correct clause becomes:

correct(Word, CorrectWord) :-
    word(CorrectWord),  % Search through the knowledge base of words
    sort(Word, S), sort(CorrectWord, S).  % Compare the two words
correct(Word, CorrectWord) :-.
    word(CorrectWord),
    jaccard_coef(Word, CorrectWord, JC),
    JC > 0.4. % Where 0.4 is a number chosen as a threshold.

选择

0.4 以便示例通过.1 将需要完全匹配,根据您要求的严格程度对其进行调整,或者您可以更新方法以找到最大 jaccard_coef 以仅返回最相似的单词而不是一组相似的单词.

0.4 is chosen so the example passes. 1 would require an exact match, adjust it for how strict you require it to be, or you could update the method to find the maximum jaccard_coef to return only the most similar word rather than a selection of similar words.

这篇关于如何更改正确单词的错误拼写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆