[docs]def__init__(self,path:Union[str,Path],encoding:Optional[str]=None,errors:Optional[str]=None,custom_html_tag:Optional[Tuple[str,dict]]=None,patterns:Sequence[str]=("*.htm","*.html"),exclude_links_ratio:float=1.0,**kwargs:Optional[Any],):""" Initialize ReadTheDocsLoader The loader loops over all files under `path` and extracts the actual content of the files by retrieving main html tags. Default main html tags include `<main id="main-content>`, <`div role="main>`, and `<article role="main">`. You can also define your own html tags by passing custom_html_tag, e.g. `("div", "class=main")`. The loader iterates html tags with the order of custom html tags (if exists) and default html tags. If any of the tags is not empty, the loop will break and retrieve the content out of that tag. Args: path: The location of pulled readthedocs folder. encoding: The encoding with which to open the documents. errors: Specify how encoding and decoding errors are to be handledโthis cannot be used in binary mode. custom_html_tag: Optional custom html tag to retrieve the content from files. patterns: The file patterns to load, passed to `glob.rglob`. exclude_links_ratio: The ratio of links:content to exclude pages from. This is to reduce the frequency at which index pages make their way into retrieved results. Recommended: 0.5 kwargs: named arguments passed to `bs4.BeautifulSoup`. """try:frombs4importBeautifulSoupexceptImportError:raiseImportError("Could not import python packages. ""Please install it with `pip install beautifulsoup4`. ")try:_=BeautifulSoup("<html><body>Parser builder library test.</body></html>","html.parser",**kwargs,)exceptExceptionase:raiseValueError("Parsing kwargs do not appear valid")fromeself.file_path=Path(path)self.encoding=encodingself.errors=errorsself.custom_html_tag=custom_html_tagself.patterns=patternsself.bs_kwargs=kwargsself.exclude_links_ratio=exclude_links_ratio
[docs]deflazy_load(self)->Iterator[Document]:"""A lazy loader for Documents."""forfile_patterninself.patterns:forpinself.file_path.rglob(file_pattern):ifp.is_dir():continuewithopen(p,encoding=self.encoding,errors=self.errors)asf:text=self._clean_data(f.read())yieldDocument(page_content=text,metadata={"source":str(p)})
def_clean_data(self,data:str)->str:frombs4importBeautifulSoupsoup=BeautifulSoup(data,"html.parser",**self.bs_kwargs)# default tagshtml_tags=[("div",{"role":"main"}),("main",{"id":"main-content"}),]ifself.custom_html_tagisnotNone:html_tags.append(self.custom_html_tag)element=None# reversed order. check the custom one firstfortag,attrsinhtml_tags[::-1]:element=soup.find(tag,attrs)# if found, breakifelementisnotNone:breakifelementisnotNoneand_get_link_ratio(element)<=self.exclude_links_ratio:text=_get_clean_text(element)else:text=""# trim empty linesreturn"\n".join([tfortintext.split("\n")ift])
def_get_clean_text(element:Tag)->str:"""Returns cleaned text with newlines preserved and irrelevant elements removed."""elements_to_skip=["script","noscript","canvas","meta","svg","map","area","audio","source","track","video","embed","object","param","picture","iframe","frame","frameset","noframes","applet","form","button","select","base","style","img",]newline_elements=["p","div","ul","ol","li","h1","h2","h3","h4","h5","h6","pre","table","tr",]text=_process_element(element,elements_to_skip,newline_elements)returntext.strip()def_get_link_ratio(section:Tag)->float:links=section.find_all("a")total_text="".join(str(s)forsinsection.stripped_strings)iflen(total_text)==0:return0link_text="".join(str(string.string.strip())forlinkinlinksforstringinlink.stringsifstring)returnlen(link_text)/len(total_text)def_process_element(element:Union[Tag,NavigableString,Comment],elements_to_skip:List[str],newline_elements:List[str],)->str:""" Traverse through HTML tree recursively to preserve newline and skip unwanted (code/binary) elements """frombs4importNavigableStringfrombs4.elementimportComment,Tagtag_name=getattr(element,"name",None)ifisinstance(element,Comment)ortag_nameinelements_to_skip:return""elifisinstance(element,NavigableString):returnelementeliftag_name=="br":return"\n"eliftag_nameinnewline_elements:return("".join(_process_element(child,elements_to_skip,newline_elements)forchildinelement.childrenifisinstance(child,(Tag,NavigableString,Comment)))+"\n")else:return"".join(_process_element(child,elements_to_skip,newline_elements)forchildinelement.childrenifisinstance(child,(Tag,NavigableString,Comment)))