當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python PyTorch ParagraphAggregator用法及代碼示例

本文簡要介紹python語言中 torchdata.datapipes.iter.ParagraphAggregator 的用法。

用法:

class torchdata.datapipes.iter.ParagraphAggregator(source_datapipe: IterDataPipe[Tuple[str, T_co]], joiner: Callable = <function _default_line_join>)

參數

  • source_datapipe-帶有文件名和行元組的 DataPipe

  • joiner-將行列表連接在一起的函數

將同一文件中的文本行聚合到單個段落中(函數名稱:lines_to_paragraphs)。具體來說,它接受由文件名和行的元組組成的DataPipe。對於每個元組,它檢查文件名是否與前一個元組的文件名匹配。如果是,它將當前行與現有段落連接起來。如果文件名不匹配,則生成現有段落並開始新段落。

示例

>>> from torchdata.datapipes.iter import IterableWrapper
>>> source_dp = IterableWrapper(
>>>                 [("file1", "Line1"), ("file1", "Line2"), ("file2", "Line2,1"), ("file2", "Line2,2"), ("file2", "Line2,3")]
>>>             )
>>> para_agg_dp = source_dp.lines_to_paragraphs(joiner=lambda ls: " ".join(ls))
>>> list(para_agg_dp)
[('file1', 'Line1 Line2'), ('file2', 'Line2,1 Line2,2 Line2,3')]

相關用法


注:本文由純淨天空篩選整理自pytorch.org大神的英文原創作品 torchdata.datapipes.iter.ParagraphAggregator。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。