MCPcopy Create free account
hub / github.com/cloudwego/eino-examples / main

Function main

components/document/parser/extparser/ext_parser.go:31–86  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

29)
30
31func main() {
32 ctx := context.Background()
33
34 textParser := parser.TextParser{}
35
36 htmlParser, err := html.NewParser(ctx, &html.Config{
37 Selector: gptr.Of("body"),
38 })
39 if err != nil {
40 logs.Errorf("html.NewParser failed, err=%v", err)
41 return
42 }
43
44 pdfParser, err := pdf.NewPDFParser(ctx, &pdf.Config{})
45 if err != nil {
46 logs.Errorf("pdf.NewPDFParser failed, err=%v", err)
47 return
48 }
49
50 // 创建扩展解析器
51 extParser, err := parser.NewExtParser(ctx, &parser.ExtParserConfig{
52 // 注册特定扩展名的解析器
53 Parsers: map[string]parser.Parser{
54 ".html": htmlParser,
55 ".pdf": pdfParser,
56 },
57 // 设置默认解析器,用于处理未知格式
58 FallbackParser: textParser,
59 })
60 if err != nil {
61 return
62 }
63
64 // 使用解析器
65 filePath := "./testdata/test.html"
66 file, err := os.Open(filePath)
67 if err != nil {
68 logs.Errorf("os.Open failed, file=%v, err=%v", filePath, err)
69 return
70 }
71 docs, err := extParser.Parse(ctx, file,
72 // 必须提供 URI ExtParser 选择正确的解析器进行解析
73 parser.WithURI(filePath),
74 parser.WithExtraMeta(map[string]any{
75 "source": "local",
76 }),
77 )
78 if err != nil {
79 logs.Errorf("extParser.Parse, err=%v", err)
80 return
81 }
82
83 for idx, doc := range docs {
84 logs.Infof("doc_%v content: %v", idx, doc.Content)
85 }
86}

Callers

nothing calls this directly

Calls 4

OfFunction · 0.92
ErrorfFunction · 0.92
InfofFunction · 0.92
ParseMethod · 0.80

Tested by

no test coverage detected