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.
"
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);
빈 bag-of-words 모델을 만듭니다.
bag = bagOfWords
bag =
bagOfWords with properties:
NumWords: 0
Counts: []
Vocabulary: [1×0 string]
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:
NumWords: 276
Counts: [4×276 double]
Vocabulary: ["From" "fairest" "creatures" "we" "desire" "increase" "," "That" "thereby" "beauty's" "rose" "might" "never" "die" "But" "as" "the" "riper" "should" "by" … ] (1×276 string)
NumDocuments: 4
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"
입력 인수
파일 이름으로, string형 스칼라, 문자형 벡터 또는 문자형 벡터를 포함하는 1×1 셀형 배열로 지정됩니다.
데이터형: string | char | cell
이름-값 인수
선택적 인수 쌍을 Name1=Value1,...,NameN=ValueN으로 지정합니다. 여기서 Name은 인수 이름이고 Value는 대응값입니다. 이름-값 인수는 다른 인수 뒤에 와야 하지만, 인수 쌍의 순서는 상관없습니다.
R2021a 이전 릴리스에서는 각 이름과 값을 쉼표로 구분하고 Name을 따옴표로 묶으십시오.
예: 'Pages',[1 3 5]는 PDF 파일의 1, 3, 5페이지를 읽어오도록 지정합니다.
사용할 문자 인코딩으로, 'Encoding'과 함께 문자형 벡터 또는 string형 스칼라가 쉼표로 구분되어 지정됩니다. 문자형 벡터 또는 string형 스칼라에는 다음과 같은 표준 문자 인코딩 체계 이름이 포함되어야 합니다.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
|
인코딩 체계를 지정하지 않은 경우 함수는 사용할 인코딩을 발견적 방식으로 자동 검출합니다. 발견법은 로캘에 따라 달라집니다. 이러한 발견법이 실패하면 명시적으로 하나를 지정해야 합니다.
이 옵션은 입력이 일반 텍스트 파일일 때만 적용됩니다.
데이터형: char | string
추출 방법으로, 'ExtractionMethod'와 함께 다음 중 하나가 쉼표로 구분되어 지정됩니다.
| 옵션 | 설명 |
|---|---|
'tree' | DOM 트리와 텍스트 내용을 분석한 후 단락 블록을 추출합니다. |
'article' | 기사 텍스트를 검출하고 단락 블록을 추출합니다. |
'all-text' | 스크립트와 CSS 스타일을 제외하고, HTML 본문의 모든 텍스트를 추출합니다. |
이 옵션은 HTML 파일 입력만 지원합니다.
PDF 파일을 열기 위한 비밀번호로, 'Password'와 함께 문자형 벡터 또는 string형 스칼라가 쉼표로 구분되어 지정됩니다. 이 옵션은 입력 파일이 PDF일 경우에만 적용됩니다.
예: 'Password','skroWhtaM'
데이터형: char | string
PDF 파일에서 읽을 페이지로, 'Pages'와 함께 양의 정수로 구성된 벡터가 쉼표로 구분되어 지정됩니다. 이 옵션은 입력 파일이 PDF 파일일 경우에만 적용됩니다. 기본적으로 이 함수는 PDF 파일의 모든 페이지를 읽어옵니다.
예: 'Pages',[1 3 5]
데이터형: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64
팁
HTML 코드에서 직접 텍스트를 읽어오려면
extractHTMLText를 사용하십시오.텍스트 파일에서 줄로 구분된 텍스트를 읽어오려면
readlines를 사용합니다.
버전 내역
R2017b에 개발됨extractFileText 함수를 사용하여 Microsoft® Word 97–2003 이진 DOC 파일로부터 텍스트를 추출하는 기능에 대한 지원은 제거되었습니다. Microsoft Word DOCX 파일은 계속 지원됩니다.
Microsoft Word 97–2003 이진 DOC 파일에서 텍스트 데이터를 추출하려면 먼저 파일을 PDF, Microsoft Word DOCX, HTML 또는 일반 텍스트 파일로 저장한 다음, extractFileText 함수를 사용하십시오.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- 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)