Download ocr_metadata/scripts/u_func.py from chcaa/kb-books: direct link, hf CLI and curl.
- Browser
- Download file 2.91 kB
-
https://huggingface.co/datasets/chcaa/kb-books/resolve/main/ocr_metadata/scripts/u_func.py
- Command line
-
hf download hf://datasets/chcaa/kb-books/ocr_metadata/scripts/u_func.py
-
curl -L -o u_func.py https://huggingface.co/datasets/chcaa/kb-books/resolve/main/ocr_metadata/scripts/u_func.py
2.91 kB
| from datasets import Dataset | |
| def only_nan(input:list) -> bool: | |
| #remove nans and whitespaces | |
| c_input = [e for e in input if e==e and e!=" "] | |
| if len(c_input) > 0: | |
| return False | |
| else: | |
| return True | |
| def shard_ds(ds:Dataset, size=1000) -> list[Dataset]: | |
| if len(ds) < 1000: | |
| ds_list = [ds] | |
| else: | |
| ds_list = [] | |
| n_s = round(len(ds)/size) | |
| indices = [i for i in range(n_s)] | |
| for i in indices: | |
| data_shard = ds.shard(num_shards=n_s, index = i) | |
| ds_list.append(data_shard) | |
| return ds_list | |
| def word_tokenize(text: str) -> list[str]: | |
| """ | |
| Tokenizes a string into words, splitting on whitespace and punctuation. | |
| Example: | |
| >>> word_tokenize("Hello, world!") | |
| ['Hello', ',', 'world', '!'] | |
| >>> word_tokenize("This is a test.") | |
| ['This', 'is', 'a', 'test', '.'] | |
| >>> word_tokenize("Many spaces between words.") | |
| ['Many', 'spaces', 'between', 'words', '.'] | |
| """ | |
| punkt = [",", ".", "!", "?", ":", ";", "(", ")", "[", "]", "{", "}", '"', "'"] | |
| for p in punkt: | |
| text = text.replace(p, f" {p} ") | |
| t = text.split() | |
| return t | |
| def remove_punkt_and_numbers(tokens:list[str]) -> list[str]: | |
| punkt = [",", ".", "!", "?", ":", ";", "(", ")", "[", "]", "{", "}", '"', "'"] | |
| tokens = [i.lower() for i in tokens if i not in punkt and i.isalpha()] | |
| return tokens | |
| def count_tokens_and_words(page:Dataset,colname:str,wordlist:list[str])-> Dataset: | |
| """returns all token count including numbers and punctuation, | |
| as well as relation of dictionary items to all tokens (measures general quality), | |
| and relation of dictionary items to only alphabetic words (measures difference between ocr methods) | |
| """ | |
| toks = word_tokenize(page[colname]) | |
| a_toks = remove_punkt_and_numbers(toks) | |
| t_length = len(toks) | |
| punkt = [",", ".", "!", "?", ":", ";", "(", ")", "[", "]", "{", "}", '"', "'"] | |
| t_punkt = [i for i in toks if i in punkt] | |
| n_punkt = len(t_punkt) | |
| word_c = [i for i in a_toks if i in wordlist] | |
| if len(a_toks) == 0: | |
| wc_rel = 0.0 | |
| wc_a_rel = 0.0 | |
| else: | |
| wc_rel = round(len(word_c)/len(toks),4) | |
| wc_a_rel = round(len(word_c)/len(a_toks),4) | |
| t_name = colname + "_n_tokens" | |
| wc_name = colname + "_rel_words" | |
| wc_a_name = colname + "_rel_a_words" | |
| p_t_name = colname + "_punkt_tokens" | |
| a_t_name = colname + "_a_tokens" | |
| page.update({t_name:t_length, | |
| wc_name:wc_rel, | |
| wc_a_name:wc_a_rel, | |
| p_t_name:n_punkt, | |
| a_t_name:len(a_toks)}) | |
| return page | |
| def make_year_list(start_year: int, stop_year: int) -> list[str]: | |
| """make a list of file names based on years""" | |
| year_list = list(range(start_year, stop_year + 1)) | |
| year_list = [str(i) for i in year_list] | |
| return year_list | |