🌴

【LangChain】HTMLHeaderTextSplitterのLimitations(制限)のメモ

2024/02/14に公開

HTMLデータの加工でHTMLHeaderTextSplitterを使っている際に、ドキュメントにLimitationsが書かれていたので、その読解メモです。

(この記事はChatGPTとの会話を編集して記載しています。)

Limitations

There can be quite a bit of structural variation from one HTML document to another, and while HTMLHeaderTextSplitter will attempt to attach all “relevant” headers to any given chunk, it can sometimes miss certain headers. For example, the algorithm assumes an informational hierarchy in which headers are always at nodes “above” associated text, i.e. prior siblings, ancestors, and combinations thereof. In the following news article (as of the writing of this document), the document is structured such that the text of the top-level headline, while tagged “h1”, is in a distinct subtree from the text elements that we’d expect it to be “above”—so we can observe that the “h1” element and its associated text do not show up in the chunk metadata (but, where applicable, we do see “h2” and its associated text):

https://python.langchain.com/docs/modules/data_connection/document_transformers/HTML_header_metadata#limitations

HTMLHeaderTextSplitterのLimitations(制限)とは

「HTMLHeaderTextSplitter」はHTMLドキュメントの構造に深く依存していて、ドキュメントのHTML構造が仮定と異なる場合、期待されるヘッダー情報の一部を見逃す可能性があります。

これが「HTMLHeaderTextSplitter」を使う上で注意すべき点です。

HTMLドキュメント間の構造的なバリエーション:

HTMLドキュメントは、その構造において大きなバリエーションがあります。HTMLHeaderTextSplitterは、与えられたチャンクに「関連する」すべてのヘッダーを添付しようとしますが、特定のヘッダーを見逃すことがあります。

これは、ヘッダーが常に関連するテキストの「上」にあるノード、つまり先行する兄弟、祖先、またはその組み合わせにあるという情報階層を仮定しているためです。

特定のHTML構造での課題:

例えば、トップレベルの見出しのテキストが「h1」タグでマークされているにもかかわらず、期待されるテキスト要素とは異なるサブツリーにあるようなドキュメント構造では、HTMLHeaderTextSplitterはその「h1」要素と関連するテキストをチャンクのメタデータに表示しないことがあります。

これは、特定のHTML構造がHTMLHeaderTextSplitterの仮定と異なる場合に、関連するヘッダーがチャンクに正確に反映されない可能性があることを意味します。

HTMLHeaderTextSplitterが適している構造と適していない構造

OK: 適しているHTML構造の例

  • 階層的なヘッダー構造
    ドキュメントがh1から始まり、サブセクションがh2, h3などでマークアップされている場合。このような構造では、HTMLHeaderTextSplitterは各セクションとサブセクションを正確に識別し、関連するテキストを適切にグループ化できます。
<div>
    <h1>Main Section</h1>
    <p>Introduction to the main section.</p>
    <h2>Subsection 1</h2>
    <p>Details about subsection 1.</p>
    <h2>Subsection 2</h2>
    <p>Details about subsection 2.</p>
</div>

NG: 適していないHTML構造の例

  • 非階層的または不規則なヘッダー使用
    ヘッダーが文書の論理的な構造を反映していない、またはヘッダーがランダムに使用されている場合。例えば、h1がドキュメントの中で複数回ランダムに使用される、またはサブセクションがh1でマークアップされ、メインセクションがh2でマークアップされるなどです。

  • ヘッダーとテキストが異なるサブツリーにある
    メインのヘッダーが一つのコンテナにあり、関連するテキストが別のコンテナに分離されている場合。このような構造では、HTMLHeaderTextSplitterはヘッダーとテキストの関連付けを正確に行うことができない可能性があります。

<div>
    <div>
        <h1>Main Section</h1>
    </div>
    <div>
        <p>Introduction to the main section that is logically under the Main Section header, but placed in a separate container.</p>
    </div>
</div>

まとめ

調べてみると「HTMLHeaderTextSplitter」が使える場面は思ったより少なそうという印象でした。(適しているような整ったHTML構造のサイトが少ないイメージ...)

その場合は通常のTextSplitter(改行や空白などで分割する)を使おうかなと思います!

Discussion