extractFileText
PDF、Microsoft Word、HTML、およびプレーン テキスト ファイルからのテキストの読み取り
説明
は、1 つ以上の名前と値のペアの引数を使用して、追加のオプションを指定します。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");
2 番目のソネットを表示します。
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. "
テキスト データが 1 つのフォルダー内の複数のファイルに含まれている場合、ファイル データストアを使用してテキスト データを 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 コードからテキスト データを直接抽出するには、HTML コードを string として指定して extractHTMLText
を使用します。
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 列の cell 配列として指定します。
データ型: 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.
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)