Main Content

wordcloud

テキスト データからワード クラウド チャートを作成

  • Word cloud chart from text data

説明

wordcloud(tbl,wordVar,sizeVar) は、テーブル tbl からワード クラウド チャートを作成します。table の変数である wordVarsizeVar で単語とワード サイズをそれぞれ指定します。

wordcloud(words,sizeData) は、SizeData で指定されたワード サイズを使用して、words の要素からワード クラウド チャートを作成します。

wordcloud(C) は、頻度数に対応するサイズを使用して、categorical 配列 C の一意の要素からワード クラウド チャートを作成します。Text Analytics Toolbox™ がある場合は、C に string 配列、文字ベクトル、文字ベクトルの cell 配列を指定できます。

wordcloud(___,Name,Value) は、名前と値のペアの引数を 1 つ以上使用して、追加の WordCloudChart プロパティを指定します。

wordcloud(parent,___) は、parent で指定された Figure、パネルまたはタブにワード クラウドを作成します。

wc = wordcloud(___) は、WordCloudChart オブジェクトを返します。ワード クラウドのプロパティを作成後に変更するには、wc を使用します。プロパティの一覧については、WordCloudChart のプロパティ を参照してください。

メモ

Text Analytics Toolbox は、MATLAB® 関数 wordcloud の機能を拡張します。これにより、string 配列からのワード クラウドの直接作成、および bag of words モデル、bag of n gram モデル、LDA トピックからのワード クラウドの作成のサポートが追加されます。wordcloud (Text Analytics Toolbox) リファレンス ページについては、wordcloud (Text Analytics Toolbox) を参照してください。

すべて折りたたむ

サンプル データ sonnetsTable を読み込みます。table tbl には、変数 Word に単語のリストが格納され、対応する頻度数が変数 Count に格納されています。

load sonnetsTable
head(tbl)
       Word        Count
    ___________    _____

    {'''tis'  }      1  
    {''Amen'' }      1  
    {''Fair'  }      2  
    {''Gainst'}      1  
    {''Since' }      1  
    {''This'  }      2  
    {''Thou'  }      1  
    {''Thus'  }      1  

table データを wordcloud を使用してプロットします。単語と対応するワード サイズを変数 Word と変数 Count にそれぞれ指定します。

figure
wordcloud(tbl,'Word','Count');
title("Sonnets Word Cloud")

Figure contains an object of type wordcloud. The chart of type wordcloud has title Sonnets Word Cloud.

Text Analytics Toolbox™ がインストールされている場合は、string 配列から直接ワード クラウドを作成できます。詳細については、wordcloud (Text Analytics Toolbox)を参照してください。Text Analytics Toolbox がない場合は、テキスト データの前処理を手動で行わなければなりません。

この例では、プレーン テキストから string 配列に読み取り、前処理して、それを関数 wordcloud に渡して、ワード クラウドを作成する方法を示します。

関数 fileread を使用して、シェークスピアのソネットからテキストを読み取り、string に変換します。

sonnets = string(fileread("sonnets.txt"));
extractBefore(sonnets,"II")
ans = 
    "THE SONNETS
     
     by William Shakespeare
     
     
     
     
       I
     
       From fairest creatures we desire increase,
       That thereby beauty's rose might never die,
       But as the riper should by time decease,
       His tender heir might bear his memory:
       But thou, contracted to thine own bright eyes,
       Feed'st thy light's flame with self-substantial fuel,
       Making a famine where abundance lies,
       Thy self thy foe, to thy sweet self too cruel:
       Thou that art now the world's fresh ornament,
       And only herald to the gaudy spring,
       Within thine own bud buriest thy content,
       And tender churl mak'st waste in niggarding:
         Pity the world, or else this glutton be,
         To eat the world's due, by the grave and thee.
     
       "

sonnets を、個々の単語を要素として含む string 配列に分割します。これを行うには、句読点を削除し、すべての string 要素を 1 行 1 列の string に結合してから、空白文字の位置で分割します。その後、5 文字未満の単語を削除し、単語を小文字に変換します。

punctuationCharacters = ["." "?" "!" "," ";" ":"];
sonnets = replace(sonnets,punctuationCharacters," ");
words = split(join(sonnets));
words(strlength(words)<5) = [];
words = lower(words);
words(1:10)
ans = 10x1 string
    "sonnets"
    "william"
    "shakespeare"
    "fairest"
    "creatures"
    "desire"
    "increase"
    "thereby"
    "beauty's"
    "might"

sonnets を categorical 配列に変換して、wordcloud を使用してプロットします。この関数は C の一意の要素を、その頻度数に対応するサイズでプロットします。

C = categorical(words);
figure
wordcloud(C);
title("Sonnets Word Cloud")

Figure contains an object of type wordcloud. The chart of type wordcloud has title Sonnets Word Cloud.

プレーン テキストから string 配列に読み取り、前処理して、それを関数 wordcloud に渡して、ワード クラウドを作成します。

関数 fileread を使用して、シェークスピアのソネットからテキストを読み取り、string に変換します。

sonnets = string(fileread('sonnets.txt'));
extractBefore(sonnets,"II")
ans = 
    "THE SONNETS
     
     by William Shakespeare
     
     
     
     
       I
     
       From fairest creatures we desire increase,
       That thereby beauty's rose might never die,
       But as the riper should by time decease,
       His tender heir might bear his memory:
       But thou, contracted to thine own bright eyes,
       Feed'st thy light's flame with self-substantial fuel,
       Making a famine where abundance lies,
       Thy self thy foe, to thy sweet self too cruel:
       Thou that art now the world's fresh ornament,
       And only herald to the gaudy spring,
       Within thine own bud buriest thy content,
       And tender churl mak'st waste in niggarding:
         Pity the world, or else this glutton be,
         To eat the world's due, by the grave and thee.
     
       "

sonnets を、個々の単語を要素として含む string 配列に分割します。これを行うには、句読点を削除し、すべての string 要素を 1 行 1 列の string に結合してから、空白文字の位置で分割します。その後、5 文字未満の単語を削除し、単語を小文字に変換します。

punctuationCharacters = ["." "?" "!" "," ";" ":"];
sonnets = replace(sonnets,punctuationCharacters," ");
words = split(join(sonnets));
words(strlength(words)<5) = [];
words = lower(words);
words(1:10)
ans = 10×1 string
    "sonnets"
    "william"
    "shakespeare"
    "fairest"
    "creatures"
    "desire"
    "increase"
    "thereby"
    "beauty's"
    "might"

sonnets の一意の単語を検索し、出現頻度をカウントします。頻度数をサイズ データとして使用して、ワード クラウドを作成します。

[numOccurrences,uniqueWords] = histcounts(categorical(words));
figure
wordcloud(uniqueWords,numOccurrences);
title("Sonnets Word Cloud")

Figure contains an object of type wordcloud. The chart of type wordcloud has title Sonnets Word Cloud.

サンプル データ sonnetsTable を読み込みます。table tbl には、変数 Word に単語のリストが格納され、対応する頻度数が変数 Count に格納されています。

load sonnetsTable
head(tbl)
       Word        Count
    ___________    _____

    {'''tis'  }      1  
    {''Amen'' }      1  
    {''Fair'  }      2  
    {''Gainst'}      1  
    {''Since' }      1  
    {''This'  }      2  
    {''Thou'  }      1  
    {''Thus'  }      1  

table データを wordcloud を使用してプロットします。単語と対応するワード サイズを変数 Word と変数 Count にそれぞれ指定します。単語の色を乱数値に設定するには、'Color' を、単語ごとに 1 つの行をもつ乱数行列または RGB 3 成分に設定します。

numWords = size(tbl,1);
colors = rand(numWords,3);
figure
wordcloud(tbl,'Word','Count','Color',colors);
title("Sonnets Word Cloud")

Figure contains an object of type wordcloud. The chart of type wordcloud has title Sonnets Word Cloud.

Text Analytics Toolbox がインストールされている場合は、string 配列から直接ワード クラウドを作成できます。Text Analytics Toolbox がない場合は、テキスト データの前処理を手動で行わなければなりません。Text Analytics Toolbox なしでワード クラウドを作成する方法の例については、ワード クラウドのテキスト データの準備を参照してください。

extractFileText を使用して sonnets.txt からテキストを抽出します。

str = extractFileText("sonnets.txt");
extractBefore(str,"II")
ans = 

    "THE SONNETS
     
     by William Shakespeare
     
     
     
     
       I
     
       From fairest creatures we desire increase,
       That thereby beauty's rose might never die,
       But as the riper should by time decease,
       His tender heir might bear his memory:
       But thou, contracted to thine own bright eyes,
       Feed'st thy light's flame with self-substantial fuel,
       Making a famine where abundance lies,
       Thy self thy foe, to thy sweet self too cruel:
       Thou that art now the world's fresh ornament,
       And only herald to the gaudy spring,
       Within thine own bud buriest thy content,
       And tender churl mak'st waste in niggarding:
         Pity the world, or else this glutton be,
         To eat the world's due, by the grave and thee.
     
       "

ソネットの単語をワード クラウドで表示します。

figure
wordcloud(str);

入力引数

すべて折りたたむ

単語とワード サイズを指定する列を含む入力 table。入力引数の wordVarsizeVar で指定される変数で単語と対応するワード サイズをそれぞれ指定します。

データ型: table

単語データの table 変数。string スカラー、文字ベクトル、数値インデックス、または logical ベクトルとして指定します。

データ型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | string

サイズ データの table 変数。string スカラー、文字ベクトル、数値インデックス、または logical ベクトルとして指定します。

データ型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | string

入力 categorical データ。categorical 配列として指定します。この関数は、C の一意の各要素を、histcounts(C) に対応するサイズでプロットします。

データ型: categorical

入力語。string ベクトル、または文字ベクトルの cell 配列として指定します。

データ型: string | cell

ワード サイズ データ。数値ベクトルとして指定します。

データ型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

親コンテナー。FigurePanelTabTiledChartLayout、または GridLayout オブジェクトとして指定します。

名前と値の引数

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

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

例: 'HighlightColor','red' は強調表示の色を赤に設定します。

ここでは、WordCloudChart プロパティの一部だけを紹介しています。完全な一覧については、WordCloudChart のプロパティを参照してください。

表示する単語の最大数。非負の整数として指定します。最大 MaxDisplayWords の数の単語が表示されます。

単語の色。RGB 3 成分、色名を含む文字ベクトル、または N 行 3 列の行列 (NWordData の長さ) として指定します。Color が行列の場合、各行は WordData の対応する単語の RGB 3 成分に対応します。

RGB 3 成分および 16 進数カラー コードは、カスタム色を指定するのに役立ちます。

  • RGB 3 成分は、色の赤、緑、青成分の強度を指定する 3 成分の行ベクトルです。強度値は [0,1] の範囲でなければなりません。たとえば [0.4 0.6 0.7] のようになります。

  • 16 進数カラー コードは、ハッシュ記号 (#) で始まり、3 桁または 6 桁の 0 から F までの範囲の 16 進数が続く文字ベクトルまたは string スカラーです。この値は大文字と小文字を区別しません。したがって、カラー コード "#FF8800""#ff8800""#F80"、および "#f80" は等価です。

あるいは、名前を使用して一部の一般的な色を指定できます。次の表に、名前の付いた色オプション、等価の RGB 3 成分、および 16 進数カラー コードを示します。

色名省略名RGB 3 成分16 進数カラー コード外観
"red""r"[1 0 0]"#FF0000"

Sample of the color red

"green""g"[0 1 0]"#00FF00"

Sample of the color green

"blue""b"[0 0 1]"#0000FF"

Sample of the color blue

"cyan" "c"[0 1 1]"#00FFFF"

Sample of the color cyan

"magenta""m"[1 0 1]"#FF00FF"

Sample of the color magenta

"yellow""y"[1 1 0]"#FFFF00"

Sample of the color yellow

"black""k"[0 0 0]"#000000"

Sample of the color black

"white""w"[1 1 1]"#FFFFFF"

Sample of the color white

MATLAB の多くのタイプのプロットで使用されている既定の色の RGB 3 成分および 16 進数カラー コードを次に示します。

RGB 3 成分16 進数カラー コード外観
[0 0.4470 0.7410]"#0072BD"

Sample of RGB triplet [0 0.4470 0.7410], which appears as dark blue

[0.8500 0.3250 0.0980]"#D95319"

Sample of RGB triplet [0.8500 0.3250 0.0980], which appears as dark orange

[0.9290 0.6940 0.1250]"#EDB120"

Sample of RGB triplet [0.9290 0.6940 0.1250], which appears as dark yellow

[0.4940 0.1840 0.5560]"#7E2F8E"

Sample of RGB triplet [0.4940 0.1840 0.5560], which appears as dark purple

[0.4660 0.6740 0.1880]"#77AC30"

Sample of RGB triplet [0.4660 0.6740 0.1880], which appears as medium green

[0.3010 0.7450 0.9330]"#4DBEEE"

Sample of RGB triplet [0.3010 0.7450 0.9330], which appears as light blue

[0.6350 0.0780 0.1840]"#A2142F"

Sample of RGB triplet [0.6350 0.0780 0.1840], which appears as dark red

例: 'blue'

例: [0 0 1]

単語の強調表示の色。RGB 3 成分、または色名を含む文字ベクトルとして指定します。サイズが最も大きい単語がこの色で強調表示されます。

RGB 3 成分および 16 進数カラー コードは、カスタム色を指定するのに役立ちます。

  • RGB 3 成分は、色の赤、緑、青成分の強度を指定する 3 成分の行ベクトルです。強度値は [0,1] の範囲でなければなりません。たとえば [0.4 0.6 0.7] のようになります。

  • 16 進数カラー コードは、ハッシュ記号 (#) で始まり、3 桁または 6 桁の 0 から F までの範囲の 16 進数が続く文字ベクトルまたは string スカラーです。この値は大文字と小文字を区別しません。したがって、カラー コード "#FF8800""#ff8800""#F80"、および "#f80" は等価です。

あるいは、名前を使用して一部の一般的な色を指定できます。次の表に、名前の付いた色オプション、等価の RGB 3 成分、および 16 進数カラー コードを示します。

色名省略名RGB 3 成分16 進数カラー コード外観
"red""r"[1 0 0]"#FF0000"

Sample of the color red

"green""g"[0 1 0]"#00FF00"

Sample of the color green

"blue""b"[0 0 1]"#0000FF"

Sample of the color blue

"cyan" "c"[0 1 1]"#00FFFF"

Sample of the color cyan

"magenta""m"[1 0 1]"#FF00FF"

Sample of the color magenta

"yellow""y"[1 1 0]"#FFFF00"

Sample of the color yellow

"black""k"[0 0 0]"#000000"

Sample of the color black

"white""w"[1 1 1]"#FFFFFF"

Sample of the color white

MATLAB の多くのタイプのプロットで使用されている既定の色の RGB 3 成分および 16 進数カラー コードを次に示します。

RGB 3 成分16 進数カラー コード外観
[0 0.4470 0.7410]"#0072BD"

Sample of RGB triplet [0 0.4470 0.7410], which appears as dark blue

[0.8500 0.3250 0.0980]"#D95319"

Sample of RGB triplet [0.8500 0.3250 0.0980], which appears as dark orange

[0.9290 0.6940 0.1250]"#EDB120"

Sample of RGB triplet [0.9290 0.6940 0.1250], which appears as dark yellow

[0.4940 0.1840 0.5560]"#7E2F8E"

Sample of RGB triplet [0.4940 0.1840 0.5560], which appears as dark purple

[0.4660 0.6740 0.1880]"#77AC30"

Sample of RGB triplet [0.4660 0.6740 0.1880], which appears as medium green

[0.3010 0.7450 0.9330]"#4DBEEE"

Sample of RGB triplet [0.3010 0.7450 0.9330], which appears as light blue

[0.6350 0.0780 0.1840]"#A2142F"

Sample of RGB triplet [0.6350 0.0780 0.1840], which appears as dark red

例: 'blue'

例: [0 0 1]

ワード クラウド チャートの形状。'oval' または 'rectangle' として指定します。

例: 'rectangle'

単語配置レイアウト。非負の整数として指定します。同じ入力を使用して wordcloud を繰り返し呼び出すと、単語配置レイアウトは毎回同じになります。別の単語配置レイアウトを取得するには、異なる LayoutNum 値を使用します。

出力引数

すべて折りたたむ

WordCloudChart オブジェクト。WordCloudChart の作成後にプロパティを変更することができます。詳細については、WordCloudChart のプロパティ を参照してください。

ヒント

Text Analytics Toolbox は、MATLAB 関数 wordcloud の機能を拡張します。これにより、string 配列からのワード クラウドの直接作成、および bag of words モデル、bag of n gram モデル、LDA トピックからのワード クラウドの作成のサポートが追加されます。wordcloud (Text Analytics Toolbox) リファレンス ページについては、wordcloud (Text Analytics Toolbox) を参照してください。

拡張機能

バージョン履歴

R2017b で導入