blob: 24881653e6d4e3c46773bb828871c5a75a3a951c [file] [log] [blame]
Marc Kupietz4292bc02026-03-07 16:12:52 +01001SHELL := /bin/bash
Marc Kupietz89a44072025-12-11 07:14:00 +01002SRC_DIR ?= I5
Marc Kupietze7807322026-06-05 08:55:41 +02003KORAP_PORT ?= 64543
Marc Kupietz89a44072025-12-11 07:14:00 +01004
Marc Kupietza6effde2026-08-03 16:04:02 +02005# Optional: path to a Kalamar-Instance-* checkout (e.g. ../Kalamar-Instance-IDS)
6# whose customizations (config, custom templates, plugins) should be mounted
7# into the kalamar container. The instance ships its own compose.instance.yaml
8# describing what to mount where, so this project needs no knowledge of it.
9# make korap KALAMAR_INSTANCE=../Kalamar-Instance-IDS
10KALAMAR_INSTANCE ?=
11KALAMAR_INSTANCE_DIR := $(abspath $(KALAMAR_INSTANCE))
12
Marc Kupietz34b17bb2026-08-03 17:40:03 +020013# Optional: override the kalamar container image (e.g. a locally built tag).
14# The instance's compose.instance.yaml already defaults to a renderer-capable
15# image; set this to test a specific build, e.g.
16# make korap KALAMAR_INSTANCE=../Kalamar-Instance-IDS KALAMAR_IMAGE=korap/kalamar:v0.66-alpha-large
17KALAMAR_IMAGE ?=
18
Marc Kupietz9adff672026-06-09 17:18:17 +020019# Discover all *.i5.xml files in SRC_DIR, excluding inlined tagged copies (*-TAG.i5.xml)
20I5_FILES := $(filter-out %-TAG.i5.xml,$(wildcard $(SRC_DIR)/*.i5.xml))
Marc Kupietz89a44072025-12-11 07:14:00 +010021BASENAMES := $(patsubst %.i5.xml,%,$(notdir $(I5_FILES)))
22
Marc Kupietz99ff2b02026-06-05 08:54:37 +020023# Standard TEI P5 support
24TEI_DIR ?= TEI
25TEI_FILES := $(wildcard $(TEI_DIR)/*.xml)
Marc Kupietz3b2f4402026-06-13 10:30:19 +020026# Same files as seen from inside the container (the dir is mounted at /input).
27TEI_INPUTS := $(patsubst $(TEI_DIR)/%,/input/%,$(TEI_FILES))
Marc Kupietz99ff2b02026-06-05 08:54:37 +020028TEI_ZIP_NAME ?= tei
29TEI_FLAGS ?= --auto-textsigle 'TEI/XYZ.00001' # --xmlid-to-textsigle '([A-Z]+)\.(.*)\.([0-9]+)\.*([0-9])@$$1/$$2/$$3$$4'
Marc Kupietz99ff2b02026-06-05 08:54:37 +020030
Marc Kupietz00db6b02026-06-11 11:46:23 +020031# If set to a foundry name (e.g. gingko), tei2korapxml will use inline annotations and tokens from the source XML
32USE_INLINE_ANNOTATIONS_AS ?=
33TEI2KORAPXML_FLAGS ?= $(if $(USE_INLINE_ANNOTATIONS_AS),--no-tokenizer --inline-tokens $(USE_INLINE_ANNOTATIONS_AS),-s -tk)
34
Marc Kupietz99ff2b02026-06-05 08:54:37 +020035ifneq ($(TEI_FILES),)
36BASENAMES += $(TEI_ZIP_NAME)
37endif
38
Marc Kupietz89a44072025-12-11 07:14:00 +010039BUILD_DIR = build
Marc Kupietz15b313d2025-12-11 12:20:56 +010040TARGET_DIR ?= ./target
Marc Kupietz89a44072025-12-11 07:14:00 +010041MAX_THREADS ?= 8 # $(shell nproc)
42MAKE ?= make -j $(shell nproc)
Marc Kupietz15b313d2025-12-11 12:20:56 +010043# KORAPXMLTOOL_HEAP ?= $(shell echo "$$(($(MAX_THREADS) * 2500))")
Marc Kupietz89a44072025-12-11 07:14:00 +010044KORAPXMLTOOL ?= ./bin/korapxmltool
Marc Kupietz15b313d2025-12-11 12:20:56 +010045KORAPXMLTOOL_MODELS_PATH ?= models
Marc Kupietz4292bc02026-03-07 16:12:52 +010046DOCKER_CPU_SHARES ?= # e.g. 512 for lower priority (default Docker value is 1024)
Marc Kupietz89a44072025-12-11 07:14:00 +010047
48.DELETE_ON_ERROR:
49
Marc Kupietz19fa4482026-06-07 15:03:56 +020050.PHONY: all clean test index korap check-src pre-krill krill meta
Marc Kupietz89a44072025-12-11 07:14:00 +010051
Marc Kupietz19fa4482026-06-07 15:03:56 +020052.PRECIOUS: $(BUILD_DIR)/%.zip $(BUILD_DIR)/%.tree_tagger.zip $(BUILD_DIR)/%.marmot-malt.zip $(BUILD_DIR)/%.spacy.zip $(BUILD_DIR)/%.corenlp.zip $(BUILD_DIR)/%.cmc.zip $(BUILD_DIR)/%.opennlp.zip $(BUILD_DIR)/%.meta.xml $(BUILD_DIR)/%.krill.tar %.i5.xml
Marc Kupietz89a44072025-12-11 07:14:00 +010053
Marc Kupietz15b313d2025-12-11 12:20:56 +010054all: check-src korap
Marc Kupietz89a44072025-12-11 07:14:00 +010055
Marc Kupietz15b313d2025-12-11 12:20:56 +010056index: check-src $(TARGET_DIR)/index
57
58check-src:
Marc Kupietz99ff2b02026-06-05 08:54:37 +020059 @if [ ! -d "$(SRC_DIR)" ] && [ ! -d "$(TEI_DIR)" ]; then \
60 echo "Error: Neither SRC_DIR '$(SRC_DIR)' nor TEI_DIR '$(TEI_DIR)' exists."; \
61 echo "Please place your .i5.xml files in '$(SRC_DIR)' or your TEI .xml files in '$(TEI_DIR)'."; \
Marc Kupietz15b313d2025-12-11 12:20:56 +010062 exit 1; \
63 fi
Marc Kupietz9adff672026-06-09 17:18:17 +020064 @if [ -z "$$(find "$(SRC_DIR)" -maxdepth 1 -name '*.i5.xml' ! -name '*-TAG.i5.xml' -print -quit 2>/dev/null)" ] && \
Marc Kupietz99ff2b02026-06-05 08:54:37 +020065 [ -z "$$(find "$(TEI_DIR)" -maxdepth 1 -name '*.xml' -print -quit 2>/dev/null)" ]; then \
66 echo "Error: No .i5.xml files found in '$(SRC_DIR)' and no .xml files found in '$(TEI_DIR)'."; \
Marc Kupietz15b313d2025-12-11 12:20:56 +010067 exit 1; \
68 fi
Marc Kupietz89a44072025-12-11 07:14:00 +010069
70$(BUILD_DIR)/%.zip: $(SRC_DIR)/%.i5.xml
71 mkdir -p $(BUILD_DIR)
Marc Kupietz00db6b02026-06-11 11:46:23 +020072 docker run --rm -i $(if $(DOCKER_CPU_SHARES),--cpu-shares $(DOCKER_CPU_SHARES)) korap/tei2korapxml:latest -l warn $(TEI2KORAPXML_FLAGS) - < $< > $@ 2> >(tee $(@:.zip=.log) >&2)
73# docker run --rm $(if $(DOCKER_CPU_SHARES),--cpu-shares $(DOCKER_CPU_SHARES)) -v $(abspath $<):/input.i5.xml:ro korap/tei2korapxml:latest --progress -l warn $(TEI2KORAPXML_FLAGS) /input.i5.xml > $@ 2> >(tee $(@:.zip=.log) >&2)
Marc Kupietz4292bc02026-03-07 16:12:52 +010074 printf "%s\t%s\n" "$$(grep -c '<idsText ' $<)" "$$(unzip -l $@ | grep data.xml | wc -l)"
Marc Kupietz89a44072025-12-11 07:14:00 +010075
Marc Kupietz99ff2b02026-06-05 08:54:37 +020076$(BUILD_DIR)/$(TEI_ZIP_NAME).zip: $(TEI_FILES)
77 mkdir -p $(BUILD_DIR)
Marc Kupietz3b2f4402026-06-13 10:30:19 +020078 docker run --rm $(if $(DOCKER_CPU_SHARES),--cpu-shares $(DOCKER_CPU_SHARES)) -v "$(abspath $(TEI_DIR)):/input:ro" korap/tei2korapxml:latest -l warn $(TEI2KORAPXML_FLAGS) $(TEI_FLAGS) $(TEI_INPUTS) > $@ 2> >(tee $(@:.zip=.log) >&2)
Marc Kupietz99ff2b02026-06-05 08:54:37 +020079 printf "%s\t%s\n" "$$(cat $^ | grep -c '<teiHeader')" "$$(unzip -l $@ | grep data.xml | wc -l)"
80
Marc Kupietz89a44072025-12-11 07:14:00 +010081
Marc Kupietzb820ece2026-06-11 14:29:05 +020082$(BUILD_DIR)/%.tree_tagger.zip: $(BUILD_DIR)/%.zip | bin/korapxmltool
Marc Kupietz5b3ecc02025-12-15 14:43:09 +010083 $(KORAPXMLTOOL) -j 1 -T treetagger -t zip --force -D $(BUILD_DIR) $<
Marc Kupietz89a44072025-12-11 07:14:00 +010084# $(KORAPXMLTOOL) $< | pv | docker run --rm -i korap/conllu2treetagger -l german | conllu2korapxml > $@
85
Marc Kupietzb820ece2026-06-11 14:29:05 +020086$(BUILD_DIR)/%.spacy.zip: $(BUILD_DIR)/%.zip | bin/korapxmltool
Marc Kupietz15b313d2025-12-11 12:20:56 +010087 $(KORAPXMLTOOL) -P spacy -t zip --force -D $(BUILD_DIR) $<
Marc Kupietz89a44072025-12-11 07:14:00 +010088
Marc Kupietz15b313d2025-12-11 12:20:56 +010089lib/Krill-Indexer.jar:
90 mkdir -p lib
91 curl -sL -o $@ https://github.com/korap/Krill/releases/latest/download/Krill-Indexer.jar
Marc Kupietz5b3ecc02025-12-15 14:43:09 +010092
Marc Kupietz89a44072025-12-11 07:14:00 +010093bin/korapxmltool:
94 mkdir -p bin
Marc Kupietz15b313d2025-12-11 12:20:56 +010095 curl -sL -o $@ https://github.com/korap/korapxmltool/releases/latest/download/korapxmltool
Marc Kupietz89a44072025-12-11 07:14:00 +010096 chmod +x $@
97
Marc Kupietzc5239812026-06-05 08:56:46 +020098bin/conllu-gender:
99 mkdir -p bin
100 curl -sL -o $@ https://github.com/KorAP/conllu-gender/releases/latest/download/conllu-gender
101 chmod +x $@
102
Marc Kupietz15b313d2025-12-11 12:20:56 +0100103$(KORAPXMLTOOL_MODELS_PATH)/de.marmot:
104 mkdir -p $(KORAPXMLTOOL_MODELS_PATH)
Marc Kupietz89a44072025-12-11 07:14:00 +0100105 curl -sL -o $@ https://cistern.cis.lmu.de/marmot/models/CURRENT/spmrl/de.marmot
106
Marc Kupietz15b313d2025-12-11 12:20:56 +0100107$(KORAPXMLTOOL_MODELS_PATH)/german.mco:
108 mkdir -p $(KORAPXMLTOOL_MODELS_PATH)
Marc Kupietz89a44072025-12-11 07:14:00 +0100109 curl -sL -o $@ https://corpora.ids-mannheim.de/tools/$@
110
Marc Kupietz15b313d2025-12-11 12:20:56 +0100111$(KORAPXMLTOOL_MODELS_PATH)/dereko_domains_s.classifier:
112 mkdir -p $(KORAPXMLTOOL_MODELS_PATH)
113 curl -sL -o $@ https://corpora.ids-mannheim.de/tools/models/$@
114
115$(KORAPXMLTOOL_MODELS_PATH)/german-fast.tagger:
116 mkdir -p $(KORAPXMLTOOL_MODELS_PATH)
Marc Kupietz89a44072025-12-11 07:14:00 +0100117 curl -sL -o $@ https://corpora.ids-mannheim.de/tools/$@
118
Marc Kupietz15b313d2025-12-11 12:20:56 +0100119$(KORAPXMLTOOL_MODELS_PATH)/germanSR.ser.gz:
120 mkdir -p $(KORAPXMLTOOL_MODELS_PATH)
Marc Kupietz89a44072025-12-11 07:14:00 +0100121 curl -sL -o $@ https://corpora.ids-mannheim.de/tools/$@
122
Marc Kupietz15b313d2025-12-11 12:20:56 +0100123$(KORAPXMLTOOL_MODELS_PATH)/de-pos-maxent.bin:
124 mkdir -p $(KORAPXMLTOOL_MODELS_PATH)
Marc Kupietz89a44072025-12-11 07:14:00 +0100125 curl -sL -o $@ https://corpora.ids-mannheim.de/tools/$@
126
Marc Kupietzb820ece2026-06-11 14:29:05 +0200127$(BUILD_DIR)/%.marmot-malt.zip: $(BUILD_DIR)/%.zip $(KORAPXMLTOOL_MODELS_PATH)/de.marmot $(KORAPXMLTOOL_MODELS_PATH)/german.mco | bin/korapxmltool
Marc Kupietz89a44072025-12-11 07:14:00 +0100128 $(KORAPXMLTOOL) -T marmot:models/de.marmot -P malt:models/german.mco -t zip --force -D $(BUILD_DIR) $<
Marc Kupietz5b3ecc02025-12-15 14:43:09 +0100129
Marc Kupietzb820ece2026-06-11 14:29:05 +0200130$(BUILD_DIR)/%.corenlp.zip: $(BUILD_DIR)/%.zip $(KORAPXMLTOOL_MODELS_PATH)/german-fast.tagger $(KORAPXMLTOOL_MODELS_PATH)/germanSR.ser.gz | bin/korapxmltool
Marc Kupietz89a44072025-12-11 07:14:00 +0100131 $(KORAPXMLTOOL) -T corenlp -P corenlp -t zip --force -D $(BUILD_DIR) $<
132
Marc Kupietzb820ece2026-06-11 14:29:05 +0200133$(BUILD_DIR)/%.opennlp.zip: $(BUILD_DIR)/%.zip $(KORAPXMLTOOL_MODELS_PATH)/de-pos-maxent.bin | bin/korapxmltool
Marc Kupietz89a44072025-12-11 07:14:00 +0100134 $(KORAPXMLTOOL) -T opennlp -t zip --force -D $(BUILD_DIR) $<
135
Marc Kupietzb820ece2026-06-11 14:29:05 +0200136$(BUILD_DIR)/%.cmc.zip: $(BUILD_DIR)/%.zip | bin/korapxmltool
Marc Kupietz5b3ecc02025-12-15 14:43:09 +0100137 $(KORAPXMLTOOL) -j 1 -A "docker run --rm -i korap/conllu-cmc -s" -l error -F cmc -t zip --force -D $(BUILD_DIR) $<
Marc Kupietz800f9bb2025-12-11 17:30:24 +0100138
Marc Kupietzb820ece2026-06-11 14:29:05 +0200139$(BUILD_DIR)/%.gender.zip: $(BUILD_DIR)/%.zip | bin/conllu-gender bin/korapxmltool
Marc Kupietz463da332026-03-08 13:19:15 +0100140 $(KORAPXMLTOOL) -j 1 -A "bin/conllu-gender -s" -l WARNING -F gender -t zip --force -D $(BUILD_DIR) $<
Marc Kupietza2876eb2026-03-07 22:03:17 +0100141
Marc Kupietz19fa4482026-06-07 15:03:56 +0200142# --- Stand-off metadata annotations -----------------------------------------
Marc Kupietz1ed71232026-07-22 11:52:08 +0200143WIKI_TAXONOMY_IMAGE ?= korap/wiki-taxonomy:1.1.1
Marc Kupietz19fa4482026-06-07 15:03:56 +0200144
Marc Kupietzed8cdf02026-06-09 18:26:22 +0200145# Pass --gpus=all to GPU-capable docker runs when a CUDA GPU is usable, detected
146# by nvidia-smi succeeding on the host. (The nvidia runtime is not listed in
147# `docker info` when the Container Toolkit uses CDI, so we don't probe for it.)
Marc Kupietz49773a62026-06-09 17:35:45 +0200148# Probed once at parse time; override by setting GPU_FLAG (e.g. GPU_FLAG= to disable).
Marc Kupietzed8cdf02026-06-09 18:26:22 +0200149GPU_FLAG ?= $(shell if command -v nvidia-smi >/dev/null 2>&1 && nvidia-smi >/dev/null 2>&1; then echo --gpus=all; fi)
Marc Kupietz49773a62026-06-09 17:35:45 +0200150
Marc Kupietz1ed71232026-07-22 11:52:08 +0200151# wiki-taxonomy >= 1.1.1 requires --model-type: the embeddings model only pays
152# off with fp16 acceleration, i.e. on GPUs with tensor cores. Mirror the tool's
153# own fp16 whitelist (FP16_FAST_GPUS in now_to_tei.py; a name list because e.g.
154# GTX 16xx reports compute capability 7.5 despite lacking tensor cores) against
155# the first GPU's name; unknown GPUs and CPU-only hosts get distilbert.
156WIKI_TAXONOMY_FP16_GPUS ?= H100|H200|A100|A30|A40|A10|A16|L4|L40S|L40|T4|V100|RTX
157WIKI_TAXONOMY_MODEL_TYPE ?= $(shell if command -v nvidia-smi >/dev/null 2>&1 \
158 && nvidia-smi --query-gpu=name --format=csv,noheader 2>/dev/null | head -n1 \
159 | grep -qwE '$(WIKI_TAXONOMY_FP16_GPUS)'; \
160 then echo embeddings; else echo distilbert; fi)
161
Marc Kupietz19fa4482026-06-07 15:03:56 +0200162# Wikipedia top-level topic-domain classification (annotation: wikidomain).
163# Unlike the foundry annotations this yields a single stand-off metadata XML per
164# corpus, not a per-text zip. The model is baked into the image, so nothing to mount.
Marc Kupietzb820ece2026-06-11 14:29:05 +0200165$(BUILD_DIR)/%.wikidomain.meta.xml: $(BUILD_DIR)/%.zip | bin/korapxmltool
Marc Kupietz19fa4482026-06-07 15:03:56 +0200166 set -o pipefail; $(KORAPXMLTOOL) -t now $< \
Marc Kupietz49773a62026-06-09 17:35:45 +0200167 | docker run --rm -i $(GPU_FLAG) $(if $(DOCKER_CPU_SHARES),--cpu-shares $(DOCKER_CPU_SHARES)) \
Marc Kupietz1ed71232026-07-22 11:52:08 +0200168 $(WIKI_TAXONOMY_IMAGE) --model-type $(WIKI_TAXONOMY_MODEL_TYPE) --topk 2 --threshold 0.4 > $@ 2> >(tee $(@:.xml=.log) >&2)
Marc Kupietz19fa4482026-06-07 15:03:56 +0200169
Marc Kupietz89a44072025-12-11 07:14:00 +0100170# udpipe target removed as requested
171# %.ud.zip: %.zip
172# $(KORAPXMLTOOL) $< | pv | ./scripts/udpipe2 | conllu2korapxml > $@
173
Marc Kupietz19fa4482026-06-07 15:03:56 +0200174# Active annotation layers to run and package into Krill. Most produce a per-text
175# foundry zip; those also listed in META_ANNOTATIONS instead produce a single
176# stand-off metadata XML per corpus (auto-detected by korapxmltool).
177ANNOTATIONS ?= marmot-malt tree_tagger spacy corenlp opennlp wikidomain
178META_ANNOTATIONS ?= wikidomain
Marc Kupietzc5239812026-06-05 08:56:46 +0200179
Marc Kupietz19fa4482026-06-07 15:03:56 +0200180# Build artifact produced by annotation $(2) for base $(1): a stand-off .meta.xml
181# for meta-type annotations, otherwise a foundry .zip.
182artifact = $(BUILD_DIR)/$(1).$(2).$(if $(filter $(2),$(META_ANNOTATIONS)),meta.xml,zip)
183# Meta-type annotations that are actually active (must be passed to krill explicitly,
184# since the recipe globs *.zip only).
185ACTIVE_META := $(filter $(META_ANNOTATIONS),$(ANNOTATIONS))
186
187KRILL_PREREQS := $(foreach base,$(BASENAMES),$(BUILD_DIR)/$(base).zip $(foreach ann,$(ANNOTATIONS),$(call artifact,$(base),$(ann))))
Marc Kupietz4292bc02026-03-07 16:12:52 +0100188
189pre-krill: check-src $(KRILL_PREREQS)
190
Marc Kupietz19fa4482026-06-07 15:03:56 +0200191# Build just the stand-off metadata layers without indexing.
192meta: check-src $(foreach base,$(BASENAMES),$(foreach ann,$(ACTIVE_META),$(BUILD_DIR)/$(base).$(ann).meta.xml))
193
194# The base zip, every annotation zip, and every stand-off meta XML are folded into
195# one Krill tar. korapxmltool auto-detects the .meta.xml stand-off inputs.
Marc Kupietzb820ece2026-06-11 14:29:05 +0200196$(BUILD_DIR)/%.krill.tar: $(BUILD_DIR)/%.zip $(foreach ann,$(ANNOTATIONS),$(call artifact,%,$(ann))) | bin/korapxmltool
Marc Kupietz19fa4482026-06-07 15:03:56 +0200197 $(KORAPXMLTOOL) --non-word-tokens -f -t krill -D $(BUILD_DIR) $(basename $<)*.zip $(foreach ann,$(ACTIVE_META),$(BUILD_DIR)/$*.$(ann).meta.xml)
Marc Kupietz89a44072025-12-11 07:14:00 +0100198
Marc Kupietz463da332026-03-08 13:19:15 +0100199krill: $(foreach base,$(BASENAMES),$(BUILD_DIR)/$(base).krill.tar)
200
Marc Kupietz15b313d2025-12-11 12:20:56 +0100201$(TARGET_DIR)/index: $(foreach base,$(BASENAMES),$(BUILD_DIR)/$(base).krill.tar)
202 make lib/Krill-Indexer.jar
203 touch lib/krill.cfg
204 mkdir -p $(TARGET_DIR)
205 java -jar lib/Krill-Indexer.jar -c lib/krill.cfg --progress -i $(subst " ",;,$^) -o $@
206
207korap: check-src $(TARGET_DIR)/index
Marc Kupietz34b17bb2026-08-03 17:40:03 +0200208 curl -s https://raw.githubusercontent.com/KorAP/KorAP-Docker/master/compose.yaml | sed 's/64543:64543/$(KORAP_PORT):64543/g' | COMPOSE_PROFILES='export,open,lite' INDEX='$(TARGET_DIR)/index' $(if $(KALAMAR_INSTANCE),KALAMAR_INSTANCE_DIR='$(KALAMAR_INSTANCE_DIR)') $(if $(KALAMAR_IMAGE),KALAMAR_IMAGE='$(KALAMAR_IMAGE)') docker compose -p korap -f - $(if $(KALAMAR_INSTANCE),-f '$(KALAMAR_INSTANCE_DIR)/compose.instance.yaml') up
Marc Kupietz89a44072025-12-11 07:14:00 +0100209
210$(TARGET_DIR)/index.tar.xz: $(TARGET_DIR)/index
211 tar -I 'xz -T0' -C $(dir $<) -cf $@ $(notdir $<)
212
213clean:
214 rm -rf $(BUILD_DIR) $(TARGET_DIR)