Download ocr_metadata/token_count/make_count.py from chcaa/kb-books: direct link, hf CLI and curl.
- Browser
- Download file 3.27 kB
-
https://huggingface.co/datasets/chcaa/kb-books/resolve/main/ocr_metadata/token_count/make_count.py
- Command line
-
hf download hf://datasets/chcaa/kb-books/ocr_metadata/token_count/make_count.py
-
curl -L -o make_count.py https://huggingface.co/datasets/chcaa/kb-books/resolve/main/ocr_metadata/token_count/make_count.py
3.27 kB
| # /// script | |
| # requires-python = "==3.12" | |
| # dependencies = [ | |
| # "huggingface-hub>= 0.34.4" | |
| # "datasets>=3.5.0", | |
| # ] | |
| # /// | |
| import regex as re | |
| import pandas as pd | |
| from datasets import load_dataset, concatenate_datasets | |
| from huggingface_hub import HfFileSystem | |
| from u_func import * | |
| #consider appendix | |
| appendix = False | |
| #which | |
| a="_4" | |
| #set years | |
| start_year = 1930 | |
| #last year to process | |
| stop_year = 1930 | |
| year_list = sorted(make_year_list(start_year,stop_year)) | |
| if appendix: | |
| year_list = [i+a for i in year_list] | |
| def get_files_year(year:str)->list[str]: | |
| #get filenames in a year | |
| fs = HfFileSystem() | |
| # List files | |
| try: | |
| f_dict = fs.ls(f"datasets/chcaa/kb-books/data/{year}") | |
| f_names = [i.get("name") for i in f_dict] | |
| f_names = [re.sub("datasets/chcaa/kb-books/","",i) for i in f_names] | |
| except FileNotFoundError: | |
| f_names = "Not Found" | |
| return f_names | |
| def main(): | |
| #load in wordset to check against | |
| danish_words=list(pd.read_csv("./ods_fullforms_2020-08-26.csv", sep = "\t")["A"]) | |
| danish_words = [i.lower() for i in danish_words] | |
| danish_words = list(set(danish_words)) | |
| for year in year_list: | |
| print(f"STARTING YEAR {year}...") | |
| #load in data | |
| #year_path = os.path.join("..","data",year) | |
| #year_data = load_from_disk(year_path) | |
| year_files = get_files_year(year) | |
| if year_files == "Not Found": | |
| print(f"{year} does not exist") | |
| continue | |
| year_data = load_dataset("chcaa/kb-books", split="train", | |
| data_files=year_files) | |
| #count tokens | |
| #drop image | |
| year_data = year_data.remove_columns("page_image") | |
| #shuffling and sharding is to try and increase efficiency of the processes | |
| #if a single process gets stuck with an unlucky draw of pages the available cpu | |
| #power is not fully utilized | |
| #shuffle dataset | |
| year_data = year_data.shuffle(seed=1956) | |
| #flatten indices | |
| year_data = year_data.flatten_indices() | |
| #shard the year | |
| year_data = shard_ds(year_data) | |
| #print(year_data) | |
| yd_list = [] | |
| for i in year_data: | |
| i = i.map( | |
| count_tokens_and_words, | |
| fn_kwargs = {"colname":"page_text", | |
| "wordlist":danish_words}, | |
| num_proc = 16) | |
| yd_list.append(i) | |
| #print(yd_list) | |
| #reassemble | |
| year_data = concatenate_datasets(yd_list) | |
| cols = year_data.column_names | |
| wanted_cols = ["page_id","title","published", | |
| "page_text_n_tokens", | |
| "page_text_rel_a_words", | |
| "page_text_rel_words", | |
| "page_text_punkt_tokens", | |
| "page_text_a_tokens", | |
| "file_name"] | |
| unwanted_cols = [i for i in cols if i not in wanted_cols] | |
| #remove unnecessary columns | |
| year_data = year_data.remove_columns(column_names=unwanted_cols) | |
| #save by year | |
| year_data.to_parquet(f"./metadata/{year}.parquet") | |
| if __name__ == "__main__": | |
| main() |