Main Content

wordcloud

텍스트 데이터에서 워드 클라우드 차트(word cloud chart) 만들기

  • Word cloud chart from text data

설명

예제

wordcloud(tbl,wordVar,sizeVar)은 테이블 tbl에서 워드 클라우드 차트를 만듭니다. 테이블의 변수 wordVarsizeVar은 각각 단어와 단어 크기를 지정합니다.

예제

wordcloud(words,sizeData)words의 요소에서 SizeData로 지정된 단어 크기를 갖는 워드 클라우드 차트를 만듭니다.

예제

wordcloud(C)는 categorical형 배열 C의 고유한 요소에서 워드 클라우드 차트를 만듭니다. 각 요소의 크기는 요소가 나타나는 빈도 수에 비례해서 보여집니다. Text Analytics Toolbox™가 설치되어 있는 경우 C는 string형 배열, 문자형 벡터 또는 문자형 벡터로 구성된 셀형 배열일 수 있습니다.

예제

wordcloud(___,Name,Value)는 하나 이상의 이름-값 쌍의 인수를 사용하여 추가로 WordCloudChart 속성을 지정합니다.

wordcloud(parent,___)parent로 지정된 Figure, 패널 또는 탭에 워드 클라우드를 만듭니다.

wc = wordcloud(___)WordCloudChart 객체를 반환합니다. 워드 클라우드를 생성한 후에 속성을 수정하려면 wc를 사용하십시오. 속성 목록은 WordCloudChart Properties을 참조하십시오.

참고

Text Analytics Toolbox가 있으면 wordcloud(MATLAB®) 함수의 기능을 확장해 사용할 수 있습니다. string형 배열에서 직접 워드 클라우드를 만들고 bag-of-words 모델, bag-of-n-gram 모델 및 LDA 토픽에서 워드 클라우드를 만들 수 있습니다. wordcloud(Text Analytics Toolbox) 함수 도움말 페이지는 wordcloud (Text Analytics Toolbox)를 참조하십시오.

예제

모두 축소

예제 데이터 sonnetsTable을 불러옵니다. 테이블 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  

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을 불러옵니다. 테이블 tblWord 변수에 단어 목록을 포함하고 Count 변수에 그에 대응하는 빈도 수를 포함합니다.

load sonnetsTable
head(tbl)
       Word        Count
    ___________    _____

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

wordcloud를 사용하여 테이블 데이터를 플로팅합니다. 단어와 그에 대응하는 단어 크기를 각각 Word 변수와 Count 변수로 지정합니다. 단어 색을 난수 값으로 설정하기 위해 'Color'를 단어 하나당 행을 하나씩 갖는 확률 행렬 또는 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);

입력 인수

모두 축소

입력 테이블로, 단어와 단어 크기를 지정하는 열을 가집니다. wordVar 입력 인수와 sizeVar 입력 인수로서 주어지는 변수에 단어와 그에 대응하는 단어 크기를 각각 지정하십시오.

데이터형: table

단어 데이터가 들어 있는 테이블 변수로, string형 스칼라, 문자형 벡터, 숫자형 인덱스 또는 논리형 벡터로 지정됩니다.

데이터형: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | string

크기 데이터가 들어 있는 테이블 변수로, string형 스칼라, 문자형 벡터, 숫자형 인덱스 또는 논리형 벡터로 지정됩니다.

데이터형: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | string

입력 categorical형 데이터로, categorical형 배열로 지정됩니다. 함수는 C의 고유한 요소를 histcounts(C)의 크기로 각각 플로팅합니다.

데이터형: categorical

입력 단어로, string형 벡터 또는 문자형 벡터로 구성된 셀형 배열로 지정됩니다.

데이터형: string | cell

단어 크기 데이터로, 숫자형 벡터로 지정됩니다.

데이터형: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

부모 컨테이너로, Figure, Panel, Tab, TiledChartLayout 또는 GridLayout 객체로 지정됩니다.

이름-값 인수

선택적 인수 쌍을 Name1=Value1,...,NameN=ValueN으로 지정합니다. 여기서 Name은 인수 이름이고 Value는 대응값입니다. 이름-값 인수는 다른 인수 뒤에 와야 하지만, 인수 쌍의 순서는 상관없습니다.

R2021a 이전 릴리스에서는 쉼표를 사용하여 각 이름과 값을 구분하고 Name을 따옴표로 묶으십시오.

예: 'HighlightColor','red'는 강조 표시 색을 빨간색으로 설정합니다.

여기에 나와 있는 WordCloudChart 속성은 일부에 불과합니다. 전체 목록을 보려면 WordCloudChart Properties 항목을 참조하십시오.

표시할 단어의 최대 개수로, 음이 아닌 정수로 지정됩니다. 소프트웨어는 가장 큰 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진수 색 코드는 문자형 벡터 또는 string형 스칼라로, 해시 기호(#)로 시작하고 그 뒤에 3자리 또는 6자리의 16진수 숫자(0에서 F 사이일 수 있음)가 옵니다. 이 값은 대/소문자를 구분하지 않습니다. 따라서 색 코드 "#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진수 색 코드는 문자형 벡터 또는 string형 스칼라로, 해시 기호(#)로 시작하고 그 뒤에 3자리 또는 6자리의 16진수 숫자(0에서 F 사이일 수 있음)가 옵니다. 이 값은 대/소문자를 구분하지 않습니다. 따라서 색 코드 "#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 Properties을 참조하십시오.

Text Analytics Toolbox가 있으면 wordcloud(MATLAB) 함수의 기능을 확장해 사용할 수 있습니다. string형 배열에서 직접 워드 클라우드를 만들고 bag-of-words 모델, bag-of-n-gram 모델 및 LDA 토픽에서 워드 클라우드를 만들 수 있습니다. wordcloud(Text Analytics Toolbox) 함수 도움말 페이지는 wordcloud (Text Analytics Toolbox)를 참조하십시오.

확장 기능

버전 내역

R2017b에 개발됨