|
|
|
|
|
import re |
|
from unstructured.partition.html import partition_html |
|
from unstructured.staging.base import convert_to_isd |
|
from unstructured.cleaners.core import clean |
|
from build_index.parser.base import BaseParser |
|
|
|
|
|
class HTMLParser(BaseParser): |
|
def parse_file(self, file): |
|
with open(file, "r", encoding="utf-8") as fp: |
|
elements = partition_html(file=fp) |
|
isd = convert_to_isd(elements) |
|
|
|
for isd_el in isd: |
|
isd_el['text'] = isd_el['text'].encode("ascii", "ignore").decode() |
|
isd_el['text'] = self.remove_dup_space(isd_el['text']) |
|
isd_el['text'] = self.remove_empty_line(isd_el['text']) |
|
clean(isd_el['text'], extra_whitespace=True, dashes=True, bullets=True, trailing_punctuation=True ) |
|
|
|
|
|
title_indexes = [i for i, isd_el in enumerate(isd) if isd_el['type'] == 'Title'] |
|
|
|
|
|
|
|
|
|
|
|
|
|
Chunks = [[]] |
|
final_chunks = list(list()) |
|
|
|
for i, isd_el in enumerate(isd): |
|
if i in title_indexes: |
|
Chunks.append([]) |
|
Chunks[-1].append(isd_el['text']) |
|
|
|
|
|
for chunk in Chunks: |
|
|
|
sum = 0 |
|
sum += len(str(chunk)) |
|
if sum < 25: |
|
Chunks.remove(chunk) |
|
else : |
|
|
|
final_chunks.append(" ".join([str(item) for item in chunk])) |
|
return final_chunks |
|
|