Fixes Issue #79. Fixed repetition of text in to_text and to_html methods of the Document class
Fixes #79
Here is the current implementation of the `to_text` method:
```python
def to_text(self):
"""
Returns text of a document by iterating through all the sections '\n'
"""
text = ""
for section in self.sections():
text = text + section.to_text(include_children=True, recurse=True) + "\n"
return text
```
The issue occurs when the document tree has the following (or similar) structure:
```
Section-1
├── Section-2
├── Section-3
```
The text of `Section-2` is included in the output when the `to_text` method is called (recursively) for `Section-1` as well as for`Section-2`.
Similarly, the text of `Section-3` is also duplicated in the output.
To remove the duplicates, iterate over all the sections in the document and choose the top sections i.e., those sections which are not a children of any other section. At the end, concatenate the (recursive) text of all the top sections.
Here is a summary of the changes made:
- The method `_get_top_sections` returns all the top sections in the document tree.
- `to_text` and `to_html` methods accept a boolean parameter `include_duplicates`.
- If `include_duplicates` is `False`, only the top sections are considered.
- Otherwise, all the sections are considered.
You can take a look at this fix in **Google Colab** [here](https://colab.research.google.com/drive/1YatMfrkFxJYfB6Ohkml9PGhZTnTng3-i?usp=sharing).
合并状态:已合并 合并于 2024-06-13 关闭于 2024-06-13 5 条评论