fix(docx): classify paragraphs using document languages
Fixes #3044.
partition_docx now passes the document languages to is_possible_narrative_text when it classifies a paragraph. Before, the call used the default languages=["eng"], so the English sentence-count and verb checks ran on every paragraph. A Danish sentence such as "Dette er et eksempel på en kort sætning." became Text instead of NarrativeText, even with languages=["dan"], because that argument only set metadata.languages.
Changes in unstructured/partition/docx.py:
partition_docxdeclareslanguagesexplicitly and passes it toDocxPartitionerOptions. Before, it acceptedlanguagesthrough**kwargsfor metadata only.DocxPartitionerOptions.languagesreturns the caller's languages. When the caller gives none, it detects them from the document text withdetect_languages, the same function that setsmetadata.languages. It falls back to["eng"]when detection returns nothing, for example for an empty document orlanguages=[""].- The paragraph classifier passes these languages to
is_possible_narrative_text.
partition_doc and partition_odt forward their kwargs to partition_docx, so they get the same behaviour.
Design choices:
- The fix is in the docx partitioner, not
text_type.py. The issue suggests thatis_possible_narrative_textskip the English verb check unlesslanguage_checksis set. All partitioners call it withlanguage_checks=False, so that would also turn off the verb check for English text everywhere. - Languages are detected when the caller gives none. This fixes the default
partition_docx(filename=...)andpartition(...)calls, and classification agrees withmetadata.languages. If you prefer explicitlanguagesonly,DocxPartitionerOptions.languagescan returnself._languages or ["eng"]instead. language_checksstays off. Turning it on would add an English-vocabulary requirement for English documents, which is a separate change.
The text, pptx, xlsx and html partitioners also call is_possible_narrative_text without languages. They are out of scope here.
Two new tests in test_unstructured/partition/test_docx.py partition a Danish paragraph, one with languages=["dan"] and one with no languages. Both fail on main and pass with this change.