Main Content

replaceWords

Replace words in documents

Description

example

newDocuments= replaceWords(documents,oldWords,newWords)updates the specified documents by replacing the words inoldWordswith the corresponding words innewWords. The function, by default, is case sensitive.

newDocuments= replaceWords(documents,oldWords,newWords,'IgnoreCase',true)replaces the words inoldWordsignoring case.

Examples

collapse all

Use thereplaceWordsfunction to replace shorthand words with their corresponding full words.

Create an array of tokenized documents.

str = [..."Increased activity Mon to Fri.""Reduced activity Sat to Sun."]; documents = tokenizedDocument(str)
documents = 2x1 tokenizedDocument: 6 tokens: Increased activity Mon to Fri . 6 tokens: Reduced activity Sat to Sun .

Replace the shorthand words with their corresponding full words.

oldWords = ["Mon""Tue""Wed""Thu""Fri""Sat""Sun"]; newWords = ["Monday""Tuesday""Wednesday""Thursday""Friday""Saturday""Sunday"]; documents = replaceWords(documents,oldWords,newWords)
documents = 2x1 tokenizedDocument: 6 tokens: Increased activity Monday to Friday . 6 tokens: Reduced activity Saturday to Sunday .

Input Arguments

collapse all

Input documents, specified as atokenizedDocumentarray.

Words to replace, specified as a string array, character vector, or cell array of character vectors.

Data Types:string|char|cell

New words, specified as a string array, character vector, or cell array of character vectors.

newWordsmust contain one word or be the same size asoldWords. IfnewWordscontains only one word, then the function replaces all the words inoldWordswith this word.

Data Types:string|char|cell

Output Arguments

collapse all

Output documents, returned as atokenizedDocumentarray.

Tips

  • To replace words in documents by specifyingpatternarrays, use thereplacefunction.

Introduced in R2019a