extractFileText
PDF, Microsoft Word, HTML 및 일반 텍스트 파일에서 텍스트 읽어오기
설명
는 하나 이상의 이름-값 쌍의 인수를 사용하여 옵션을 추가로 지정합니다.str
= extractFileText(filename
,Name,Value
)
예제
텍스트 파일에서 텍스트 데이터 추출하기
extractFileText
를 사용하여 sonnets.txt
에서 텍스트를 추출합니다. sonnets.txt
파일에는 셰익스피어의 소네트가 일반 텍스트로 포함되어 있습니다.
str = extractFileText("sonnets.txt");
첫 번째 소네트를 표시합니다.
i = strfind(str,"I"); ii = strfind(str,"II"); start = i(1); fin = ii(1); extractBetween(str,start,fin-1)
ans = "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. "
PDF에서 텍스트 데이터 추출하기
extractFileText
를 사용하여 exampleSonnets.pdf
에서 텍스트를 추출합니다. exampleSonnets.pdf
파일에는 셰익스피어의 소네트가 PDF 파일로 포함되어 있습니다.
str = extractFileText("exampleSonnets.pdf");
두 번째 소네트를 표시합니다.
ii = strfind(str,"II"); iii = strfind(str,"III"); start = ii(1); fin = iii(1); extractBetween(str,start,fin-1)
ans = "II When forty winters shall besiege thy brow, And dig deep trenches in thy beauty's field, Thy youth's proud livery so gazed on now, Will be a tatter'd weed of small worth held: Then being asked, where all thy beauty lies, Where all the treasure of thy lusty days; To say, within thine own deep sunken eyes, Were an all-eating shame, and thriftless praise. How much more praise deserv'd thy beauty's use, If thou couldst answer 'This fair child of mine Shall sum my count, and make my old excuse,' Proving his beauty by succession thine! This were to be new made when thou art old, And see thy blood warm when thou feel'st it cold. "
PDF 파일의 3, 5, 7페이지에서 텍스트를 추출합니다.
pages = [3 5 7]; str = extractFileText("exampleSonnets.pdf", ... 'Pages',pages);
10번째 소네트를 표시합니다.
x = strfind(str,"X"); xi = strfind(str,"XI"); start = x(1); fin = xi(1); extractBetween(str,start,fin-1)
ans = "X Is it for fear to wet a widow's eye, That thou consum'st thy self in single life? Ah! if thou issueless shalt hap to die, The world will wail thee like a makeless wife; The world will be thy widow and still weep That thou no form of thee hast left behind, When every private widow well may keep By children's eyes, her husband's shape in mind: Look! what an unthrift in the world doth spend Shifts but his place, for still the world enjoys it; But beauty's waste hath in the world an end, And kept unused the user so destroys it. No love toward others in that bosom sits That on himself such murd'rous shame commits. X For shame! deny that thou bear'st love to any, Who for thy self art so unprovident. Grant, if thou wilt, thou art belov'd of many, But that thou none lov'st is most evident: For thou art so possess'd with murderous hate, That 'gainst thy self thou stick'st not to conspire, Seeking that beauteous roof to ruinate Which to repair should be thy chief desire. "
파일 데이터저장소를 사용하여 여러 파일에서 텍스트 가져오기
텍스트 데이터가 한 폴더 내 여러 파일에 포함되어 있는 경우 파일 데이터저장소를 사용하여 텍스트 데이터를 MATLAB으로 가져올 수 있습니다.
예제 소네트 텍스트 파일을 위한 파일 데이터저장소를 만듭니다. 예제 소네트의 파일 이름은 "exampleSonnetN.txt
"입니다. 여기서 N
은 소네트 번호입니다. extractFileText
를 읽기 함수로 지정합니다.
readFcn = @extractFileText; fds = fileDatastore('exampleSonnet*.txt','ReadFcn',readFcn)
fds = FileDatastore with properties: Files: { ' .../tp863ef885/textanalytics-ex73762432/exampleSonnet1.txt'; ' .../tp863ef885/textanalytics-ex73762432/exampleSonnet2.txt'; ' .../tp863ef885/textanalytics-ex73762432/exampleSonnet3.txt' ... and 1 more } Folders: { '/tmp/Bdoc22b_2115095_2029149/tp863ef885/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
에 추가합니다.
while hasdata(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
HTML에서 텍스트 추출하기
HTML 코드에서 텍스트 데이터를 바로 추출하려면 extractHTMLText
를 사용하고 HTML 코드를 string형으로 지정하십시오.
code = "<html><body><h1>THE SONNETS</h1><p>by William Shakespeare</p></body></html>";
str = extractHTMLText(code)
str = "THE SONNETS by William Shakespeare"
입력 인수
filename
— 파일 이름
string형 스칼라 | 문자형 벡터
파일 이름으로, string형 스칼라 또는 문자형 벡터로 지정됩니다.
데이터형: string
| char
이름-값 인수
선택적 인수 쌍을 Name1=Value1,...,NameN=ValueN
으로 지정합니다. 여기서 Name
은 인수 이름이고 Value
는 대응값입니다. 이름-값 인수는 다른 인수 뒤에 와야 하지만, 인수 쌍의 순서는 상관없습니다.
R2021a 이전 릴리스에서는 각 이름과 값을 쉼표로 구분하고 Name
을 따옴표로 묶으십시오.
예: 'Pages',[1 3 5]
는 PDF 파일의 1, 3, 5페이지를 읽어오도록 지정합니다.
Encoding
— 문자 인코딩
'auto'
(디폴트 값) | 'UTF-8'
| 'ISO-8859-1'
| 'windows-1251'
| 'windows-1252'
| ...
사용할 문자 인코딩으로, 'Encoding'
과 함께 문자형 벡터 또는 string형 스칼라가 쉼표로 구분되어 지정됩니다. 문자형 벡터 또는 string형 스칼라에는 다음과 같은 표준 문자 인코딩 체계 이름이 포함되어야 합니다.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
|
인코딩 체계를 지정하지 않은 경우 함수는 사용할 인코딩을 발견적 방식으로 자동 감지합니다. 발견법은 로캘에 따라 달라집니다. 이러한 발견법이 실패하면 명시적으로 하나를 지정해야 합니다.
이 옵션은 입력이 일반 텍스트 파일일 때만 적용됩니다.
데이터형: char
| string
ExtractionMethod
— 추출 방법
'tree'
(디폴트 값) | 'article'
| 'all-text'
추출 방법으로, 'ExtractionMethod'
와 함께 다음 중 하나가 쉼표로 구분되어 지정됩니다.
옵션 | 설명 |
---|---|
'tree' | DOM 트리와 텍스트 내용을 분석한 후 단락 블록을 추출합니다. |
'article' | 기사 텍스트를 검출하고 단락 블록을 추출합니다. |
'all-text' | 스크립트와 CSS 스타일을 제외하고, HTML 본문의 모든 텍스트를 추출합니다. |
이 옵션은 HTML 파일 입력만 지원합니다.
Password
— PDF 파일을 열기 위한 비밀번호
문자형 벡터 | string형 스칼라
PDF 파일을 열기 위한 비밀번호로, 'Password'
와 함께 문자형 벡터 또는 string형 스칼라가 쉼표로 구분되어 지정됩니다. 이 옵션은 입력 파일이 PDF일 경우에만 적용됩니다.
예: 'Password','skroWhtaM'
데이터형: char
| string
Pages
— PDF 파일에서 읽을 페이지
양의 정수로 구성된 벡터
PDF 파일에서 읽을 페이지로, 'Pages'
와 함께 양의 정수로 구성된 벡터가 쉼표로 구분되어 지정됩니다. 이 옵션은 입력 파일이 PDF 파일일 경우에만 적용됩니다. 기본적으로 이 함수는 PDF 파일의 모든 페이지를 읽어옵니다.
예: 'Pages',[1 3 5]
데이터형: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
팁
HTML 코드에서 직접 텍스트를 읽어오려면
extractHTMLText
를 사용하십시오.
버전 내역
R2017b에 개발됨R2020b: extractFileText
는 Microsoft Word 97–2003 이진 DOC 파일로부터의 텍스트 추출을 더 이상 지원하지 않습니다.
extractFileText
함수를 사용하여 Microsoft® Word 97–2003 이진 DOC 파일로부터 텍스트를 추출하는 기능에 대한 지원은 제거되었습니다. Microsoft Word DOCX 파일은 계속 지원됩니다.
Microsoft Word 97–2003 이진 DOC 파일에서 텍스트 데이터를 추출하려면 먼저 파일을 PDF, Microsoft Word DOCX, HTML 또는 일반 텍스트 파일로 저장한 다음, extractFileText
함수를 사용하십시오.
MATLAB 명령
다음 MATLAB 명령에 해당하는 링크를 클릭했습니다.
명령을 실행하려면 MATLAB 명령 창에 입력하십시오. 웹 브라우저는 MATLAB 명령을 지원하지 않습니다.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)