Main Content

이 번역 페이지는 최신 내용을 담고 있지 않습니다. 최신 내용을 영문으로 보려면 여기를 클릭하십시오.

bagOfWords

Bag-of-words 모델

설명

bag-of-words 모델(단어 빈도 카운터라고도 함)은 단어가 문서 모음의 각 문서에서 나타나는 횟수를 기록합니다.

bagOfWords는 텍스트를 단어로 분할하지 않습니다. 토큰화된 문서로 구성된 배열을 만들려면tokenizedDocument항목을 참조하십시오.

생성

설명

bag= bagOfWords는 빈 bag-of-words 모델을 만듭니다.

예제

bag= bagOfWords(documents)documents에 나타나는 단어 개수를 계산하고 bag-of-words 모델을 반환합니다.

예제

bag= bagOfWords(uniqueWords,计数)uniqueWords의 단어와计数의 해당 빈도 수를 사용하여 bag-of-words 모델을 만듭니다.

입력 인수

모두 확장

입력 문서로,tokenizedDocument배열, 단어로 구성된 string형 배열 또는 문자형 벡터로 구성된 셀형 배열로 지정됩니다.documentstokenizedDocument배열이 아닌 경우 이는 단일 문서를 나타내고 각 요소가 단어인 행 벡터여야 합니다. 문서를 여러 개 지정하려면tokenizedDocument배열을 사용하십시오.

고유한 단어 목록으로, string형 벡터 또는 문자형 벡터로 구성된 셀형 배열로 지정됩니다.uniqueWords이 있는 경우 함수는 누락값을 무시합니다.uniqueWords의 크기는 1xV여야 하며, 여기서 V는计数의 열 개수입니다.

예:["an" "example" "list"]

데이터형:string|cell

uniqueWords에 해당하는 단어의 빈도 수로, 음이 아닌 정수로 구성된 행렬로 지정됩니다. 값计数(i,j)는 i번째 문서에서 단어uniqueWords(j)가나오는 횟수에 해당합니다.

计数에는numel(uniqueWords)개의 열이 있어야 합니다.

속성

모두 확장

문서당 단어 개수로, 희소 행렬로 지정됩니다.

문서가 나오는 횟수로, 음이 아닌 정수로 지정됩니다.

모델 내 고유한 단어 개수로, 음이 아닌 정수로 지정됩니다.

모델 내 고유한 단어로, string형 벡터로 지정됩니다.

데이터형:string

객체 함수

encode 문서를 단어 개수 또는 n-gram 개수로 구성된 행렬로 인코딩
tfidf TF-IDF(단어 빈도-역 문서 빈도) 행렬
topkwords Most important words in bag-of-words model or LDA topic
addDocument bag-of-words 모델 또는 bag-of-n-grams 모델에 문서 추가
removeDocument bag-of-words 모델 또는 bag-of-n-grams 모델에서 문서 제거
removeEmptyDocuments 토큰화된 문서 배열, bag-of-words 모델 또는 bag-of-n-grams 모델에서 빈 문서 제거
removeWords 문서 또는 bag-of-words 모델에서 선택한 단어 제거
removeInfrequentWords bag-of-words 모델에서 개수가 적은 단어 제거
join Combine multiple bag-of-words or bag-of-n-grams models
wordcloud Create word cloud chart from text, bag-of-words model, bag-of-n-grams model, or LDA model

예제

모두 축소

예제 데이터를 불러옵니다. 파일sonnetsPreprocessed.txt에는 셰익스피어 소네트의 전처리된 버전이 들어 있습니다. 파일에는 한 줄에 하나씩 소네트가 들어 있으며 단어가 공백으로 구분되어 있습니다.sonnetsPreprocessed.txt에서 텍스트를 추출하고, 추출한 텍스트를 새 줄 문자에서 문서로 분할한 후 그 문서를 토큰화합니다.

文件名="sonnetsPreprocessed.txt"; str = extractFileText(filename); textData = split(str,newline); documents = tokenizedDocument(textData);

bagOfWords를 사용하여 bag-of-words 모델을 만듭니다.

bag = bagOfWords(documents)
bag = bagOfWords with properties: Counts: [154x3092 double] Vocabulary: ["fairest" "creatures" "desire" ... ] NumWords: 3092 NumDocuments: 154

상위 10개의 단어와 총 개수를 표시합니다.

tbl = topkwords(bag,10)
tbl=10×2 tableWord Count _______ _____ "thy" 281 "thou" 234 "love" 162 "thee" 161 "doth" 88 "mine" 63 "shall" 59 "eyes" 56 "sweet" 55 "time" 53

