Simple spell checker

Xidian University's Python programming experiment on the computer - a simple spelling check program

The whole program consists of 3 parts:

① In the first part, write a function to compare 2 strings, and then return the values ​​0, 1, 2 according to the conditions.

② In the second part, write a function to check whether a string can match another string while inserting or deleting a character.

③ In the third part, write a function that checks the spelling of a sentence (string) with a list of correct words.

Specific steps are as follows:

(1) In the first part, write a function called find_mismatch that receives 2 strings as parameters and returns:

0: If the 2 strings match exactly.

1: If the 2 strings have the same length and only one character does not match.

2: If the length of the 2 strings is not the same or there are 2 or more characters that do not match.

Case-insensitive, that is, the case of the same character is considered to match. Here are some examples:

Please add a picture description

(1) In the second part, write a function named single_insert_or_delete, which receives 2 strings as parameters and returns:

0: if the 2 strings match exactly;

1: If the first string can match the second string exactly after inserting or deleting one character. Note: Inserting or deleting a character is not the same as replacing a character; replacement is not considered here;

2: Other cases.

Case-insensitive, that is, the case of the same character is considered to match. Here are some examples:

Please add a picture description

In the figure above, the function returns only numbers, and the () is just an explanation, not the return value of the function.

(2) In the third part, write a function named spelling_corrector that receives 2 parameters. The first parameter is a sentence (string), and the second parameter is a list of correct words. The function checks whether each word in the sentence matches a word in the list of correctly spelled words and returns a string: a. If the word in the sentence matches a word in the list exactly, then the word in the sentence should follow Output as it is; b. If the word in the sentence matches the word in the list by replacing, inserting, or deleting a character, then the word in the sentence should be replaced by the correct word in the list; c. If the word in the sentence is not If the above two conditions are met, then the words in the sentence should be output as they are.

Notice:

l Do not check words with only 1 or 2 characters in the sentence, and output them as they are;

l In order to avoid more than 2 matching situations, please use the first matching word in the word list;

l Ignore case;

l The return value string of the function, all words are output in lowercase;

l Assume that the sentence only includes 26 uppercase English letters and lowercase English letters; (az and AZ)

l Remove redundant spaces between words in sentences;

l Remove the extra space at the first character string of the function return value.

Example:

Please add a picture description

The functions of the third part call the functions of the first part and the second part.

Note that this program is just an exercise, real spell checking programs are much more complex than this and require more features.

def find_mismatch(s1, s2):
    cnt, lst1, lst2 = 0, [], []
    for i in s1.lower():
        lst1.append(i)
    for i in s2.lower():
        lst2.append(i)
    if s1.lower() == s2.lower():
        return 0
    if len(s1) == len(s2):
        for i in range(len(s1)):
            if lst1[i] != lst2[i]:
                cnt += 1
        if cnt == 1:
            return 1
        if cnt >= 2:
            return 2
    else:
        return 2


def single_insert_or_delete(m, n):
    mlist, nlist = [], []
    for i in m.lower():
        mlist.append(i)
    for i in n.lower():
        nlist.append(i)
    if len(m) == len(n):
        for i in range(len(m)):
            if mlist[i] != nlist[i]:
                return 2
        return 0
    if abs(len(m) - len(n)) == 1:
        for t in range(10):
            for i in mlist:
                for j in nlist:
                    if i == j:
                        try:
                            mlist.remove(i)
                            nlist.remove(j)
                        except Exception:
                            continue
        if abs(len(mlist) - len(nlist)) and (len(mlist) == 0 or len(nlist) == 0) == 1:
            return 1
        return 2
    if abs(len(m) - len(n)) != 0 and abs(len(m) - len(n)) != 1:
        return 2


def spelling_corrector(string, lst):
    words, word_list, s = string.lower().split(' '), [], ''
    for i in lst:
        word_list.append(i.lower())
    for i in range(len(words)):
        for j in word_list:
            if find_mismatch(words[i], j) == 1 or single_insert_or_delete(words[i], j) == 1:
                words[i] = j
    for i in words:
        s += i
        s += ' '
    return s


# print(find_mismatch('Python', 'Java'))  # 2
# print(find_mismatch('Hello there', 'helloothere'))  # 1
# print(find_mismatch('dog', 'Dog'))  # 0

# print(single_insert_or_delete('Python', 'Java'))  # 2
# print(single_insert_or_delete('book', 'boot'))  # 2
# print(single_insert_or_delete('sin', 'sink'))  # 1
# print(single_insert_or_delete('Dog', 'dog'))  # 0
# print(single_insert_or_delete('poke', 'spoke'))  # 1

print(spelling_corrector('Thes is the Firs cas', ['that', 'first', 'case', 'car']))
print(spelling_corrector('programing is fan and eesy', ['programming', 'this', 'fun', 'easy', 'book']))
print(spelling_corrector('Thes is vary essy', ['this', 'is', 'very', 'very', 'easy']))
print(spelling_corrector('Wee lpve Pythen', ['we', 'Live', 'In', 'Python']))

is vary essy’, [‘this’, ‘is’, ‘very’, ‘very’, ‘easy’]))
print(spelling_corrector(‘Wee lpve Pythen’, [‘we’, ‘Live’, ‘In’, ‘Python’]))


Guess you like

Origin blog.csdn.net/wwx1239021388/article/details/129392383