import json import os from typing import List from huggingface_hub import hf_hub_url import datasets _KnessetCorpus_CITATION = """\ @article{ } """ _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) """ _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) """ _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) """ _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=["https://dl.fbaipublicfiles.com/glue/superglue/data/v2/BoolQ.zip?raw=true"],#TODO 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=["https://huggingface.co/datasets/HaifaCLGroup/KnessetCorpus/resolve/main/protocols_sentences/committee_full_sentences.jsonl.bz2","https://huggingface.co/datasets/HaifaCLGroup/KnessetCorpus/resolve/main/protocols_sentences/plenary_full_sentences.jsonl.bz2"], 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=["https://huggingface.co/datasets/HaifaCLGroup/KnessetCorpus/resolve/main/protocols_sentences/committee_full_sentences.jsonl.bz2"], 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=["https://huggingface.co/datasets/HaifaCLGroup/KnessetCorpus/resolve/main/protocols_sentences/plenary_full_sentences.jsonl.bz2"], 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: 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"), "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")}), "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 }, ), ] def _generate_examples(self, data_files): print(f'data_files:{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)