KnessetCorpus / knessetCorpus.py
GiliGold's picture
Update knessetCorpus.py
168f2b4 verified
raw
history blame
No virus
27.4 kB
import json
import os
from typing import List
from huggingface_hub import hf_hub_url
import datasets
_KnessetCorpus_CITATION = "@misc{goldin2024knesset, title={The Knesset Corpus: An Annotated Corpus of Hebrew Parliamentary Proceedings}, author={Gili Goldin and Nick Howell and Noam Ordan and Ella Rabinovich and Shuly Wintner}, year={2024}, eprint={2405.18115}, archivePrefix={arXiv}, primaryClass={cs.CL} }"
_KnessetCorpus_DESCRIPTION = """
An annotated corpus of Hebrew parliamentary proceedings containing over 32 million sentences from all the (plenary and committee) protocols held in the Israeli parliament from 1998 to 2022.
Sentences are annotated with various levels of linguistic information, including part-of-speech tags, morphological features, dependency structures, and named entities.
They are also associated with detailed meta-information reflecting demographic and political properties of the speakers, based on a large database of parliament members and factions that we compiled.
"""
_AllFeaturesSentences_DESCRIPTION = """\
AllFeaturesSentences consists of samples of all the sentences in the corpus (plenary and committee) together with all the features available in the dataset:\
The features are consistent with the Sentence model features, the Protocol model features of the protocol the sentence belongs to,
the Person model features of the speaker and the Faction model features \
of the faction of the speaker at that time.
This is roughly equivalent to a join between all models in dataset (Protocol, Faction, Person, Sentence)
"""
_NoMorphAllFeaturesSentences_DESCRIPTION = """\
AllFeaturesSentences consists of samples of all the sentences in the corpus (plenary and committee) together with all the non-morphological features available in the dataset :\
The features are consistent with the Sentence model features, the Protocol model features of the protocol the sentence belongs to,
the Person model features of the speaker and the Faction model features \
of the faction of the speaker at that time.
This is roughly equivalent to a join between all models in dataset (Protocol, Faction, Person, Sentence)
Ideal if morpho-syntactic annotations aren't relevant to your work, providing a less disk space heavy option.
"""
_CommitteeAllFeaturesSentences_DESCRIPTION = """\
CommitteeAllFeaturesSentences consists of samples of all the sentences in the committee sessions in the corpus together with all the features available in the dataset:\
The features are consistent with the Sentence model features, the Protocol model features of the protocol the sentence belongs to,
the Person model features of the speaker and the Faction model features \
of the faction of the speaker at that time.
This is roughly equivalent to a join between all models in dataset (Protocol, Faction, Person, Sentence)
"""
_NoMorphCommitteeAllFeaturesSentences_DESCRIPTION = """\
AllFeaturesSentences consists of samples of all the sentences in the committee sessions in the corpus together with all the non-morphological features available in the dataset :\
The features are consistent with the Sentence model features, the Protocol model features of the protocol the sentence belongs to,
the Person model features of the speaker and the Faction model features \
of the faction of the speaker at that time.
This is roughly equivalent to a join between all models in dataset (Protocol, Faction, Person, Sentence)
Ideal if morpho-syntactic annotations aren't relevant to your work, providing a less disk space heavy option.
"""
_PlenaryAllFeaturesSentences_DESCRIPTION = """\
PlenaryAllFeaturesSentences consists of samples of all the sentences in the plenary sessions in the corpus together with all the features available in the dataset:\
The features are consistent with the Sentence model features, the Protocol model features of the protocol the sentence belongs to,
the Person model features of the speaker and the Faction model features \
of the faction of the speaker at that time.
This is roughly equivalent to a join between all models in dataset (Protocol, Faction, Person, Sentence)
"""
_NoMorphPlenaryAllFeaturesSentences_DESCRIPTION = """\
AllFeaturesSentences consists of samples of all the sentences in the plenary sessions in the corpus together with all the non-morphological features available in the dataset :\
The features are consistent with the Sentence model features, the Protocol model features of the protocol the sentence belongs to,
the Person model features of the speaker and the Faction model features \
of the faction of the speaker at that time.
This is roughly equivalent to a join between all models in dataset (Protocol, Faction, Person, Sentence)
Ideal if morpho-syntactic annotations aren't relevant to your work, providing a less disk space heavy option.
"""
_KnessetMembers_DESCRIPTION = """\
KnessetMembers consists of samples of the knesset members in the dataset and their meta-data information such as name, gender and factions affiliations.
The features are consistent with the Person model features
"""
_Factions_DESCRIPTION = """\
Factions consists of samples of the factions in the dataset and their meta-data information such as name, political orientation and active periods.
The features are consistent with the Faction model features
"""
_Protocols_DESCRIPTION = """\
Protocols consists of samples of the protocols in the dataset and their meta-data information such as date, knesset number, session name and a list of its sentences.
The features are consistent with the Protocol model features and the features for each sentence are consistent with the Sentence model
"""
class KnessetCorpusConfig(datasets.BuilderConfig):
"""BuilderConfig for KnessetCorpus."""
def __init__(self, features, data_urls, citation, **kwargs):
"""BuilderConfig for KnessetCorpus.
Args:
features: *list[string]*, list of the features that will appear in the
feature dict. Should not include "label".
data_urls: *list[string]*, urls to download the files from.
citation: *string*, citation for the data set.
**kwargs: keyword arguments forwarded to super.
"""
super().__init__(version=datasets.Version("1.0.1"), **kwargs)
self.features = features
self.data_urls = data_urls
self.citation = citation
class KnessetCorpus(datasets.GeneratorBasedBuilder):
BUILDER_CONFIG_CLASS = KnessetCorpusConfig
BUILDER_CONFIGS = [
KnessetCorpusConfig(
name="knessetMembers",
description=_KnessetMembers_DESCRIPTION,
features=["person_id", "first_name", "last_name", "full_name", "is_knesset_member", "gender", "email", "is_current", "last_updated_date", "date_of_birth", "place_of_birth", "year_of_aliya", "date_of_death", "mother_tongue", "religion", "nationality", "religious_orientation", "residence", "factions_memberships", "languages","allSources", "wikiLink","notes" ],
data_urls=["https://huggingface.co/datasets/HaifaCLGroup/KnessetCorpus/raw/main/all_knesset_members_jsons.jsonl"],
citation=_KnessetCorpus_CITATION,
),
KnessetCorpusConfig(
name="factions",
description=_Factions_DESCRIPTION,
features=["faction_name", "faction_popular_initials", "faction_id", "active_periods", "knesset_numbers", "coalition_or_opposition_memberships", "political_orientation", "other_names", "notes", "wiki_link", ],
data_urls=["https://huggingface.co/datasets/HaifaCLGroup/KnessetCorpus/raw/main/factions_jsons.jsonl"],
citation=_KnessetCorpus_CITATION,
),
KnessetCorpusConfig(
name="protocols",
description=_Protocols_DESCRIPTION,
features=["protocol_name", "session_name", "parent_session_name", "knesset_number", "protocol_number", "protocol_date", "is_ocr_output", "protocol_type", "protocol_sentences"],
data_urls=[f"https://huggingface.co/datasets/HaifaCLGroup/KnessetCorpus/resolve/main/protocols_sentences/committee_protocols_jsons.zip", f"https://huggingface.co/datasets/HaifaCLGroup/KnessetCorpus/resolve/main/protocols_sentences/plenary_protocols_jsons.zip"],
citation=_KnessetCorpus_CITATION,
),
KnessetCorpusConfig(
name="all_features_sentences",
description=_AllFeaturesSentences_DESCRIPTION,
features=[ "_id", "knesset_number", "protocol_name", "protocol_number", "protocol_type", "session_name", "parent_session_name", "@timestamp", "protocol_date", "is_ocr_output", "sentence_id", "speaker_id", "speaker_name", "is_valid_speaker", "is_chairman", "turn_num_in_protocol", "sent_num_in_turn", "sentence_text", "morphological_fields", "factuality_fields", "speaker_first_name", "speaker_last_name", "speaker_is_knesset_member", "speaker_gender", "speaker_email", "speaker_last_updated_date", "speaker_date_of_birth", "speaker_place_of_birth","speaker_year_of_aliya", "speaker_date_of_death", "speaker_mother_tongue", "speaker_religion", "speaker_nationality", "speaker_religious_orientation", "speaker_residence", "speaker_factions_memberships", "speaker_languages", "speaker_sources", "speaker_notes","faction_id", "faction_general_name", "knesset_faction_id", "current_faction_name", "faction_popular_initials", "faction_active_periods", "faction_knesset_numbers","faction_coalition_or_opposition_memberships", "faction_political_orientation", "faction_other_names", "faction_notes", "faction_wiki_link"],
data_urls=[f"https://huggingface.co/datasets/HaifaCLGroup/KnessetCorpus/resolve/main/protocols_sentences/plenary_full_sentences_shards_bzip2_files/plenary_full_sentences_shard_{str(i).zfill(2)}.jsonl.bz2" for i in range(27)]+[f"https://huggingface.co/datasets/HaifaCLGroup/KnessetCorpus/resolve/main/protocols_sentences/committee_full_sentences_shards_bzip2_files/committee_full_sentences_shard_{str(i).zfill(2)}.jsonl.bz2" for i in range(83)],
citation=_KnessetCorpus_CITATION,
),
KnessetCorpusConfig(
name="no_morph_all_features_sentences",
description=_NoMorphAllFeaturesSentences_DESCRIPTION,
features=["_id", "knesset_number", "protocol_name", "protocol_number", "protocol_type", "session_name",
"parent_session_name", "@timestamp", "protocol_date", "is_ocr_output", "sentence_id",
"speaker_id", "speaker_name", "is_valid_speaker", "is_chairman", "turn_num_in_protocol",
"sent_num_in_turn", "sentence_text", "factuality_fields",
"speaker_first_name", "speaker_last_name", "speaker_is_knesset_member", "speaker_gender",
"speaker_email", "speaker_last_updated_date", "speaker_date_of_birth", "speaker_place_of_birth",
"speaker_year_of_aliya", "speaker_date_of_death", "speaker_mother_tongue", "speaker_religion",
"speaker_nationality", "speaker_religious_orientation", "speaker_residence",
"speaker_factions_memberships", "speaker_languages", "speaker_sources", "speaker_notes",
"faction_id", "faction_general_name", "knesset_faction_id", "current_faction_name",
"faction_popular_initials", "faction_active_periods", "faction_knesset_numbers",
"faction_coalition_or_opposition_memberships", "faction_political_orientation",
"faction_other_names", "faction_notes", "faction_wiki_link"],
data_urls=[
f"https://huggingface.co/datasets/HaifaCLGroup/KnessetCorpus/resolve/main/protocols_sentences/plenary_no_morph_sentences_shards_bzip2_files/no_morph_plenary_full_sentences_shard_{str(i).zfill(2)}.jsonl.bz2" for i in range(27)]+[ f"https://huggingface.co/datasets/HaifaCLGroup/KnessetCorpus/resolve/main/protocols_sentences/committee_no_morph_sentences_shards_bzip2_files/no_morph_committee_full_sentences_shard_{str(i).zfill(2)}.jsonl.bz2" for i in range(27)],#TODO update num of shards
citation=_KnessetCorpus_CITATION,
),
KnessetCorpusConfig(
name="committees_all_features_sentences",
description=_AllFeaturesSentences_DESCRIPTION,
features=[ "_id", "knesset_number", "protocol_name", "protocol_number", "protocol_type", "session_name", "parent_session_name", "@timestamp", "protocol_date", "is_ocr_output", "sentence_id", "speaker_id", "speaker_name", "is_valid_speaker", "is_chairman", "turn_num_in_protocol", "sent_num_in_turn", "sentence_text", "morphological_fields", "factuality_fields", "speaker_first_name", "speaker_last_name", "speaker_is_knesset_member", "speaker_gender", "speaker_email", "speaker_last_updated_date", "speaker_date_of_birth", "speaker_place_of_birth","speaker_year_of_aliya", "speaker_date_of_death", "speaker_mother_tongue", "speaker_religion", "speaker_nationality", "speaker_religious_orientation", "speaker_residence", "speaker_factions_memberships", "speaker_languages", "speaker_sources", "speaker_notes","faction_id", "faction_general_name", "knesset_faction_id", "current_faction_name", "faction_popular_initials", "faction_active_periods", "faction_knesset_numbers","faction_coalition_or_opposition_memberships", "faction_political_orientation", "faction_other_names", "faction_notes", "faction_wiki_link"],
data_urls=[f"https://huggingface.co/datasets/HaifaCLGroup/KnessetCorpus/resolve/main/protocols_sentences/committee_full_sentences_shards_bzip2_files/committee_full_sentences_shard_{str(i).zfill(2)}.jsonl.bz2" for i in range(83)],
citation=_KnessetCorpus_CITATION,
),
KnessetCorpusConfig(
name="no_morph_committee_all_features_sentences",
description=_NoMorphCommitteeAllFeaturesSentences_DESCRIPTION,
features=["_id", "knesset_number", "protocol_name", "protocol_number", "protocol_type", "session_name",
"parent_session_name", "@timestamp", "protocol_date", "is_ocr_output", "sentence_id",
"speaker_id", "speaker_name", "is_valid_speaker", "is_chairman", "turn_num_in_protocol",
"sent_num_in_turn", "sentence_text", "factuality_fields",
"speaker_first_name", "speaker_last_name", "speaker_is_knesset_member", "speaker_gender",
"speaker_email", "speaker_last_updated_date", "speaker_date_of_birth", "speaker_place_of_birth",
"speaker_year_of_aliya", "speaker_date_of_death", "speaker_mother_tongue", "speaker_religion",
"speaker_nationality", "speaker_religious_orientation", "speaker_residence",
"speaker_factions_memberships", "speaker_languages", "speaker_sources", "speaker_notes",
"faction_id", "faction_general_name", "knesset_faction_id", "current_faction_name",
"faction_popular_initials", "faction_active_periods", "faction_knesset_numbers",
"faction_coalition_or_opposition_memberships", "faction_political_orientation",
"faction_other_names", "faction_notes", "faction_wiki_link"],
data_urls=[ f"https://huggingface.co/datasets/HaifaCLGroup/KnessetCorpus/resolve/main/protocols_sentences/committee_no_morph_sentences_shards_bzip2_files/no_morph_committee_full_sentences_shard_{str(i).zfill(2)}.jsonl.bz2" for i in range(83)],
citation=_KnessetCorpus_CITATION,
),
KnessetCorpusConfig(
name="no_morph_plenary_all_features_sentences",
description=_NoMorphPlenaryAllFeaturesSentences_DESCRIPTION,
features=["_id", "knesset_number", "protocol_name", "protocol_number", "protocol_type", "session_name",
"parent_session_name", "@timestamp", "protocol_date", "is_ocr_output", "sentence_id",
"speaker_id", "speaker_name", "is_valid_speaker", "is_chairman", "turn_num_in_protocol",
"sent_num_in_turn", "sentence_text", "factuality_fields",
"speaker_first_name", "speaker_last_name", "speaker_is_knesset_member", "speaker_gender",
"speaker_email", "speaker_last_updated_date", "speaker_date_of_birth", "speaker_place_of_birth",
"speaker_year_of_aliya", "speaker_date_of_death", "speaker_mother_tongue", "speaker_religion",
"speaker_nationality", "speaker_religious_orientation", "speaker_residence",
"speaker_factions_memberships", "speaker_languages", "speaker_sources", "speaker_notes",
"faction_id", "faction_general_name", "knesset_faction_id", "current_faction_name",
"faction_popular_initials", "faction_active_periods", "faction_knesset_numbers",
"faction_coalition_or_opposition_memberships", "faction_political_orientation",
"faction_other_names", "faction_notes", "faction_wiki_link"],
data_urls=[
f"https://huggingface.co/datasets/HaifaCLGroup/KnessetCorpus/resolve/main/protocols_sentences/plenary_no_morph_sentences_shards_bzip2_files/no_morph_plenary_full_sentences_shard_{str(i).zfill(2)}.jsonl.bz2" for i in range(27)],
citation=_KnessetCorpus_CITATION,
),
KnessetCorpusConfig(
name="plenary_all_features_sentences",
description=_AllFeaturesSentences_DESCRIPTION,
features=[ "_id", "knesset_number", "protocol_name", "protocol_number", "protocol_type", "session_name", "parent_session_name", "@timestamp", "protocol_date", "is_ocr_output", "sentence_id", "speaker_id", "speaker_name", "is_valid_speaker", "is_chairman", "turn_num_in_protocol", "sent_num_in_turn", "sentence_text", "morphological_fields", "factuality_fields", "speaker_first_name", "speaker_last_name", "speaker_is_knesset_member", "speaker_gender", "speaker_email", "speaker_last_updated_date", "speaker_date_of_birth", "speaker_place_of_birth","speaker_year_of_aliya", "speaker_date_of_death", "speaker_mother_tongue", "speaker_religion", "speaker_nationality", "speaker_religious_orientation", "speaker_residence", "speaker_factions_memberships", "speaker_languages", "speaker_sources", "speaker_notes","faction_id", "faction_general_name", "knesset_faction_id", "current_faction_name", "faction_popular_initials", "faction_active_periods", "faction_knesset_numbers","faction_coalition_or_opposition_memberships", "faction_political_orientation", "faction_other_names", "faction_notes", "faction_wiki_link"],
data_urls=[f"https://huggingface.co/datasets/HaifaCLGroup/KnessetCorpus/resolve/main/protocols_sentences/plenary_full_sentences_shards_bzip2_files/plenary_full_sentences_shard_{str(i).zfill(2)}.jsonl.bz2" for i in range(27)],
citation=_KnessetCorpus_CITATION,
)]
DEFAULT_CONFIG_NAME = "all_features_sentences"
def _info(self):
features = {feature: datasets.Value("string") for feature in self.config.features}
if "all_features_sentences" in self.config.name:
if not "no_morph" in self.config.name:
features.update({"morphological_fields": datasets.Sequence({
"id": datasets.Value("string"),
"form": datasets.Value("string"),
"lemma": datasets.Value("string"),
"upos": datasets.Value("string"),
"xpos": datasets.Value("string"),
"feats": datasets.Value("string"),
"head": datasets.Value("int32"),
"deprel": datasets.Value("string"),
"deps": datasets.Value("string"),
"misc": datasets.Value("string")})})
features.update({
"faction_active_periods": datasets.Sequence({
"end_date": datasets.Value("string"), # Date converted to string
"start_date": datasets.Value("string"), # Date converted to string
}),
"faction_coalition_or_opposition_memberships": datasets.Sequence({
"end_date": datasets.Value("string"), # Date converted to string
"knesset_faction_name": datasets.Value("string"),
"knesset_num": datasets.Value("string"),
"member_of_coalition": datasets.Value("bool"),
"notes": datasets.Value("string"),
"start_date": datasets.Value("string") # Date converted to string
}),
"faction_other_names": datasets.Sequence({
"name": datasets.Value("string"),
"name_end_date": datasets.Value("string"), # Date converted to string
"name_start_date": datasets.Value("string") # Date converted to string
}),
"is_chairman": datasets.Value("bool"),
"is_ocr_output": datasets.Value("bool"),
"is_valid_speaker": datasets.Value("bool"),
"sent_num_in_turn": datasets.Value("int64"),
"turn_num_in_protocol": datasets.Value("int64"),
"speaker_factions_memberships": datasets.Sequence({
"end_date": datasets.Value("string"),
"faction_id": datasets.Value("string"),
"faction_name": datasets.Value("string"),
"knesset_faction_id": datasets.Value("string"),
"knesset_number": datasets.Value("string"),
"start_date": datasets.Value("string")}),
"speaker_is_knesset_member": datasets.Value("bool"),
"speaker_notes": datasets.Sequence(datasets.Value("string")),
"speaker_languages": datasets.Sequence(datasets.Value("string"))
})
if self.config.name == "protocols":
features.update({
"is_ocr_output": datasets.Value("bool"),
"protocol_sentences": datasets.Sequence({
"sentence_id": datasets.Value("string"),
"protocol_name": datasets.Value("string"),
"speaker_id": datasets.Value("string"),
"speaker_name": datasets.Value("string"),
"is_valid_speaker": datasets.Value("bool"),
"turn_num_in_protocol": datasets.Value("int64"),
"sent_num_in_turn": datasets.Value("int64"),
"sentence_text": datasets.Value("string"),
"is_chairman": datasets.Value("bool"),
"morphological_fields": datasets.Sequence({
"id": datasets.Value("string"),
"form": datasets.Value("string"),
"lemma": datasets.Value("string"),
"upos": datasets.Value("string"),
"xpos": datasets.Value("string"),
"feats": datasets.Value("string"),
"head": datasets.Value("int32"),
"deprel": datasets.Value("string"),
"deps": datasets.Value("string"),
"misc": datasets.Value("string"),
}),
"factuality_fields": datasets.Value("string")
})
})
if self.config.name == "factions":
features.update({
"active_periods": datasets.Sequence({
"end_date": datasets.Value("string"), # Date converted to string
"start_date": datasets.Value("string"), # Date converted to string
}),
"coalition_or_opposition_memberships": datasets.Sequence({
"end_date": datasets.Value("string"), # Date converted to string
"knesset_faction_name": datasets.Value("string"),
"knesset_num": datasets.Value("string"),
"member_of_coalition": datasets.Value("bool"),
"notes": datasets.Value("string"),
"start_date": datasets.Value("string"), # Date converted to string
}),
"other_names": datasets.Sequence({
"name": datasets.Value("string"),
"name_end_date": datasets.Value("string"), # Date converted to string
"name_start_date": datasets.Value("string"), # Date converted to string
})
})
if self.config.name == "KnessetMembers":
features.update({
"is_knesset_member": datasets.Value("bool"),
"factions_memberships": datasets.Sequence({
"end_date": datasets.Value("string"),
"faction_id": datasets.Value("string"),
"faction_name": datasets.Value("string"),
"knesset_faction_id": datasets.Value("string"),
"knesset_number": datasets.Value("string"),
"start_date": datasets.Value("string")}),
"notes": datasets.Sequence(datasets.Value("string")),
"languages": datasets.Sequence(datasets.Value("string"))
})
return datasets.DatasetInfo(
description=_KnessetCorpus_DESCRIPTION + self.config.description,
features=datasets.Features(features),
citation=self.config.citation
)
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
urls = self.config.data_urls
downloaded_files = dl_manager.download_and_extract(urls)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"data_files": downloaded_files[url]
},
)
for url in urls
]
def _generate_examples(self, data_files):
if "all_features_sentences" in self.config.name:
id_field_name = "sentence_id"
elif self.config.name == "protocols":
id_field_name = "protocol_name"
elif self.config.name == "factions":
id_field_name = "faction_id"
elif self.config.name == "knessetMembers":
id_field_name = "person_id"
for i, filepath in enumerate(data_files):
with open(filepath, encoding="utf-8") as f:
for line in f:
sample = {}
try:
row = json.loads(line)
except Exception as e:
print(f'couldnt load sample. error was: {e}. Continuing to next sample')
continue
id_ = row.get(id_field_name, None)
if id_ is None:
print(f"Key '{id_field_name}' not found in row. Skipping this row. row is: {row}")
continue
for feature in self._info().features:
sample[feature] = row[feature]
try:
yield id_, sample
except Exception as e:
print(f'couldnt yield sample. error: {e}. sample is: {sample}.', flush=True)