Part 2: Find single insertion or deletion 


Мы поможем в написании ваших работ!



ЗНАЕТЕ ЛИ ВЫ?

Part 2: Find single insertion or deletion

Find mismatch

(30/30 points)

Write a function named find_mismatch that accepts two strings as input arguments and returns:

  • 0 if the two strings match exactly.
  • 1 if the two strings have the same length and mismatch in only one character.
  • 2 if the two strings do not have the same length or mismatch in two or more characters.

Capital letters are considered the same as lower case letters. Here are some examples:

First string

Second String

Function return

Python

Java

Hello There

helloothere

sin

sink

2 (note not the same length)

dog

Dog

 

Part 2: Find single insertion or deletion

(30/30 points)

Write a function named single_insert_or_delete that accepts two strings as input arguments and returns:

  • 0 if the two strings match exactly.
  • 1 if the first string can become the same as the second string by inserting or deleting a single character. Notice that inserting and deleting a character is not the same as replacing a character.
  • 2 otherwise

Capital letters are considered the same as lower case letters. Here are some examples:

First string

Second String

Function return

Python

Java

book

boot

sin

sink

1 (Inserting a single character at the end)

dog

Dog

poke

spoke

1 (Inserting a single character at the start)

poker

poke

1 (Deleting a single character from the end)

programing

programming

1 (Inserting a single character)

Part 3: Spelling corrector

(40 points possible)

Write a function named spelling_corrector that accepts two arguments. The first argument is a sentence (string) and the second argument is a list of words (correct_spells). Your function should check each word in the input string against all the words in the correct_spells list and return a string such that:

  • If a word in the original sentence matches exactly with a word in the correct_spells then the word is not modified and it should be directly copied to the output string.
  • if a word in the sentence can match a word in the correct_spells list by replacing, inserting, or deleting a single character, then that word should be replaced by the correct word in the correct_spelled list.
  • If neither of the two previous conditions is true, then the word in the original string should not be modified and should be directly copied to the output string.

Notes:

  • Do not spell check one or two letter words (copy them directly to the output string).
  • In case of a tie use the first word from the correct_spelled list.
  • Ignore capitalization, i.e. consider capital letters to be the same as lower case letters.
  • All characters in the output string should all be in lower case letters.
  • Assume that the input string only includes alphabetic characters and spaces. (a-z and A-Z)
  • Remove extra spaces between the words.
  • Remove spaces at the start and end of the output string.

Examples:

Sentence (str)

correct_spells (list)

Function return (str)

Thes is the Firs cas

['that','first','case','car']

thes is the first case

programing is fan and eesy

['programming','this','fun','easy','book' ]

programming is fun and easy

Thes is vary essy

['this', 'is', 'very', 'very', 'easy']

this is very easy

Wee lpve Pythen

['we', 'Live', 'In', 'Python']

we live python

Notice:

  • In the first example 'thes' is not replaced with anything.
  • In the first example both 'case' and 'car' could replace the 'cas' in the original sentence, but 'case' is selected because it was encountered first.

Please notice that this assignment is only an exercise and a real spell checker requires more functionalities.

Hint: You should use the functions that you developed in part 1 and part 2 to help you solve this problem.

 



Поделиться:


Последнее изменение этой страницы: 2024-07-06; просмотров: 41; Нарушение авторского права страницы; Мы поможем в написании вашей работы!

infopedia.su Все материалы представленные на сайте исключительно с целью ознакомления читателями и не преследуют коммерческих целей или нарушение авторских прав. Обратная связь - 216.73.216.198 (0.005 с.)