고유한 단어로 구성된 string형 배열과 단어 개수로 구성된 행렬을 사용하여 bag-of-words 모델을 만듭니다.

uniqueWords = ["a""an""another""example""final""sentence""third"]; counts = [...1 2 0 1 0 1 0; 0 0 3 1 0 4 0; 1 0 0 5 0 3 1; 1 0 0 1 7 0 0]; bag = bagOfWords(uniqueWords,counts)
bag = bagOfWords with properties: Counts: [4x7 double] Vocabulary: ["a" "an" "another" "example" ... ] NumWords: 7 NumDocuments: 4

텍스트 데이터가 한 폴더 내 여러 파일에 포함되어 있는 경우 파일 데이터저장소를 사용하여 텍스트 데이터를 MATLAB으로 가져올 수 있습니다.

예제 소네트 텍스트 파일을 위한 파일 데이터저장소를 만듭니다. 예제 소네트의 파일 이름은 "exampleSonnetN.txt"입니다. 여기서N은 소네트 번호입니다.extractFileText를 읽기 함수로 지정합니다.

readFcn = @extractFileText; fds = fileDatastore('exampleSonnet*.txt','ReadFcn',readFcn)
fds = FileDatastore with properties: Files: { ' .../tpac88f007/textanalytics-ex73762432/exampleSonnet1.txt'; ' .../tpac88f007/textanalytics-ex73762432/exampleSonnet2.txt'; ' .../tpac88f007/textanalytics-ex73762432/exampleSonnet3.txt' ... and 1 more } Folders: { '/tmp/Bdoc21b_1723106_97109/tpac88f007/textanalytics-ex73762432' } UniformRead: 0 ReadMode: 'file' BlockSize: Inf PreviewFcn: @extractFileText SupportedOutputFormats: ["txt" "csv" "xlsx" "xls" ... ] ReadFcn: @extractFileText AlternateFileSystemRoots: {}

빈 bag-of-words 모델을 만듭니다.

bag = bagOfWords
bag = bagOfWords with properties: Counts: [] Vocabulary: [1x0 string] NumWords: 0 NumDocuments: 0

루프를 사용해 데이터저장소에 있는 파일을 순회하여 각 파일을 읽어옵니다. 각 파일의 텍스트를 토큰화하고 문서를bag에 추가합니다.

whilehasdata(fds) str = read(fds); document = tokenizedDocument(str); bag = addDocument(bag,document);end

업데이트된bag-of-words 모델을 표시합니다.

bag
bag = bagOfWords with properties: Counts: [4x276 double] Vocabulary: ["From" "fairest" "creatures" "we" ... ] NumWords: 276 NumDocuments: 4

removeWords에 불용어 목록을 입력하여 bag-of-words 모델에서 불용어를 제거합니다. 불용어는 "a", "the", "in"과 같이 일반적으로 분석 전에 텍스트에서 제거되는 단어입니다.

documents = tokenizedDocument(["an example of a short sentence""a second short sentence"]); bag = bagOfWords(documents); newBag = removeWords(bag,stopWords)
newBag = bagOfWords with properties: Counts: [2x4 double] Vocabulary: ["example" "short" "sentence" "second"] NumWords: 4 NumDocuments: 2

bag-of-words 모델에서 빈도가 가장 높은 단어의 테이블을 만듭니다.

예제 데이터를 불러옵니다. 파일sonnetsPreprocessed.txt에는 셰익스피어 소네트의 전처리된 버전이 들어 있습니다. 파일에는 한 줄에 하나씩 소네트가 들어 있으며 단어가 공백으로 구분되어 있습니다.sonnetsPreprocessed.txt에서 텍스트를 추출하고, 추출한 텍스트를 새 줄 문자에서 문서로 분할한 후 그 문서를 토큰화합니다.

文件名="sonnetsPreprocessed.txt"; str = extractFileText(filename); textData = split(str,newline); documents = tokenizedDocument(textData);

bagOfWords를 사용하여 bag-of-words 모델을 만듭니다.

bag = bagOfWords(documents)
bag = bagOfWords with properties: Counts: [154x3092 double] Vocabulary: ["fairest" "creatures" "desire" ... ] NumWords: 3092 NumDocuments: 154

상위 5개 단어를 찾습니다.

T = topkwords(bag);

모델에서 상위 20개 단어를 찾습니다.

k = 20; T = topkwords(bag,k)
T=20×2 tableWord Count ________ _____ "thy" 281 "thou" 234 "love" 162 "thee" 161 "doth" 88 "mine" 63 "shall" 59 "eyes" 56 "sweet" 55 "time" 53 "beauty" 52 "nor" 52 "art" 51 "yet" 51 "o" 50 "heart" 50 ⋮

