daza | e3bc92e | 2020-11-04 11:06:26 +0100 | [diff] [blame] | 1 | import argparse |
| 2 | import spacy |
| 3 | from spacy.tokens import Doc |
| 4 | import logging, sys, time |
| 5 | from lib.CoNLL_Annotation import get_token_type |
| 6 | import my_utils.file_utils as fu |
| 7 | from germalemma import GermaLemma |
| 8 | |
| 9 | |
| 10 | class WhitespaceTokenizer(object): |
| 11 | def __init__(self, vocab): |
| 12 | self.vocab = vocab |
| 13 | |
| 14 | def __call__(self, text): |
| 15 | words = text.split(' ') |
| 16 | # All tokens 'own' a subsequent space character in this tokenizer |
| 17 | spaces = [True] * len(words) |
| 18 | return Doc(self.vocab, words=words, spaces=spaces) |
| 19 | |
| 20 | |
daza | 8534747 | 2020-11-23 18:43:33 +0100 | [diff] [blame] | 21 | def get_conll_str(anno_obj, spacy_doc, use_germalemma): |
| 22 | # First lines are comments. (metadata) |
| 23 | conll_lines = anno_obj.metadata # Then we want: [ID, FORM, LEMMA, UPOS, XPOS, FEATS, HEAD, DEPREL, DEPS, MISC] |
daza | e3bc92e | 2020-11-04 11:06:26 +0100 | [diff] [blame] | 24 | for ix, token in enumerate(spacy_doc): |
| 25 | if use_germalemma == "True": |
| 26 | content = (str(ix), token.text, find_germalemma(token.text, token.tag_, token.lemma_), token.pos_, token.tag_, "_", "_", "_", "_", "_") |
| 27 | else: |
| 28 | content = (str(ix), token.text, token.lemma_, token.pos_, token.tag_, "_", "_", "_", "_", "_") # Pure SpaCy! |
| 29 | conll_lines.append("\t".join(content)) |
| 30 | return "\n".join(conll_lines) |
| 31 | |
daza | e3bc92e | 2020-11-04 11:06:26 +0100 | [diff] [blame] | 32 | |
| 33 | def find_germalemma(word, pos, spacy_lemma): |
| 34 | simplify_pos = {"ADJA":"ADJ", "ADJD":"ADJ", |
| 35 | "NA":"N", "NE":"N", "NN":"N", |
| 36 | "ADV":"ADV", "PAV":"ADV", "PROAV":"ADV", "PAVREL":"ADV", "PWAV":"ADV", "PWAVREL":"ADV", |
| 37 | "VAFIN":"V", "VAIMP":"V", "VAINF":"V", "VAPP":"V", "VMFIN":"V", "VMINF":"V", |
| 38 | "VMPP":"V", "VVFIN":"V", "VVIMP":"V", "VVINF":"V", "VVIZU":"V","VVPP":"V" |
| 39 | } |
| 40 | # simplify_pos = {"VERB": "V", "ADV": "ADV", "ADJ": "ADJ", "NOUN":"N", "PROPN": "N"} |
| 41 | try: |
| 42 | return lemmatizer.find_lemma(word, simplify_pos.get(pos, "UNK")) |
| 43 | except: |
| 44 | return spacy_lemma |
| 45 | |
| 46 | |
| 47 | if __name__ == "__main__": |
| 48 | """ |
| 49 | EXAMPLE: |
daza | 8534747 | 2020-11-23 18:43:33 +0100 | [diff] [blame] | 50 | --- TIGER Classic Orthography --- |
daza | d7d7075 | 2021-01-12 18:17:49 +0100 | [diff] [blame] | 51 | python systems/parse_spacy.py --corpus_name Tiger --gld_token_type CoNLL09_Token \ |
daza | e3bc92e | 2020-11-04 11:06:26 +0100 | [diff] [blame] | 52 | -i /home/daza/datasets/TIGER_conll/tiger_release_aug07.corrected.16012013.conll09 \ |
| 53 | -o /home/daza/datasets/TIGER_conll/tiger_spacy_parsed.conllu \ |
| 54 | -t /home/daza/datasets/TIGER_conll/tiger_all.txt |
daza | d7d7075 | 2021-01-12 18:17:49 +0100 | [diff] [blame] | 55 | |
| 56 | python systems/parse_spacy.py --corpus_name TigerOld_test \ |
| 57 | -i /home/daza/datasets/TIGER_conll/data_splits/test/Tiger.OldOrth.test.conll \ |
| 58 | -o /home/daza/datasets/TIGER_conll/tiger_spacy_parsed.test.conllu |
daza | 8534747 | 2020-11-23 18:43:33 +0100 | [diff] [blame] | 59 | |
| 60 | --- TIGER New Orthography --- |
daza | d7d7075 | 2021-01-12 18:17:49 +0100 | [diff] [blame] | 61 | python systems/parse_spacy.py --corpus_name TigerNew \ |
daza | 8534747 | 2020-11-23 18:43:33 +0100 | [diff] [blame] | 62 | -i /home/daza/datasets/TIGER_conll/Tiger.NewOrth.train.conll \ |
| 63 | -o /home/daza/datasets/TIGER_conll/Tiger.NewOrth.train.spacy_parsed.conllu \ |
| 64 | -t /home/daza/datasets/TIGER_conll/Tiger.NewOrth.train.txt |
daza | d7d7075 | 2021-01-12 18:17:49 +0100 | [diff] [blame] | 65 | |
| 66 | python systems/parse_spacy.py --corpus_name TigerNew_test \ |
| 67 | -i /home/daza/datasets/TIGER_conll/data_splits/test/Tiger.NewOrth.test.conll \ |
| 68 | -o /home/daza/datasets/TIGER_conll/Tiger.NewOrth.test.spacy_parsed.conllu |
daza | 8534747 | 2020-11-23 18:43:33 +0100 | [diff] [blame] | 69 | |
| 70 | --- German GSD Universal Deps --- |
daza | d7d7075 | 2021-01-12 18:17:49 +0100 | [diff] [blame] | 71 | python systems/parse_spacy.py --corpus_name DE_GSD \ |
daza | e3bc92e | 2020-11-04 11:06:26 +0100 | [diff] [blame] | 72 | -i /home/daza/datasets/ud-treebanks-v2.2/UD_German-GSD/de_gsd-ud-test.conllu \ |
| 73 | -o /home/daza/datasets/ud-treebanks-v2.2/UD_German-GSD/de_gsd-ud-test.parsed.germalemma.conllu \ |
daza | 8534747 | 2020-11-23 18:43:33 +0100 | [diff] [blame] | 74 | -t /home/daza/datasets/ud-treebanks-v2.2/UD_German-GSD/de_gsd-ud-test.txt |
| 75 | |
| 76 | |
| 77 | --- Real Data TEST --- |
daza | d7d7075 | 2021-01-12 18:17:49 +0100 | [diff] [blame] | 78 | time python systems/parse_spacy.py --corpus_name DeReKo_a00 --comment_str "#" \ |
daza | 8534747 | 2020-11-23 18:43:33 +0100 | [diff] [blame] | 79 | -i /export/netapp/kupietz/N-GRAMM-STUDIE/conllu/a00.conllu.gz \ |
| 80 | -o /export/netapp/kupietz/N-GRAMM-STUDIE/conllu/0_SpaCyParsed/a00.spacy.gl.conllu |
daza | e3bc92e | 2020-11-04 11:06:26 +0100 | [diff] [blame] | 81 | """ |
| 82 | |
| 83 | parser = argparse.ArgumentParser() |
| 84 | parser.add_argument("-i", "--input_file", help="Input Corpus", required=True) |
| 85 | parser.add_argument("-n", "--corpus_name", help="Corpus Name", default="Corpus") |
| 86 | parser.add_argument("-o", "--output_file", help="File where the Predictions will be saved", required=True) |
| 87 | parser.add_argument("-t", "--text_file", help="Output Plain Text File", default=None) |
daza | d7d7075 | 2021-01-12 18:17:49 +0100 | [diff] [blame] | 88 | parser.add_argument("-sm", "--spacy_model", help="Spacy model containing the pipeline to tag", default="de_core_news_lg") |
| 89 | parser.add_argument("-gtt", "--gld_token_type", help="CoNLL Format of the Gold Data", default="CoNLLUP_Token") |
daza | e3bc92e | 2020-11-04 11:06:26 +0100 | [diff] [blame] | 90 | parser.add_argument("-ugl", "--use_germalemma", help="Use Germalemma lemmatizer on top of SpaCy", default="True") |
| 91 | parser.add_argument("-c", "--comment_str", help="CoNLL Format of comentaries inside the file", default="#") |
| 92 | args = parser.parse_args() |
| 93 | |
| 94 | file_has_next, chunk_ix = True, 0 |
daza | d7d7075 | 2021-01-12 18:17:49 +0100 | [diff] [blame] | 95 | CHUNK_SIZE = 20000 |
| 96 | SPACY_BATCH = 2000 |
| 97 | SPACY_PROC = 10 |
daza | e3bc92e | 2020-11-04 11:06:26 +0100 | [diff] [blame] | 98 | |
| 99 | # ===================================================================================== |
| 100 | # LOGGING INFO ... |
| 101 | # ===================================================================================== |
| 102 | logger = logging.getLogger(__name__) |
| 103 | console_hdlr = logging.StreamHandler(sys.stdout) |
| 104 | file_hdlr = logging.FileHandler(filename=f"logs/Parse_{args.corpus_name}.SpaCy.log") |
| 105 | logging.basicConfig(level=logging.INFO, handlers=[console_hdlr, file_hdlr]) |
| 106 | logger.info(f"Chunking {args.corpus_name} Corpus in chunks of {CHUNK_SIZE} Sentences") |
| 107 | |
| 108 | # ===================================================================================== |
| 109 | # POS TAG DOCUMENTS |
| 110 | # ===================================================================================== |
daza | d7d7075 | 2021-01-12 18:17:49 +0100 | [diff] [blame] | 111 | spacy_de = spacy.load(args.spacy_model, disable=["ner", "parser"]) |
daza | e3bc92e | 2020-11-04 11:06:26 +0100 | [diff] [blame] | 112 | spacy_de.tokenizer = WhitespaceTokenizer(spacy_de.vocab) # We won't re-tokenize to respect how the source CoNLL are tokenized! |
| 113 | write_out = open(args.output_file, "w") |
| 114 | lemmatizer = GermaLemma() |
| 115 | if args.text_file: write_plain = open(args.text_file, "w") |
| 116 | |
daza | 8534747 | 2020-11-23 18:43:33 +0100 | [diff] [blame] | 117 | if ".gz" == args.input_file[-3:]: |
| 118 | in_file = fu.expand_file(args.input_file) |
| 119 | else: |
| 120 | in_file = args.input_file |
| 121 | |
daza | e3bc92e | 2020-11-04 11:06:26 +0100 | [diff] [blame] | 122 | start = time.time() |
| 123 | total_processed_sents = 0 |
daza | 8534747 | 2020-11-23 18:43:33 +0100 | [diff] [blame] | 124 | line_generator = fu.file_generator(in_file) |
daza | e3bc92e | 2020-11-04 11:06:26 +0100 | [diff] [blame] | 125 | while file_has_next: |
daza | 8534747 | 2020-11-23 18:43:33 +0100 | [diff] [blame] | 126 | annos, file_has_next = fu.get_file_annos_chunk(line_generator, chunk_size=CHUNK_SIZE, token_class=get_token_type(args.gld_token_type), comment_str=args.comment_str) |
| 127 | if len(annos) == 0: break |
| 128 | total_processed_sents += len(annos) |
daza | e3bc92e | 2020-11-04 11:06:26 +0100 | [diff] [blame] | 129 | logger.info(f"Already processed {total_processed_sents} sentences...") |
daza | 8534747 | 2020-11-23 18:43:33 +0100 | [diff] [blame] | 130 | sents = [a.get_sentence() for a in annos] |
| 131 | for ix, doc in enumerate(spacy_de.pipe(sents, batch_size=SPACY_BATCH, n_process=SPACY_PROC)): |
| 132 | conll_str = get_conll_str(annos[ix], doc, use_germalemma=args.use_germalemma) |
daza | e3bc92e | 2020-11-04 11:06:26 +0100 | [diff] [blame] | 133 | write_out.write(conll_str) |
| 134 | write_out.write("\n\n") |
| 135 | if args.text_file: |
| 136 | write_plain.write(" ".join([x.text for x in doc])+"\n") |
| 137 | |
| 138 | end = time.time() |
| 139 | logger.info(f"Processing {args.corpus_name} took {(end - start)} seconds!") |
| 140 | |