Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

html_parser.py 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. from rag.nlp import find_codec
  18. import readability
  19. import html_text
  20. import chardet
  21. def get_encoding(file):
  22. with open(file,'rb') as f:
  23. tmp = chardet.detect(f.read())
  24. return tmp['encoding']
  25. class RAGFlowHtmlParser:
  26. def __call__(self, fnm, binary=None):
  27. txt = ""
  28. if binary:
  29. encoding = find_codec(binary)
  30. txt = binary.decode(encoding, errors="ignore")
  31. else:
  32. with open(fnm, "r",encoding=get_encoding(fnm)) as f:
  33. txt = f.read()
  34. return self.parser_txt(txt)
  35. @classmethod
  36. def parser_txt(cls, txt):
  37. if not isinstance(txt, str):
  38. raise TypeError("txt type should be string!")
  39. html_doc = readability.Document(txt)
  40. title = html_doc.title()
  41. content = html_text.extract_text(html_doc.summary(html_partial=True))
  42. txt = f"{title}\n{content}"
  43. sections = txt.split("\n")
  44. return sections