メインコンテンツ

encode

R2026b

文書を単語または n-gram のカウントの行列として符号化する

説明

encode を使用し、bag-of-words モデルまたは bag-of-n-grams モデルに従って、トークン化された文書の配列を単語または n-gram のカウントの行列として符号化します。文書をベクトルまたは単語インデックスとして符号化するには、wordEncoding オブジェクトを使用します。

counts = encode(bag,documents) は、bag-of-words モデルまたは bag-of-n-grams モデル bag に基づいて、documents の頻度カウントの行列を返します。

例

counts = encode(bag,words) は、単語リストの頻度カウントの行列を返します。

例

counts = encode(___,Name,Value) は、1 つ以上の名前と値のペアの引数を使用して、追加のオプションを指定します。

例

例

すべて折りたたむ

文書の配列を単語カウントの行列として符号化します。

documents = tokenizedDocument([
    "an example of a short sentence" 
    "a second short sentence"]);
bag = bagOfWords(documents) 
bag = 
  bagOfWords with properties:

        NumWords: 7
          Counts: [2×7 double]
      Vocabulary: ["an"    "example"    "of"    "a"    "short"    "sentence"    "second"]
    NumDocuments: 2

documents = tokenizedDocument([
    "a new sentence" 
    "a second new sentence"])
documents = 
  2×1 tokenizedDocument:

    3 tokens: a new sentence
    4 tokens: a second new sentence

単語カウントの行列として符号化された文書を表示します。"new" という単語は bag に含まれていないため、カウントされません。

counts = encode(bag,documents);
full(counts)
ans = 2×7

     0     0     0     1     0     1     0
     0     0     0     1     0     1     1

列は bag-of-words モデルのボキャブラリに対応します。

bag.Vocabulary
ans = 1×7 string
    "an"    "example"    "of"    "a"    "short"    "sentence"    "second"

単語の配列を単語カウントのベクトルとして符号化します。

documents = tokenizedDocument([
    "an example of a short sentence"
    "a second short sentence"]);
bag = bagOfWords(documents)
bag = 
  bagOfWords with properties:

        NumWords: 7
          Counts: [2×7 double]
      Vocabulary: ["an"    "example"    "of"    "a"    "short"    "sentence"    "second"]
    NumDocuments: 2

words = ["another" "example" "of" "a" "short" "example" "sentence"];
counts = encode(bag,words)
counts = 1×7 sparse double row vector (5 nonzeros)
   (1,2)        2
   (1,3)        1
   (1,4)        1
   (1,5)        1
   (1,6)        1

文書の配列を文書を列とする単語カウントの行列として符号化します。

documents = tokenizedDocument([
    "an example of a short sentence"
    "a second short sentence"]);
bag = bagOfWords(documents)
bag = 
  bagOfWords with properties:

        NumWords: 7
          Counts: [2×7 double]
      Vocabulary: ["an"    "example"    "of"    "a"    "short"    "sentence"    "second"]
    NumDocuments: 2

documents = tokenizedDocument([
    "a new sentence"
    "a second new sentence"])
documents = 
  2×1 tokenizedDocument:

    3 tokens: a new sentence
    4 tokens: a second new sentence

文書を列とする単語カウントの行列として符号化された文書を表示します。"new" という単語は bag に含まれていないため、カウントされません。

counts = encode(bag,documents,'DocumentsIn','columns');
full(counts)
ans = 7×2

     0     0
     0     0
     0     0
     1     1
     0     0
     1     1
     0     1

入力引数

すべて折りたたむ

入力の bag-of-words モデルまたは bag-of-n-grams モデル。bagOfWords オブジェクトまたは bagOfNgrams オブジェクトとして指定します。

入力文書。tokenizedDocument 配列、単語の string 配列、または文字ベクトルの cell 配列として指定します。documents は、string 配列または文字ベクトルの cell 配列である場合、各要素が単語である単一の文書を表す行ベクトルでなければなりません。

ヒント

文書が必ず正しく符号化されるようにするには、入力モデルの作成に使用した文書と同じステップを使用して、入力文書を前処理しなければなりません。テキスト データを前処理する関数を作成する方法を示す例については、解析用のテキスト データの準備を参照してください。

入力単語。string ベクトル、文字ベクトル、または文字ベクトルの cell 配列として指定します。words を文字ベクトルとして指定した場合、関数は引数を 1 つの単語として扱います。

データ型: string | char | cell

名前と値の引数

すべて折りたたむ

オプションの引数のペアを Name1=Value1,...,NameN=ValueN として指定します。ここで、Name は引数名で、Value は対応する値です。名前と値の引数は他の引数の後に指定しなければなりませんが、ペアの順序は重要ではありません。

R2021a より前では、コンマを使用して名前と値をそれぞれ区切り、Name を引用符で囲みます。

例: 'DocumentsIn','rows' は出力文書の方向を行として指定します。

頻度カウント行列での出力文書の方向。'DocumentsIn' と次のいずれかで構成されるコンマ区切りのペアとして指定します。

  • 'rows' – 文書に対応する行をもつ頻度カウントの行列を返します。

  • 'columns' – 文書に対応する列をもつ頻度カウントの転置行列を返します。

データ型: char

cell 配列として出力を返すように強制するインジケーター。'ForceCellOutput' および true または false で構成されるコンマ区切りのペアとして指定します。

データ型: logical

出力引数

すべて折りたたむ

単語または n-gram のカウント。非負の整数のスパース行列またはスパース行列の cell 配列として返されます。

bag が非スカラー配列であるか 'ForceCellOutput' が true の場合、関数は出力をスパース行列の cell 配列として返します。cell 配列の各要素は、bag の対応する要素の単語カウントまたは n-gram カウントの行列です。

バージョン履歴

R2017b で導入