-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #306 from roshan-research/pn-summary
Add pn-summary corpus reader
- Loading branch information
Showing
6 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
::: hazm.corpus_readers.pn_summary_reader |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
"""این ماژول شامل کلاسها و توابعی برای خواندن پیکرهٔ pn-summary است. | ||
پیکرهٔ [pn-summary](https://github.com/hooshvare/pn-summary) با هدف کمک به سیستمهای یادگیری عمیق و ساخت مدلهای بهتر برای خلاصهسازی دقیقتر متنهای فارسی تهیه شده است. این پیکره شامل ۹۳,۲۰۷ متن خبری تمیزشده است که از ۶ خبرگزاری فارسی و از میان حدوداً ۲۰۰ هزار خبر استخراج شده است. | ||
""" | ||
import csv | ||
from pathlib import Path | ||
from typing import Iterator | ||
from typing import List | ||
from typing import Tuple | ||
|
||
|
||
class PnSummaryReader: | ||
"""این کلاس شامل توابعی برای خواندن پیکرهٔ pn-summary است. | ||
Args: | ||
corpus_folder: مسیر فولدر حاوی فایلهای پیکره. | ||
subset: نوع دیتاست: `test` یا `train` یا `dev` | ||
""" | ||
|
||
def __init__(self: "PnSummaryReader", corpus_folder: str, subset: str="train") -> None: | ||
self._file_paths=Path(corpus_folder).glob(f"{subset}*.csv") | ||
|
||
def docs(self: "PnSummaryReader") -> Iterator[Tuple[str, str, str, str, str, List[str], str, str]]: | ||
"""خبرها را یکبهیک برمیگرداند. | ||
Examples: | ||
>>> pn_summary = PnSummaryReader("pn-summary", "test") | ||
>>> next(pn_summary.docs()) | ||
( | ||
'ff49386698b87be4fc3943bd3cf88987157e1d47', | ||
'کاهش ۵۸ درصدی مصرف نفت کوره منطقه سبزوار', | ||
'مدیر شرکت ملی پخش فرآوردههای نفتی منطقه سبزوار به خبرنگار شانا، گفت..., | ||
'مصرف نفت کوره منطقه سبزوار در بهار امسال، نسبت به مدت مشابه پارسال، ۵۸ درصد کاهش یافت.', | ||
'Oil-Energy', | ||
['پالایش و پخش'], | ||
'Shana', | ||
'https://www.shana.ir/news/243726/%DA%A9%D8%A7%D9%87%D8...' | ||
) | ||
Yields: | ||
خبر بعدی در قالب `(شناسه, عنوان, متن خبر ,خلاصهٔ خبر, موضوع خبر به انگلیسی, [موضوع ۱ به فارسی، موضوع ۲ به فارسی، ...], منبع, لینک)` | ||
""" | ||
for file_path in self._file_paths: | ||
with Path(file_path).open("r", encoding="utf-8") as file: | ||
reader = csv.reader(file, delimiter="\t") | ||
next(reader) # Skip the header row | ||
|
||
for row in reader: | ||
_id, title, article, summary, category, categories, network, link = (field.strip() for field in row) | ||
categories = categories.split("+") | ||
yield (_id, title, article, summary, category, categories, network, link) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from hazm import PnSummaryReader | ||
|
||
|
||
def test_docs(): | ||
pn_summary = PnSummaryReader("tests/files/pn-summary","test") | ||
sentence = next(pn_summary.docs()) | ||
expected = ("ff49386698b87be4fc3943bd3cf88987157e1d47", "کاهش ۵۸ درصدی مصرف نفت کوره منطقه سبزوار", "مدیر شرکت ملی پخش فرآورده\u200cهای نفتی منطقه سبزوار به خبرنگار شانا، گفت: مصرف نفت کوره این منطقه به دلیل توسعه شبکه گاز طبیعی در سه ماه نخست امسال، \u200c با ۵۸ درصد کاهش نسبت به مدت مشابه سال پیش، به ۳۷۰ هزار لیتر رسیده است. [n] عیسی افتخاری سهم بنزین مصرفی این منطقه را نیز در بازه زمانی یاد شده ۳۸ میلیون و ۹۷۵ هزار لیتر اعلام و اظهار کرد: این مقدار از افزایش چهار درصدی مصرف این سوخت در قیاس با بهار پارسال حکایت دارد. [n] به گفته وی، در ۹۳ روز نخست امسال، ۴۶ میلیون و ۲۳۶ هزار لیتر نفت گاز در منطقه سبزوار مصرف شده که نسبت به مدت مشابه سال پیش، با افزایش نیم درصدی همراه شده است. [n] مدیر شرکت ملی پخش فرآورده\u200cهای نفتی منطقه سبزوار همچنین، از مصرف سه میلیون و ۹۵۹ هزار لیتر نفت سفید در این منطقه خبر داد و افزود: مصرف این سوخت در قیاس با سه ماه نخست پارسال، ۲۶ درصد کاهش یافته است. [n] افتخاری تصریح کرد: مجموع مصرف انواع فرآورده نفتی منطقه سبزوار در نخستین فصل امسال، با یک درصد کاهش نسبت به مدت مشابه پارسال، به ۸۹ میلیون و ۵۴۱ هزار لیتر رسیده است. [n] وی یادآور شد: در ۹۳ روز نخست امسال، مصرف گاز طبیعی فشرده (سی ان جی) در منطقه سبزوار، با ۳٫۵ درصد افزایش نسبت به مدت زمان یاد شده در سال پیش، به ۱۰ میلیون و ۶۰۰ هزار مترمکعب رسیده است.", "مصرف نفت کوره منطقه سبزوار در بهار امسال، نسبت به مدت مشابه پارسال، ۵۸ درصد کاهش یافت.", "Oil-Energy", ["پالایش و پخش"], "Shana", "https://www.shana.ir/news/243726/%DA%A9%D8%A7%D9%87%D8%B4-58-%D8%AF%D8%B1%D8%B5%D8%AF%DB%8C-%D9%85%D8%B5%D8%B1%D9%81-%D9%86%D9%81%D8%AA-%DA%A9%D9%88%D8%B1%D9%87-%D9%85%D9%86%D8%B7%D9%82%D9%87-%D8%B3%D8%A8%D8%B2%D9%88%D8%A7%D8%B1") | ||
assert sentence == expected |