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 | |
| 32 | |
| 33 | # def freeling_lemma_lookup(): |
| 34 | # dicts_path = "/home/daza/Frameworks/FreeLing/data/de/dictionary/entries/" |
| 35 | |
| 36 | def find_germalemma(word, pos, spacy_lemma): |
| 37 | simplify_pos = {"ADJA":"ADJ", "ADJD":"ADJ", |
| 38 | "NA":"N", "NE":"N", "NN":"N", |
| 39 | "ADV":"ADV", "PAV":"ADV", "PROAV":"ADV", "PAVREL":"ADV", "PWAV":"ADV", "PWAVREL":"ADV", |
| 40 | "VAFIN":"V", "VAIMP":"V", "VAINF":"V", "VAPP":"V", "VMFIN":"V", "VMINF":"V", |
| 41 | "VMPP":"V", "VVFIN":"V", "VVIMP":"V", "VVINF":"V", "VVIZU":"V","VVPP":"V" |
| 42 | } |
| 43 | # simplify_pos = {"VERB": "V", "ADV": "ADV", "ADJ": "ADJ", "NOUN":"N", "PROPN": "N"} |
| 44 | try: |
| 45 | return lemmatizer.find_lemma(word, simplify_pos.get(pos, "UNK")) |
| 46 | except: |
| 47 | return spacy_lemma |
| 48 | |
| 49 | |
| 50 | if __name__ == "__main__": |
| 51 | """ |
| 52 | EXAMPLE: |
daza | 8534747 | 2020-11-23 18:43:33 +0100 | [diff] [blame] | 53 | --- TIGER Classic Orthography --- |
daza | d7d7075 | 2021-01-12 18:17:49 +0100 | [diff] [blame^] | 54 | python systems/parse_spacy.py --corpus_name Tiger --gld_token_type CoNLL09_Token \ |
daza | e3bc92e | 2020-11-04 11:06:26 +0100 | [diff] [blame] | 55 | -i /home/daza/datasets/TIGER_conll/tiger_release_aug07.corrected.16012013.conll09 \ |
| 56 | -o /home/daza/datasets/TIGER_conll/tiger_spacy_parsed.conllu \ |
| 57 | -t /home/daza/datasets/TIGER_conll/tiger_all.txt |
daza | d7d7075 | 2021-01-12 18:17:49 +0100 | [diff] [blame^] | 58 | |
| 59 | python systems/parse_spacy.py --corpus_name TigerOld_test \ |
| 60 | -i /home/daza/datasets/TIGER_conll/data_splits/test/Tiger.OldOrth.test.conll \ |
| 61 | -o /home/daza/datasets/TIGER_conll/tiger_spacy_parsed.test.conllu |
daza | 8534747 | 2020-11-23 18:43:33 +0100 | [diff] [blame] | 62 | |
| 63 | --- TIGER New Orthography --- |
daza | d7d7075 | 2021-01-12 18:17:49 +0100 | [diff] [blame^] | 64 | python systems/parse_spacy.py --corpus_name TigerNew \ |
daza | 8534747 | 2020-11-23 18:43:33 +0100 | [diff] [blame] | 65 | -i /home/daza/datasets/TIGER_conll/Tiger.NewOrth.train.conll \ |
| 66 | -o /home/daza/datasets/TIGER_conll/Tiger.NewOrth.train.spacy_parsed.conllu \ |
| 67 | -t /home/daza/datasets/TIGER_conll/Tiger.NewOrth.train.txt |
daza | d7d7075 | 2021-01-12 18:17:49 +0100 | [diff] [blame^] | 68 | |
| 69 | python systems/parse_spacy.py --corpus_name TigerNew_test \ |
| 70 | -i /home/daza/datasets/TIGER_conll/data_splits/test/Tiger.NewOrth.test.conll \ |
| 71 | -o /home/daza/datasets/TIGER_conll/Tiger.NewOrth.test.spacy_parsed.conllu |
daza | 8534747 | 2020-11-23 18:43:33 +0100 | [diff] [blame] | 72 | |
| 73 | --- German GSD Universal Deps --- |
daza | d7d7075 | 2021-01-12 18:17:49 +0100 | [diff] [blame^] | 74 | python systems/parse_spacy.py --corpus_name DE_GSD \ |
daza | e3bc92e | 2020-11-04 11:06:26 +0100 | [diff] [blame] | 75 | -i /home/daza/datasets/ud-treebanks-v2.2/UD_German-GSD/de_gsd-ud-test.conllu \ |
| 76 | -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] | 77 | -t /home/daza/datasets/ud-treebanks-v2.2/UD_German-GSD/de_gsd-ud-test.txt |
| 78 | |
| 79 | |
| 80 | --- Real Data TEST --- |
daza | d7d7075 | 2021-01-12 18:17:49 +0100 | [diff] [blame^] | 81 | time python systems/parse_spacy.py --corpus_name DeReKo_a00 --comment_str "#" \ |
daza | 8534747 | 2020-11-23 18:43:33 +0100 | [diff] [blame] | 82 | -i /export/netapp/kupietz/N-GRAMM-STUDIE/conllu/a00.conllu.gz \ |
| 83 | -o /export/netapp/kupietz/N-GRAMM-STUDIE/conllu/0_SpaCyParsed/a00.spacy.gl.conllu |
| 84 | |
daza | e3bc92e | 2020-11-04 11:06:26 +0100 | [diff] [blame] | 85 | """ |
| 86 | |
| 87 | parser = argparse.ArgumentParser() |
| 88 | parser.add_argument("-i", "--input_file", help="Input Corpus", required=True) |
| 89 | parser.add_argument("-n", "--corpus_name", help="Corpus Name", default="Corpus") |
| 90 | parser.add_argument("-o", "--output_file", help="File where the Predictions will be saved", required=True) |
| 91 | parser.add_argument("-t", "--text_file", help="Output Plain Text File", default=None) |
daza | d7d7075 | 2021-01-12 18:17:49 +0100 | [diff] [blame^] | 92 | parser.add_argument("-sm", "--spacy_model", help="Spacy model containing the pipeline to tag", default="de_core_news_lg") |
| 93 | 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] | 94 | parser.add_argument("-ugl", "--use_germalemma", help="Use Germalemma lemmatizer on top of SpaCy", default="True") |
| 95 | parser.add_argument("-c", "--comment_str", help="CoNLL Format of comentaries inside the file", default="#") |
| 96 | args = parser.parse_args() |
| 97 | |
| 98 | file_has_next, chunk_ix = True, 0 |
daza | d7d7075 | 2021-01-12 18:17:49 +0100 | [diff] [blame^] | 99 | CHUNK_SIZE = 20000 |
| 100 | SPACY_BATCH = 2000 |
| 101 | SPACY_PROC = 10 |
daza | e3bc92e | 2020-11-04 11:06:26 +0100 | [diff] [blame] | 102 | |
| 103 | # ===================================================================================== |
| 104 | # LOGGING INFO ... |
| 105 | # ===================================================================================== |
| 106 | logger = logging.getLogger(__name__) |
| 107 | console_hdlr = logging.StreamHandler(sys.stdout) |
| 108 | file_hdlr = logging.FileHandler(filename=f"logs/Parse_{args.corpus_name}.SpaCy.log") |
| 109 | logging.basicConfig(level=logging.INFO, handlers=[console_hdlr, file_hdlr]) |
| 110 | logger.info(f"Chunking {args.corpus_name} Corpus in chunks of {CHUNK_SIZE} Sentences") |
| 111 | |
| 112 | # ===================================================================================== |
| 113 | # POS TAG DOCUMENTS |
| 114 | # ===================================================================================== |
daza | d7d7075 | 2021-01-12 18:17:49 +0100 | [diff] [blame^] | 115 | spacy_de = spacy.load(args.spacy_model, disable=["ner", "parser"]) |
daza | e3bc92e | 2020-11-04 11:06:26 +0100 | [diff] [blame] | 116 | spacy_de.tokenizer = WhitespaceTokenizer(spacy_de.vocab) # We won't re-tokenize to respect how the source CoNLL are tokenized! |
| 117 | write_out = open(args.output_file, "w") |
| 118 | lemmatizer = GermaLemma() |
| 119 | if args.text_file: write_plain = open(args.text_file, "w") |
| 120 | |
daza | 8534747 | 2020-11-23 18:43:33 +0100 | [diff] [blame] | 121 | if ".gz" == args.input_file[-3:]: |
| 122 | in_file = fu.expand_file(args.input_file) |
| 123 | else: |
| 124 | in_file = args.input_file |
| 125 | |
daza | e3bc92e | 2020-11-04 11:06:26 +0100 | [diff] [blame] | 126 | start = time.time() |
| 127 | total_processed_sents = 0 |
daza | 8534747 | 2020-11-23 18:43:33 +0100 | [diff] [blame] | 128 | line_generator = fu.file_generator(in_file) |
daza | e3bc92e | 2020-11-04 11:06:26 +0100 | [diff] [blame] | 129 | while file_has_next: |
daza | 8534747 | 2020-11-23 18:43:33 +0100 | [diff] [blame] | 130 | 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) |
| 131 | if len(annos) == 0: break |
| 132 | total_processed_sents += len(annos) |
daza | e3bc92e | 2020-11-04 11:06:26 +0100 | [diff] [blame] | 133 | logger.info(f"Already processed {total_processed_sents} sentences...") |
daza | 8534747 | 2020-11-23 18:43:33 +0100 | [diff] [blame] | 134 | sents = [a.get_sentence() for a in annos] |
| 135 | for ix, doc in enumerate(spacy_de.pipe(sents, batch_size=SPACY_BATCH, n_process=SPACY_PROC)): |
| 136 | conll_str = get_conll_str(annos[ix], doc, use_germalemma=args.use_germalemma) |
daza | e3bc92e | 2020-11-04 11:06:26 +0100 | [diff] [blame] | 137 | write_out.write(conll_str) |
| 138 | write_out.write("\n\n") |
| 139 | if args.text_file: |
| 140 | write_plain.write(" ".join([x.text for x in doc])+"\n") |
| 141 | |
| 142 | end = time.time() |
| 143 | logger.info(f"Processing {args.corpus_name} took {(end - start)} seconds!") |
| 144 | |