def find_with_word(word: str, sentences: List[str]) -> List[str]:
This function checks if a word is in a list of sentences.
:param word: Word to check
:param sentences: List of sentences
:return: List of sentences that contain the word
>>> print(find_with_word('experience', [
... 'Proverbs are short sentences drawn from long experience.',
... 'Naked I came into the world, and naked I must go out.']))
['Proverbs are short sentences drawn from long experience.']
def replace_word(word: str, new_word: str, sentences: List[str]) -> List[str]:
This function replaces a word with a new word in a list of sentences.
:param word: Word to replace
:param new_word: New word to replace with
:param sentences: List of sentences
:return: List of sentences with the replaced word
>>> print(replace_word('experience', 'wisdom', [
... 'Proverbs are short sentences drawn from long experience.',
... 'Naked I came into the world, and naked I must go out.']))
['Proverbs are short sentences drawn from long wisdom.']
class SpecialWordReplacer:
def __init__(self, special_words: List[str], target_word: str, replacement_char: str):
def mask_words(self, text: str) -> str:
This class replace or mask special words between in particular words in a text.
:param special_words: List of special words to replace or mask
:param target_word: Word to replace or mask in special words
:param replacement_char: Character to replace the target word with
>>> replacer = SpecialWordReplacer(['New York', 'El Nino'], ' ', '•')
>>> print(replacer.mask_words('New York is affected by El Nino.'))
New•York is affected by El•Nino.
>>> replacer = SpecialWordReplacer(['"apple"'], 'apple', 'green apple')
>>> print(replacer.mask_words('He said the word "apple" while pointing at an apple.'))
He said the word "green apple" while pointing at an apple.