llama_index [Bug]:MarkdownReader删除用尖括号括起来的超链接URL

4jb9z9bj  于 2个月前  发布在  其他
关注(0)|答案(1)|浏览(34)

Bug描述

MarkdownReader.markdown_to_tups 中的后处理过程通过将尖括号内的内容替换为空字符串来移除HTML。然而,据我所知,在markdown中用尖括号包围超链接的URL是有效的。因此,上述后处理过程会完全移除URL。
将URL用尖括号括起来是处理包含空格的URL的一种方法。另一种方法是使用百分比编码,但这只是一个附注。
我知道这是一个相当罕见的情况,但我认为它仍然是一个bug。

版本

0.10.37

重现步骤

创建一个markdown文件(不需要手动操作,下面的脚本会自动完成)*

# Heading
This is just an arbitrary text contaning a (link to a local file)[<my/dir/my file2.md> "Referenced File"] that has spaces in the name.

将其保存到 my/dir/file.md ,然后使用

import os
from llama_index.core import SimpleDirectoryReader
from llama_index.readers.file import MarkdownReader

os.makedirs("my/dir", exist_ok=True)
content = """
# Heading
This is just an arbitrary text contaning a (link to a local file)[<my/dir/my file2.md> "Referenced File"] that has spaces in the name.
"""
if not os.path.exists("my/dir/file.md"):
    with open("my/dir/file.md", "w", encoding="utf-8") as f:
        f.write(content)

documents = SimpleDirectoryReader(
    "my/dir",
    filename_as_id=False,
    recursive=True,
    required_exts=[".md"],
    file_extractor={".md": MarkdownReader(remove_hyperlinks=False, remove_images=False)},
).load_data()
print(documents[1].text)

读取它。这将输出:

Heading
This is just an arbitrary text contaning a (link to a local file)[ "Referenced File"] that has spaces in the name.

相关日志/回溯

  • 无响应*
ars1skjm

ars1skjm1#

你好!👋 我来帮助你解决任何bug、问题或你脑海中的其他贡献。让我们一起解决一些bug吧!
要解决MarkdownReader.markdown_to_tups方法删除括号内的URL的问题,你可以修改用于删除HTML标签的正则表达式。具体来说,你可以调整正则表达式以确保它不会匹配括号内的URL。
这是修改后的markdown_to_tups方法:

def markdown_to_tups(self, markdown_text: str) -> List[Tuple[Optional[str], str]]:
    """Convert a markdown file to a dictionary.

The keys are the headers and the values are the text under each header.

"""
    markdown_tups: List[Tuple[Optional[str], str]] = []
    lines = markdown_text.split("\n")

    current_header = None
    current_lines = []
    in_code_block = False

    for line in lines:
        if line.startswith("```"):
            # This is the end of a code block if we are already in it, and vice versa.
            in_code_block = not in_code_block

        header_match = re.match(r"^#+\s", line)
        if not in_code_block and header_match:
            # Upon first header, skip if current text chunk is empty
            if current_header is not None or len(current_lines) > 0:
                markdown_tups.append((current_header, "\n".join(current_lines)))

            current_header = line
            current_lines.clear()
        else:
            current_lines.append(line)

    # Append final text chunk
    markdown_tups.append((current_header, "\n".join(current_lines)))

    # Postprocess the tuples before returning
    return [
        (
            key if key is None else re.sub(r"#", "", key).strip(),
            re.sub(r"<(?!http).*?>", "", value),
        )
        for key, value in markdown_tups
    ]

在这个修改中,使用正则表达式r"<(?!http).*?>"来删除HTML标签,但不删除括号内的URL。负向前瞻(?!http)确保以“http”开头的任何标签(这通常是URL的典型特征)都不会被匹配和删除。

详细信息

关于Dosu

这个响应旨在提供帮助并节省你的时间。它不是精确的解决方案,而是你自己研究的一个起点。
帮助我变得更有用!如果这有帮助,请留下一个👍,如果无关紧要,请留下一个👎。

  • 要继续对话,请提及@dosu。*

相关问题