replace

Replace substrings in documents

Use replace to replace substrings of the words in documents. To replace entire words and n-grams in documents, use the replaceWords and replaceNgrams functions respectively.

Syntax

newDocuments = replace(documents,old,new)

Description

example

newDocuments = replace(documents,old,new) replaces all occurrences of old in documents with new.

Examples

collapse all

Replace words in a document array.

documents = tokenizedDocument([
    "an extreme example"
    "another extreme example"])
documents = 
  2x1 tokenizedDocument:

    3 tokens: an extreme example
    3 tokens: another extreme example

newDocuments = replace(documents,"example","sentence")
newDocuments = 
  2x1 tokenizedDocument:

    3 tokens: an extreme sentence
    3 tokens: another extreme sentence

Replace substrings of the words.

newDocuments = replace(documents,"ex","X-")
newDocuments = 
  2x1 tokenizedDocument:

    3 tokens: an X-treme X-ample
    3 tokens: another X-treme X-ample

Input Arguments

collapse all

Input documents, specified as a tokenizedDocument array.

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

Data Types: string | char | cell

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

Data Types: string | char | cell

Output Arguments

collapse all

Output documents, returned as a tokenizedDocument array.

Introduced in R2017b