blob: b532456fa886cbbf87b0e0553df1e5280ff81134 [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
Marc Kupietz6b848db2025-10-30 11:38:59 +010011DEFAULT_PARSE_TIMEOUT = 0.5 # seconds per sentence
Marc Kupietz095185b2025-10-27 14:41:43 +010012DEFAULT_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)
Marc Kupietz6b848db2025-10-30 11:38:59 +010042 signal.setitimer(signal.ITIMER_REAL, timeout)
Marc Kupietz095185b2025-10-27 14:41:43 +010043
44 try:
45 doc = spacy_model(text)
Marc Kupietz6b848db2025-10-30 11:38:59 +010046 signal.setitimer(signal.ITIMER_REAL, 0) # Cancel alarm
Marc Kupietz095185b2025-10-27 14:41:43 +010047 signal.signal(signal.SIGALRM, old_handler)
48 return doc, True, None
49 except TimeoutException:
Marc Kupietz6b848db2025-10-30 11:38:59 +010050 signal.setitimer(signal.ITIMER_REAL, 0) # Cancel alarm
Marc Kupietz095185b2025-10-27 14:41:43 +010051 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:
Marc Kupietz6b848db2025-10-30 11:38:59 +010057 signal.setitimer(signal.ITIMER_REAL, 0) # Cancel alarm
Marc Kupietz095185b2025-10-27 14:41:43 +010058 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")
Marc Kupietz82b64772025-10-30 10:05:56 +0100200
201 # Custom format without module name
202 formatter = logging.Formatter('%(levelname)s: %(message)s')
203 console_hdlr.setFormatter(formatter)
204 file_hdlr.setFormatter(formatter)
205
daza48606ba2021-02-10 14:16:41 +0100206 logging.basicConfig(level=logging.INFO, handlers=[console_hdlr, file_hdlr])
Marc Kupietz0ce98a62025-10-26 15:59:27 +0100207
208 # Override with environment variables if set (useful for Docker)
209 import os
210 if os.getenv("SPACY_USE_DEPENDENCIES") is not None:
211 args.use_dependencies = os.getenv("SPACY_USE_DEPENDENCIES", "True")
212 logger.info(f"Using SPACY_USE_DEPENDENCIES environment variable: {args.use_dependencies}")
213
214 if os.getenv("SPACY_USE_GERMALEMMA") is not None:
215 args.use_germalemma = os.getenv("SPACY_USE_GERMALEMMA", "True")
216 logger.info(f"Using SPACY_USE_GERMALEMMA environment variable: {args.use_germalemma}")
217
daza48606ba2021-02-10 14:16:41 +0100218 logger.info(f"Chunking {args.corpus_name} Corpus in chunks of {CHUNK_SIZE} Sentences")
Marc Kupietz45e74df2025-10-29 18:56:08 +0100219 logger.info(f"Processing configuration: batch_size={SPACY_BATCH}, n_process={SPACY_PROC}")
daza48606ba2021-02-10 14:16:41 +0100220
221 # =====================================================================================
222 # POS TAG DOCUMENTS
223 # =====================================================================================
Marc Kupietz0ce98a62025-10-26 15:59:27 +0100224 # Configure which components to disable based on dependency parsing option
225 disabled_components = ["ner"]
226 if args.use_dependencies != "True":
227 disabled_components.append("parser")
228 logger.info("Dependency parsing disabled for faster processing")
229 else:
230 logger.info("Dependency parsing enabled (slower but includes HEAD/DEPREL)")
231
232 spacy_de = spacy.load(args.spacy_model, disable=disabled_components)
daza48606ba2021-02-10 14:16:41 +0100233 spacy_de.tokenizer = WhitespaceTokenizer(spacy_de.vocab) # We won't re-tokenize to respect how the source CoNLL are tokenized!
234 lemmatizer = GermaLemma()
235
Marc Kupietzf629a402025-10-26 21:54:33 +0100236 # Log version information
237 logger.info(f"spaCy version: {spacy.__version__}")
238 logger.info(f"spaCy model: {args.spacy_model}")
239 logger.info(f"spaCy model version: {spacy_de.meta.get('version', 'unknown')}")
240 try:
241 import germalemma
242 logger.info(f"GermaLemma version: {germalemma.__version__}")
243 except AttributeError:
244 logger.info("GermaLemma version: unknown (no __version__ attribute)")
245
Marc Kupietz095185b2025-10-27 14:41:43 +0100246 # Parse timeout and sentence length limits from environment variables
Marc Kupietz6b848db2025-10-30 11:38:59 +0100247 parse_timeout = float(os.getenv("SPACY_PARSE_TIMEOUT", str(DEFAULT_PARSE_TIMEOUT)))
248 max_sentence_length = int(os.getenv("SPACY_MAX_SENTENCE_LENGTH", str(DEFAULT_MAX_SENTENCE_LENGTH)))
Marc Kupietz095185b2025-10-27 14:41:43 +0100249
250 logger.info(f"Dependency parsing limits: timeout={parse_timeout}s, max_length={max_sentence_length} tokens")
251
daza48606ba2021-02-10 14:16:41 +0100252 start = time.time()
253 total_processed_sents = 0
Marc Kupietz095185b2025-10-27 14:41:43 +0100254 dependency_warnings = 0
daza48606ba2021-02-10 14:16:41 +0100255
256 while file_has_next:
Marc Kupietza01314f2021-02-11 17:02:08 +0100257 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 +0100258 if len(annos) == 0: break
259 total_processed_sents += len(annos)
Marc Kupietz82b64772025-10-30 10:05:56 +0100260
261 # Calculate progress statistics
262 elapsed_time = time.time() - start
263 sents_per_sec = total_processed_sents / elapsed_time if elapsed_time > 0 else 0
264 current_time = time.strftime("%Y-%m-%d %H:%M:%S")
265
266 logger.info(f"{current_time} | Processed: {total_processed_sents} sentences | Elapsed: {elapsed_time:.1f}s | Speed: {sents_per_sec:.1f} sents/sec")
267
daza48606ba2021-02-10 14:16:41 +0100268 sents = [a.get_sentence() for a in annos]
Marc Kupietz095185b2025-10-27 14:41:43 +0100269
270 # Process sentences individually when dependency parsing is enabled for timeout protection
271 if args.use_dependencies == "True":
272 for ix, sent in enumerate(sents):
273 doc, dependency_success, warning = safe_dependency_parse(
274 spacy_de, sent, timeout=parse_timeout, max_length=max_sentence_length
275 )
276 if warning:
277 dependency_warnings += 1
278 logger.warning(f"Sentence {total_processed_sents - len(sents) + ix + 1}: {warning}")
279
280 # Override use_dependencies based on actual parsing success
Marc Kupietz9158a162025-10-28 09:08:12 +0100281 actual_use_dependencies = "True" if dependency_success else "False"
Marc Kupietz095185b2025-10-27 14:41:43 +0100282 conll_str = get_conll_str(annos[ix], doc, use_germalemma=args.use_germalemma, use_dependencies=actual_use_dependencies)
283 print(conll_str+ "\n")
284 else:
285 # Use batch processing for faster processing when dependencies are disabled
286 for ix, doc in enumerate(spacy_de.pipe(sents, batch_size=SPACY_BATCH, n_process=SPACY_PROC)):
287 conll_str = get_conll_str(annos[ix], doc, use_germalemma=args.use_germalemma, use_dependencies=args.use_dependencies)
288 print(conll_str+ "\n")
daza48606ba2021-02-10 14:16:41 +0100289
290 end = time.time()
Marc Kupietz82b64772025-10-30 10:05:56 +0100291 total_time = end - start
292 final_sents_per_sec = total_processed_sents / total_time if total_time > 0 else 0
293
294 logger.info(f"=== Processing Complete ===")
295 logger.info(f"Total sentences: {total_processed_sents}")
296 logger.info(f"Total time: {total_time:.2f}s")
297 logger.info(f"Average speed: {final_sents_per_sec:.1f} sents/sec")
298
Marc Kupietz095185b2025-10-27 14:41:43 +0100299 if dependency_warnings > 0:
300 logger.info(f"Dependency parsing warnings: {dependency_warnings} sentences processed without dependencies")
daza48606ba2021-02-10 14:16:41 +0100301