blob: 402d2d6ca80d08acd10d95742906b7ff0f32af68 [file] [log] [blame]
daza48606ba2021-02-10 14:16:41 +01001from sys import stdin
Marc Kupietz095185b2025-10-27 14:41:43 +01002import argparse, os
daza48606ba2021-02-10 14:16:41 +01003import spacy
4from spacy.tokens import Doc
Marc Kupietz095185b2025-10-27 14:41:43 +01005import logging, sys, time, signal
daza48606ba2021-02-10 14:16:41 +01006from lib.CoNLL_Annotation import get_token_type
7import my_utils.file_utils as fu
8from germalemma import GermaLemma
9
Marc Kupietz095185b2025-10-27 14:41:43 +010010# Dependency parsing safety limits
11DEFAULT_PARSE_TIMEOUT = 30 # seconds per sentence
12DEFAULT_MAX_SENTENCE_LENGTH = 500 # tokens
13
14class TimeoutException(Exception):
15 pass
16
17def timeout_handler(signum, frame):
18 raise TimeoutException("Dependency parsing timeout")
19
20def safe_dependency_parse(spacy_model, text, timeout=DEFAULT_PARSE_TIMEOUT, max_length=DEFAULT_MAX_SENTENCE_LENGTH):
21 """
22 Safely parse a sentence with timeout and length limits.
23
24 Args:
25 spacy_model: Loaded spaCy model
26 text: Text to parse
27 timeout: Maximum seconds to wait for parsing
28 max_length: Maximum sentence length in tokens
29
30 Returns:
31 tuple: (spacy_doc, success, warning_message)
32 """
33 # Check sentence length
34 if len(text.split()) > max_length:
35 # Process without dependency parsing for long sentences
36 disabled_components = ["ner", "parser"]
37 doc = spacy_model(text, disable=disabled_components)
38 return doc, False, f"Sentence too long ({len(text.split())} tokens > {max_length}), dependency parsing skipped"
39
40 # Set up timeout
41 old_handler = signal.signal(signal.SIGALRM, timeout_handler)
42 signal.alarm(timeout)
43
44 try:
45 doc = spacy_model(text)
46 signal.alarm(0) # Cancel alarm
47 signal.signal(signal.SIGALRM, old_handler)
48 return doc, True, None
49 except TimeoutException:
50 signal.alarm(0) # Cancel alarm
51 signal.signal(signal.SIGALRM, old_handler)
52 # Retry without dependency parsing
53 disabled_components = ["ner", "parser"]
54 doc = spacy_model(text, disable=disabled_components)
55 return doc, False, f"Dependency parsing timeout after {timeout}s, processed without dependencies"
56 except Exception as e:
57 signal.alarm(0) # Cancel alarm
58 signal.signal(signal.SIGALRM, old_handler)
59 # Retry without dependency parsing
60 disabled_components = ["ner", "parser"]
61 doc = spacy_model(text, disable=disabled_components)
62 return doc, False, f"Dependency parsing error: {str(e)}, processed without dependencies"
63
Marc Kupietz88eea722025-10-26 15:21:14 +010064def format_morphological_features(token):
65 """
66 Extract and format morphological features from a spaCy token for CoNLL-U output.
67
68 Args:
69 token: spaCy token object
70
71 Returns:
72 str: Formatted morphological features string for CoNLL-U 5th column
73 Returns "_" if no features are available
74 """
75 if not hasattr(token, 'morph') or not token.morph:
76 return "_"
77
78 morph_dict = token.morph.to_dict()
79 if not morph_dict:
80 return "_"
81
82 # Format as CoNLL-U format: Feature=Value|Feature2=Value2
83 features = []
84 for feature, value in sorted(morph_dict.items()):
85 features.append(f"{feature}={value}")
86
87 return "|".join(features)
88
daza48606ba2021-02-10 14:16:41 +010089
Marc Kupietz0ce98a62025-10-26 15:59:27 +010090def format_dependency_relations(doc):
91 """
92 Extract and format dependency relations from a spaCy doc for CoNLL-U output.
93
94 Args:
95 doc: spaCy Doc object
96
97 Returns:
98 list: List of tuples (head_id, deprel) for each token
99 """
100 dependencies = []
101 for i, token in enumerate(doc):
102 # HEAD column: 1-based index of the head token (0 for root)
103 if token.dep_ == "ROOT":
104 head_id = 0
105 else:
106 # Find the 1-based index of the head token
107 head_id = None
108 for j, potential_head in enumerate(doc):
109 if potential_head == token.head:
110 head_id = j + 1
111 break
112 if head_id is None:
113 head_id = 0 # Fallback to root if head not found
114
115 # DEPREL column: dependency relation
116 deprel = token.dep_ if token.dep_ else "_"
117
118 dependencies.append((head_id, deprel))
119
120 return dependencies
121
122
daza48606ba2021-02-10 14:16:41 +0100123class WhitespaceTokenizer(object):
124 def __init__(self, vocab):
125 self.vocab = vocab
126
127 def __call__(self, text):
128 words = text.split(' ')
129 # All tokens 'own' a subsequent space character in this tokenizer
130 spaces = [True] * len(words)
131 return Doc(self.vocab, words=words, spaces=spaces)
132
133
Marc Kupietz0ce98a62025-10-26 15:59:27 +0100134def get_conll_str(anno_obj, spacy_doc, use_germalemma, use_dependencies):
daza48606ba2021-02-10 14:16:41 +0100135 # First lines are comments. (metadata)
136 conll_lines = anno_obj.metadata # Then we want: [ID, FORM, LEMMA, UPOS, XPOS, FEATS, HEAD, DEPREL, DEPS, MISC]
Marc Kupietz0ce98a62025-10-26 15:59:27 +0100137
138 # Get dependency relations if enabled
139 dependencies = format_dependency_relations(spacy_doc) if use_dependencies == "True" else None
140
daza48606ba2021-02-10 14:16:41 +0100141 for ix, token in enumerate(spacy_doc):
Marc Kupietz88eea722025-10-26 15:21:14 +0100142 morph_features = format_morphological_features(token)
Marc Kupietz0ce98a62025-10-26 15:59:27 +0100143
144 # Get HEAD and DEPREL columns
145 if dependencies:
146 head_id, deprel = dependencies[ix]
daza48606ba2021-02-10 14:16:41 +0100147 else:
Marc Kupietz0ce98a62025-10-26 15:59:27 +0100148 head_id, deprel = "_", "_"
149
150 if use_germalemma == "True":
151 content = (str(ix+1), token.text, find_germalemma(token.text, token.tag_, token.lemma_), token.pos_, token.tag_, morph_features, str(head_id), deprel, "_", "_")
152 else:
153 content = (str(ix+1), token.text, token.lemma_, token.pos_, token.tag_, morph_features, str(head_id), deprel, "_", "_") # Pure SpaCy!
daza48606ba2021-02-10 14:16:41 +0100154 conll_lines.append("\t".join(content))
155 return "\n".join(conll_lines)
156
157
158def find_germalemma(word, pos, spacy_lemma):
159 simplify_pos = {"ADJA":"ADJ", "ADJD":"ADJ",
160 "NA":"N", "NE":"N", "NN":"N",
161 "ADV":"ADV", "PAV":"ADV", "PROAV":"ADV", "PAVREL":"ADV", "PWAV":"ADV", "PWAVREL":"ADV",
162 "VAFIN":"V", "VAIMP":"V", "VAINF":"V", "VAPP":"V", "VMFIN":"V", "VMINF":"V",
163 "VMPP":"V", "VVFIN":"V", "VVIMP":"V", "VVINF":"V", "VVIZU":"V","VVPP":"V"
164 }
165 # simplify_pos = {"VERB": "V", "ADV": "ADV", "ADJ": "ADJ", "NOUN":"N", "PROPN": "N"}
166 try:
167 return lemmatizer.find_lemma(word, simplify_pos.get(pos, "UNK"))
168 except:
169 return spacy_lemma
170
171
172if __name__ == "__main__":
173 """
174 --- Example Real Data TEST ---
175
176 cat /export/netapp/kupietz/N-GRAMM-STUDIE/conllu/zca18.conllu | python systems/parse_spacy_pipe.py \
177 --corpus_name DeReKo_zca18 --comment_str "#" > output_zca18.conll
178 """
179
180 parser = argparse.ArgumentParser()
181 parser.add_argument("-n", "--corpus_name", help="Corpus Name", default="Corpus")
182 parser.add_argument("-sm", "--spacy_model", help="Spacy model containing the pipeline to tag", default="de_core_news_lg")
183 parser.add_argument("-gtt", "--gld_token_type", help="CoNLL Format of the Gold Data", default="CoNLLUP_Token")
184 parser.add_argument("-ugl", "--use_germalemma", help="Use Germalemma lemmatizer on top of SpaCy", default="True")
Marc Kupietz0ce98a62025-10-26 15:59:27 +0100185 parser.add_argument("-udp", "--use_dependencies", help="Include dependency parsing (adds HEAD/DEPREL columns, set to False for faster processing)", default="True")
daza48606ba2021-02-10 14:16:41 +0100186 parser.add_argument("-c", "--comment_str", help="CoNLL Format of comentaries inside the file", default="#")
187 args = parser.parse_args()
188
189 file_has_next, chunk_ix = True, 0
Marc Kupietz45e74df2025-10-29 18:56:08 +0100190 CHUNK_SIZE = int(os.getenv("SPACY_CHUNK_SIZE", "20000"))
191 SPACY_BATCH = int(os.getenv("SPACY_BATCH_SIZE", "2000"))
192 SPACY_PROC = int(os.getenv("SPACY_N_PROCESS", "10"))
daza48606ba2021-02-10 14:16:41 +0100193
194 # =====================================================================================
195 # LOGGING INFO ...
196 # =====================================================================================
197 logger = logging.getLogger(__name__)
198 console_hdlr = logging.StreamHandler(sys.stderr)
199 file_hdlr = logging.FileHandler(filename=f"logs/Parse_{args.corpus_name}.SpaCy.log")
200 logging.basicConfig(level=logging.INFO, handlers=[console_hdlr, file_hdlr])
Marc Kupietz0ce98a62025-10-26 15:59:27 +0100201
202 # Override with environment variables if set (useful for Docker)
203 import os
204 if os.getenv("SPACY_USE_DEPENDENCIES") is not None:
205 args.use_dependencies = os.getenv("SPACY_USE_DEPENDENCIES", "True")
206 logger.info(f"Using SPACY_USE_DEPENDENCIES environment variable: {args.use_dependencies}")
207
208 if os.getenv("SPACY_USE_GERMALEMMA") is not None:
209 args.use_germalemma = os.getenv("SPACY_USE_GERMALEMMA", "True")
210 logger.info(f"Using SPACY_USE_GERMALEMMA environment variable: {args.use_germalemma}")
211
daza48606ba2021-02-10 14:16:41 +0100212 logger.info(f"Chunking {args.corpus_name} Corpus in chunks of {CHUNK_SIZE} Sentences")
Marc Kupietz45e74df2025-10-29 18:56:08 +0100213 logger.info(f"Processing configuration: batch_size={SPACY_BATCH}, n_process={SPACY_PROC}")
daza48606ba2021-02-10 14:16:41 +0100214
215 # =====================================================================================
216 # POS TAG DOCUMENTS
217 # =====================================================================================
Marc Kupietz0ce98a62025-10-26 15:59:27 +0100218 # Configure which components to disable based on dependency parsing option
219 disabled_components = ["ner"]
220 if args.use_dependencies != "True":
221 disabled_components.append("parser")
222 logger.info("Dependency parsing disabled for faster processing")
223 else:
224 logger.info("Dependency parsing enabled (slower but includes HEAD/DEPREL)")
225
226 spacy_de = spacy.load(args.spacy_model, disable=disabled_components)
daza48606ba2021-02-10 14:16:41 +0100227 spacy_de.tokenizer = WhitespaceTokenizer(spacy_de.vocab) # We won't re-tokenize to respect how the source CoNLL are tokenized!
228 lemmatizer = GermaLemma()
229
Marc Kupietzf629a402025-10-26 21:54:33 +0100230 # Log version information
231 logger.info(f"spaCy version: {spacy.__version__}")
232 logger.info(f"spaCy model: {args.spacy_model}")
233 logger.info(f"spaCy model version: {spacy_de.meta.get('version', 'unknown')}")
234 try:
235 import germalemma
236 logger.info(f"GermaLemma version: {germalemma.__version__}")
237 except AttributeError:
238 logger.info("GermaLemma version: unknown (no __version__ attribute)")
239
Marc Kupietz095185b2025-10-27 14:41:43 +0100240 # Parse timeout and sentence length limits from environment variables
241 parse_timeout = int(os.getenv("SPACY_PARSE_TIMEOUT", DEFAULT_PARSE_TIMEOUT))
242 max_sentence_length = int(os.getenv("SPACY_MAX_SENTENCE_LENGTH", DEFAULT_MAX_SENTENCE_LENGTH))
243
244 logger.info(f"Dependency parsing limits: timeout={parse_timeout}s, max_length={max_sentence_length} tokens")
245
daza48606ba2021-02-10 14:16:41 +0100246 start = time.time()
247 total_processed_sents = 0
Marc Kupietz095185b2025-10-27 14:41:43 +0100248 dependency_warnings = 0
daza48606ba2021-02-10 14:16:41 +0100249
250 while file_has_next:
Marc Kupietza01314f2021-02-11 17:02:08 +0100251 annos, file_has_next = fu.get_file_annos_chunk(stdin, chunk_size=CHUNK_SIZE, token_class=get_token_type(args.gld_token_type), comment_str=args.comment_str, our_foundry="spacy")
daza48606ba2021-02-10 14:16:41 +0100252 if len(annos) == 0: break
253 total_processed_sents += len(annos)
254 logger.info(f"Already processed {total_processed_sents} sentences...")
255 sents = [a.get_sentence() for a in annos]
Marc Kupietz095185b2025-10-27 14:41:43 +0100256
257 # Process sentences individually when dependency parsing is enabled for timeout protection
258 if args.use_dependencies == "True":
259 for ix, sent in enumerate(sents):
260 doc, dependency_success, warning = safe_dependency_parse(
261 spacy_de, sent, timeout=parse_timeout, max_length=max_sentence_length
262 )
263 if warning:
264 dependency_warnings += 1
265 logger.warning(f"Sentence {total_processed_sents - len(sents) + ix + 1}: {warning}")
266
267 # Override use_dependencies based on actual parsing success
Marc Kupietz9158a162025-10-28 09:08:12 +0100268 actual_use_dependencies = "True" if dependency_success else "False"
Marc Kupietz095185b2025-10-27 14:41:43 +0100269 conll_str = get_conll_str(annos[ix], doc, use_germalemma=args.use_germalemma, use_dependencies=actual_use_dependencies)
270 print(conll_str+ "\n")
271 else:
272 # Use batch processing for faster processing when dependencies are disabled
273 for ix, doc in enumerate(spacy_de.pipe(sents, batch_size=SPACY_BATCH, n_process=SPACY_PROC)):
274 conll_str = get_conll_str(annos[ix], doc, use_germalemma=args.use_germalemma, use_dependencies=args.use_dependencies)
275 print(conll_str+ "\n")
daza48606ba2021-02-10 14:16:41 +0100276
277 end = time.time()
278 logger.info(f"Processing {args.corpus_name} took {(end - start)} seconds!")
Marc Kupietz095185b2025-10-27 14:41:43 +0100279 if dependency_warnings > 0:
280 logger.info(f"Dependency parsing warnings: {dependency_warnings} sentences processed without dependencies")
daza48606ba2021-02-10 14:16:41 +0100281