bag-of-words모델에서TF -IDF(단어 빈도-역 문서 빈도) 행렬을 만듭니다.

예제 데이터를 불러옵니다. 파일sonnetsPreprocessed.txt에는 셰익스피어 소네트의 전처리된 버전이 들어 있습니다. 파일에는 한 줄에 하나씩 소네트가 들어 있으며 단어가 공백으로 구분되어 있습니다.sonnetsPreprocessed.txt에서 텍스트를 추출하고, 추출한 텍스트를 새 줄 문자에서 문서로 분할한 후 그 문서를 토큰화합니다.

文件名="sonnetsPreprocessed.txt"; str = extractFileText(filename); textData = split(str,newline); documents = tokenizedDocument(textData);

bagOfWords를 사용하여 bag-of-words 모델을 만듭니다.

bag = bagOfWords(documents)
bag = bagOfWords with properties: Counts: [154x3092 double] Vocabulary: ["fairest" "creatures" "desire" ... ] NumWords: 3092 NumDocuments: 154

tf-idf 행렬을 만듭니다. 처음 10개의 행과 열을 표시합니다.

M = tfidf(bag); full(M(1:10,1:10))
ans =10×103.6507 4.3438 2.7344 3.6507 4.3438 2.2644 3.2452 3.8918 2.4720 2.5520 0 0 0 0 0 4.5287 0 0 0 0 0 0 0 0 0 0 0 0 0 2.5520 0 0 0 0 0 2.2644 0 0 0 0 0 0 0 0 0 2.2644 0 0 0 0 0 0 0 0 0 2.2644 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2.2644 0 0 0 2.5520 0 0 2.7344 0 0 0 0 0 0 0

예제 데이터를 불러옵니다. 파일sonnetsPreprocessed.txt에는 셰익스피어 소네트의 전처리된 버전이 들어 있습니다. 파일에는 한 줄에 하나씩 소네트가 들어 있으며 단어가 공백으로 구분되어 있습니다.sonnetsPreprocessed.txt에서 텍스트를 추출하고, 추출한 텍스트를 새 줄 문자에서 문서로 분할한 후 그 문서를 토큰화합니다.

文件名="sonnetsPreprocessed.txt"; str = extractFileText(filename); textData = split(str,newline); documents = tokenizedDocument(textData);

bagOfWords를 사용하여 bag-of-words 모델을 만듭니다.

bag = bagOfWords(documents)
bag = bagOfWords with properties: Counts: [154x3092 double] Vocabulary: ["fairest" "creatures" "desire" ... ] NumWords: 3092 NumDocuments: 154

워드 클라우드를 사용하여 bag-of-words 모델을 시각화합니다.

figure wordcloud(bag);

Figure contains an object of type wordcloud.

텍스트 데이터가 한 폴더 내 여러 파일에 포함되어 있는 경우parfor를 사용하여 병렬로 텍스트 데이터를 가져오고 bag-of-words 모델을 만들 수 있습니다. Parallel Computing Toolbox™가 설치된 경우에는parfor루프가 병렬로 실행됩니다. 그렇지 않으면 직렬로 실행됩니다. bag-of-words 모델로 구성된 배열을 하나의 모델로 결합하려면join을 사용하십시오.

파일 모음에서 bag-of-words 모델을 만듭니다. 예제 소네트의 파일 이름은 "exampleSonnetN.txt"입니다. 여기서N은 소네트 번호입니다.dir을 사용하여 파일 목록 및 위치를 가져옵니다.

fileLocation = fullfile(matlabroot,'examples','textanalytics','exampleSonnet*.txt'); fileInfo = dir(fileLocation)
fileInfo = 0x1 empty struct array with fields: name folder date bytes isdir datenum

빈 bag-of-words 모델을 초기화한 다음, 루프를 사용해 파일을 순회하여 bag-of-words 모델로 구성된 배열을 만듭니다.

bag = bagOfWords; numFiles = numel(fileInfo);parfori = 1:numFiles f = fileInfo(i); filename = fullfile(f.folder,f.name); textData = extractFileText(filename); document = tokenizedDocument(textData); bag(i) = bagOfWords(document);end

join을 사용하여 bag-of-words 모델을 결합합니다.

bag = join(bag)
bag = bagOfWords with properties: Counts: [] Vocabulary: [1x0 string] NumWords: 0 NumDocuments: 0

  • 작업에 홀드아웃 테스트 세트를 사용하려면bagOfWords를 사용하기 전에 텍스트 데이터를 분할하십시오. 그렇게 하지 않으면 bag-of-words 모델에서 분석이 편향될 수 있습니다.

버전 내역

R2017b에 개발됨