-
Notifications
You must be signed in to change notification settings - Fork 0
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 #37 from tma15/fix/refactor
Fix/refactor
- Loading branch information
Showing
26 changed files
with
312 additions
and
106 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
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
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 |
---|---|---|
@@ -1,17 +1,33 @@ | ||
import csv | ||
from pathlib import Path | ||
|
||
from datasets import Dataset, load_dataset | ||
|
||
|
||
def load_data( | ||
data_path: str | Path, | ||
label_column: str = "label", | ||
text_column: str = "text", | ||
) -> tuple[list[str], list[str]]: | ||
if isinstance(data_path, str): | ||
data_path = Path(data_path) | ||
|
||
def load_data(data_path: str | Path) -> tuple[list[str], list[str]]: | ||
labels: list[str] = [] | ||
texts: list[str] = [] | ||
with open(data_path) as f: | ||
reader = csv.reader(f) | ||
for row in reader: | ||
if len(row) < 2: | ||
continue | ||
if len(row[0]) == 0 or len(row[1]) == 0: | ||
continue | ||
labels.append(row[0]) | ||
texts.append(row[1]) | ||
return labels, texts | ||
|
||
if data_path.suffix in [".csv", ".json", ".jsonl"]: | ||
suffix: str = data_path.suffix[1:] | ||
|
||
# Because datasets does not support jsonl suffix, convert it to json | ||
if suffix == "jsonl": | ||
suffix = "json" | ||
|
||
# When data_files is only a single data_path, data split is "train" | ||
dataset: Dataset = load_dataset(suffix, data_files=str(data_path))["train"] | ||
|
||
for sample in dataset: | ||
labels.append(sample[label_column]) | ||
texts.append(sample[text_column]) | ||
return labels, texts | ||
|
||
else: | ||
raise ValueError(data_path.suffix) |
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
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
Oops, something went wrong.