Main Content

replaceWords

Replace words in documents

Since R2019a

Description

example

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

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

Examples

collapse all

Use the replaceWords function 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 a tokenizedDocument array.

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.

newWords must contain one word or be the same size as oldWords. If newWords contains only one word, then the function replaces all the words in oldWords with this word.

Data Types: string | char | cell

Output Arguments

collapse all

Output documents, returned as a tokenizedDocument array.

Tips

  • To replace words in documents by specifying pattern arrays, use the replace function.

Version History

Introduced in R2019a