fix(parser): prefer anydoc for every type it converts when linked
ppt/pptx still fall back to markitdown when anydoc is not in the binary. pdf/docx/xlsx and other anydoc formats now default to the in-process converter as well; csv/md stay on the Go simple reader.main
parent
6020799792
commit
35275d5613
|
|
@ -214,8 +214,9 @@ function pickDefaultEngineName(
|
||||||
extensions: string[],
|
extensions: string[],
|
||||||
): string {
|
): string {
|
||||||
const available = engines.filter(e => e.available)
|
const available = engines.filter(e => e.available)
|
||||||
const prefersAnydoc = extensions.some(ext => ext === 'ppt' || ext === 'pptx')
|
const simpleExts = new Set(['md', 'markdown', 'txt', 'csv', 'json'])
|
||||||
if (prefersAnydoc) {
|
const allSimple = extensions.length > 0 && extensions.every(ext => simpleExts.has(ext))
|
||||||
|
if (!allSimple) {
|
||||||
const anydoc = available.find(e => e.name === 'anydoc')
|
const anydoc = available.find(e => e.name === 'anydoc')
|
||||||
if (anydoc) return anydoc.name
|
if (anydoc) return anydoc.name
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,20 +33,25 @@ func TestListAllEnginesBuiltinIncludesDocumentFormats(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDefaultParserEnginePrefersAnydocWhenLinked(t *testing.T) {
|
func TestDefaultParserEnginePrefersAnydocWhenLinked(t *testing.T) {
|
||||||
if types.DefaultParserEngine("pdf") != "" {
|
cases := []string{"pptx", "ppt", "pdf", "docx"}
|
||||||
t.Fatalf("DefaultParserEngine(pdf) should stay empty so builtin routing is unchanged")
|
|
||||||
}
|
|
||||||
got := types.DefaultParserEngine("pptx")
|
|
||||||
if anydoc.Available() {
|
if anydoc.Available() {
|
||||||
if got != AnydocEngineName {
|
for _, ft := range cases {
|
||||||
t.Fatalf("DefaultParserEngine(pptx) = %q, want anydoc when the binding is linked", got)
|
if got := types.DefaultParserEngine(ft); got != AnydocEngineName {
|
||||||
|
t.Errorf("DefaultParserEngine(%s) = %q, want anydoc when the binding is linked", ft, got)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if types.DefaultParserEngine("ppt") != AnydocEngineName {
|
if got := types.DefaultParserEngine("csv"); got != "" {
|
||||||
t.Fatalf("DefaultParserEngine(ppt) = %q, want anydoc", types.DefaultParserEngine("ppt"))
|
t.Errorf("DefaultParserEngine(csv) = %q, want empty so the Go simple reader stays default", got)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if got != "markitdown" {
|
if got := types.DefaultParserEngine("pptx"); got != "markitdown" {
|
||||||
t.Fatalf("DefaultParserEngine(pptx) = %q, want markitdown when anydoc is unavailable", got)
|
t.Fatalf("DefaultParserEngine(pptx) = %q, want markitdown when anydoc is unavailable", got)
|
||||||
}
|
}
|
||||||
|
if got := types.DefaultParserEngine("pdf"); got != "" {
|
||||||
|
t.Fatalf("DefaultParserEngine(pdf) = %q, want empty when anydoc is unavailable", got)
|
||||||
|
}
|
||||||
|
if got := types.DefaultParserEngine("docx"); got != "" {
|
||||||
|
t.Fatalf("DefaultParserEngine(docx) = %q, want empty when anydoc is unavailable", got)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -43,9 +43,11 @@ func init() {
|
||||||
|
|
||||||
// preferAnydocWhenAvailable is the type-level default override: when the
|
// preferAnydocWhenAvailable is the type-level default override: when the
|
||||||
// anydoc binding is linked and converts this file type, use it instead of
|
// anydoc binding is linked and converts this file type, use it instead of
|
||||||
// the markitdown fallback. Types without a fallback (pdf, docx, …) never
|
// builtin or the markitdown fallback. Simple formats stay on the Go reader.
|
||||||
// consult this — DefaultParserEngine short-circuits first.
|
|
||||||
func preferAnydocWhenAvailable(fileType string) string {
|
func preferAnydocWhenAvailable(fileType string) string {
|
||||||
|
if IsSimpleFormat(fileType) {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
if anydoc.Available() && anydoc.Supports(fileType, "") {
|
if anydoc.Available() && anydoc.Supports(fileType, "") {
|
||||||
return AnydocEngineName
|
return AnydocEngineName
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -250,8 +250,9 @@ type ChunkingConfig struct {
|
||||||
Separators []string `yaml:"separators" json:"separators"`
|
Separators []string `yaml:"separators" json:"separators"`
|
||||||
// ParserEngineRules configures which parser engine to use for each file type.
|
// ParserEngineRules configures which parser engine to use for each file type.
|
||||||
// When empty, DefaultParserEngine is used (builtin/simple routing, except
|
// When empty, DefaultParserEngine is used (builtin/simple routing, except
|
||||||
// types that only a specific engine can parse: ppt/pptx prefer anydoc
|
// types that only a specific engine can parse: ppt/pptx fall back to
|
||||||
// when that binding is linked, otherwise markitdown).
|
// markitdown). A linked anydoc binding is preferred for every type it
|
||||||
|
// converts.
|
||||||
ParserEngineRules []ParserEngineRule `yaml:"parser_engine_rules,omitempty" json:"parser_engine_rules,omitempty"`
|
ParserEngineRules []ParserEngineRule `yaml:"parser_engine_rules,omitempty" json:"parser_engine_rules,omitempty"`
|
||||||
// EnableParentChild enables two-level parent-child chunking strategy.
|
// EnableParentChild enables two-level parent-child chunking strategy.
|
||||||
// When enabled, large parent chunks provide context while small child chunks
|
// When enabled, large parent chunks provide context while small child chunks
|
||||||
|
|
@ -282,17 +283,16 @@ type ChunkingConfig struct {
|
||||||
|
|
||||||
// defaultParserEngineByType maps file types the builtin/simple engines cannot
|
// defaultParserEngineByType maps file types the builtin/simple engines cannot
|
||||||
// parse to a docreader engine that can. Types omitted here keep empty-string
|
// parse to a docreader engine that can. Types omitted here keep empty-string
|
||||||
// routing (Go simple formats, otherwise docreader builtin). Anydoc, when
|
// routing (Go simple formats, otherwise docreader builtin) unless
|
||||||
// linked into the binary, is preferred over these fallbacks — see
|
// SetPreferParserEngine selects a linked in-process engine such as anydoc.
|
||||||
// SetPreferParserEngine.
|
|
||||||
var defaultParserEngineByType = map[string]string{
|
var defaultParserEngineByType = map[string]string{
|
||||||
"ppt": "markitdown",
|
"ppt": "markitdown",
|
||||||
"pptx": "markitdown",
|
"pptx": "markitdown",
|
||||||
}
|
}
|
||||||
|
|
||||||
// preferParserEngine, if set, may override DefaultParserEngine for types that
|
// preferParserEngine, if set, may override DefaultParserEngine. The
|
||||||
// have a type-level fallback. The docparser package registers anydoc here so
|
// docparser package registers anydoc here so types does not import the
|
||||||
// types does not import the engine catalog.
|
// engine catalog.
|
||||||
var preferParserEngine func(fileType string) string
|
var preferParserEngine func(fileType string) string
|
||||||
|
|
||||||
// SetPreferParserEngine registers a build-time preference used by
|
// SetPreferParserEngine registers a build-time preference used by
|
||||||
|
|
@ -303,20 +303,17 @@ func SetPreferParserEngine(fn func(fileType string) string) {
|
||||||
|
|
||||||
// DefaultParserEngine returns the engine used when no parser_engine_rules
|
// DefaultParserEngine returns the engine used when no parser_engine_rules
|
||||||
// match. Empty string means builtin (docreader) or Go simple-format routing.
|
// match. Empty string means builtin (docreader) or Go simple-format routing.
|
||||||
// For ppt/pptx the preference is anydoc when that binding is available,
|
// When the anydoc binding is linked it is preferred for every type it
|
||||||
// otherwise markitdown.
|
// converts (except Go simple formats). ppt/pptx otherwise fall back to
|
||||||
|
// markitdown.
|
||||||
func DefaultParserEngine(fileType string) string {
|
func DefaultParserEngine(fileType string) string {
|
||||||
ft := normalizeParserFileType(fileType)
|
ft := normalizeParserFileType(fileType)
|
||||||
fallback := defaultParserEngineByType[ft]
|
|
||||||
if fallback == "" {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
if preferParserEngine != nil {
|
if preferParserEngine != nil {
|
||||||
if engine := preferParserEngine(ft); engine != "" {
|
if engine := preferParserEngine(ft); engine != "" {
|
||||||
return engine
|
return engine
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return fallback
|
return defaultParserEngineByType[ft]
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResolveParserEngine returns the engine name for the given file type
|
// ResolveParserEngine returns the engine name for the given file type
|
||||||
|
|
|
||||||
|
|
@ -330,10 +330,10 @@ func TestDefaultParserEngine(t *testing.T) {
|
||||||
func TestDefaultParserEnginePrefersRegisteredEngineForFallbackTypes(t *testing.T) {
|
func TestDefaultParserEnginePrefersRegisteredEngineForFallbackTypes(t *testing.T) {
|
||||||
t.Cleanup(func() { SetPreferParserEngine(nil) })
|
t.Cleanup(func() { SetPreferParserEngine(nil) })
|
||||||
SetPreferParserEngine(func(fileType string) string {
|
SetPreferParserEngine(func(fileType string) string {
|
||||||
if fileType == "pptx" || fileType == "ppt" {
|
if fileType == "pptx" || fileType == "ppt" || fileType == "pdf" || fileType == "docx" {
|
||||||
return "anydoc"
|
return "anydoc"
|
||||||
}
|
}
|
||||||
return "should-not-apply"
|
return ""
|
||||||
})
|
})
|
||||||
if got := DefaultParserEngine("pptx"); got != "anydoc" {
|
if got := DefaultParserEngine("pptx"); got != "anydoc" {
|
||||||
t.Fatalf("DefaultParserEngine(pptx) = %q, want anydoc", got)
|
t.Fatalf("DefaultParserEngine(pptx) = %q, want anydoc", got)
|
||||||
|
|
@ -341,8 +341,14 @@ func TestDefaultParserEnginePrefersRegisteredEngineForFallbackTypes(t *testing.T
|
||||||
if got := DefaultParserEngine("ppt"); got != "anydoc" {
|
if got := DefaultParserEngine("ppt"); got != "anydoc" {
|
||||||
t.Fatalf("DefaultParserEngine(ppt) = %q, want anydoc", got)
|
t.Fatalf("DefaultParserEngine(ppt) = %q, want anydoc", got)
|
||||||
}
|
}
|
||||||
if got := DefaultParserEngine("pdf"); got != "" {
|
if got := DefaultParserEngine("pdf"); got != "anydoc" {
|
||||||
t.Fatalf("DefaultParserEngine(pdf) = %q, want empty (no type-level fallback)", got)
|
t.Fatalf("DefaultParserEngine(pdf) = %q, want anydoc", got)
|
||||||
|
}
|
||||||
|
if got := DefaultParserEngine("docx"); got != "anydoc" {
|
||||||
|
t.Fatalf("DefaultParserEngine(docx) = %q, want anydoc", got)
|
||||||
|
}
|
||||||
|
if got := DefaultParserEngine("txt"); got != "" {
|
||||||
|
t.Fatalf("DefaultParserEngine(txt) = %q, want empty", got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue