メインコンテンツ

replace

文書内の部分文字列の置き換え

説明

newDocuments = replace(documents,old,new) は、documents に出現する old という部分文字列またはパターンをすべて new に置き換えます。

ヒント

replace 関数を使用し、部分文字列またはパターンを指定して、文書内にある単語の部分文字列を置き換えます。文書内にある単語全体および n-gram を置き換えるには、それぞれ replaceWords 関数と replaceNgrams 関数を使用します。

すべて折りたたむ

文書配列内の単語を置き換えます。

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

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

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

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

単語の部分文字列を置き換えます。

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

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

数字のパターンを使用して文書から数字を削除します。

トークン化された文書の配列を作成します。

textData = [
    "Text Analytics Toolbox provides over 50 functions to analyze text data."
    "The bm25Similarity function measures document similarity."];
documents = tokenizedDocument(textData);

replace 関数を使用して、数字が連続している箇所をトークン "<NUMBER>" に置き換えます。digitsPattern 関数を使用して数字のパターンを指定します。

pat = digitsPattern;
newDocuments = replace(documents,pat,"<NUMBER>")
newDocuments = 
  2×1 tokenizedDocument:

    12 tokens: Text Analytics Toolbox provides over <NUMBER> functions to analyze text data .
     7 tokens: The bm<NUMBER>Similarity function measures document similarity .

関数によってトークン "bm25Similarity" 内の数字が置き換えられることに注意してください。

数字のみで構成されるトークンを置き換えるには、replace 関数を使用し、テキストの境界も含むパターンを指定します。textBoundary 関数を使用してテキストの境界を指定します。

pat = textBoundary + digitsPattern + textBoundary;
newDocuments = replace(documents,pat,"<NUMBER>")
newDocuments = 
  2×1 tokenizedDocument:

    12 tokens: Text Analytics Toolbox provides over <NUMBER> functions to analyze text data .
     7 tokens: The bm25Similarity function measures document similarity .

この場合、関数はトークン "bm25Similarity" 内の数字を置き換えません。

入力引数

すべて折りたたむ

入力文書。tokenizedDocument 配列として指定します。

置き換える部分文字列またはパターン。次のいずれかとして指定します。

  • string 配列

  • 文字ベクトル

  • 文字ベクトルの cell 配列

  • pattern 配列

新しい部分文字列。string 配列、文字ベクトル、または文字ベクトルの cell 配列として指定します。

データ型: string | char | cell

出力引数

すべて折りたたむ

出力文書。tokenizedDocument 配列として返されます。

バージョン履歴

R2017b で導入