blob: 3fca452d3cc768a4c68cc73718d767792368cf16 [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 Kupietza67cc042026-08-06 07:27:50 +020013# Directory with Kalamar templates describing your own corpus, mounted into the
14# kalamar container. Put a doc/corpus.html.ep page there to replace Kalamar's
15# placeholder corpus page; KorAP-Docker's custom/README.md lists the available
16# templates and ships examples. The directory is created on demand.
17# make korap KALAMAR_TEMPLATES=my-corpus-docs
18KALAMAR_TEMPLATES ?= custom
19KALAMAR_TEMPLATES_DIR := $(abspath $(KALAMAR_TEMPLATES))
20
Marc Kupietz34b17bb2026-08-03 17:40:03 +020021# Optional: override the kalamar container image (e.g. a locally built tag).
22# The instance's compose.instance.yaml already defaults to a renderer-capable
23# image; set this to test a specific build, e.g.
24# make korap KALAMAR_INSTANCE=../Kalamar-Instance-IDS KALAMAR_IMAGE=korap/kalamar:v0.66-alpha-large
25KALAMAR_IMAGE ?=
26
Marc Kupietz9adff672026-06-09 17:18:17 +020027# Discover all *.i5.xml files in SRC_DIR, excluding inlined tagged copies (*-TAG.i5.xml)
28I5_FILES := $(filter-out %-TAG.i5.xml,$(wildcard $(SRC_DIR)/*.i5.xml))
Marc Kupietz89a44072025-12-11 07:14:00 +010029BASENAMES := $(patsubst %.i5.xml,%,$(notdir $(I5_FILES)))
30
Marc Kupietz99ff2b02026-06-05 08:54:37 +020031# Standard TEI P5 support
32TEI_DIR ?= TEI
33TEI_FILES := $(wildcard $(TEI_DIR)/*.xml)
Marc Kupietz3b2f4402026-06-13 10:30:19 +020034# Same files as seen from inside the container (the dir is mounted at /input).
35TEI_INPUTS := $(patsubst $(TEI_DIR)/%,/input/%,$(TEI_FILES))
Marc Kupietz99ff2b02026-06-05 08:54:37 +020036TEI_ZIP_NAME ?= tei
37TEI_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 +020038
Marc Kupietz00db6b02026-06-11 11:46:23 +020039# If set to a foundry name (e.g. gingko), tei2korapxml will use inline annotations and tokens from the source XML
40USE_INLINE_ANNOTATIONS_AS ?=
41TEI2KORAPXML_FLAGS ?= $(if $(USE_INLINE_ANNOTATIONS_AS),--no-tokenizer --inline-tokens $(USE_INLINE_ANNOTATIONS_AS),-s -tk)
42
Marc Kupietz99ff2b02026-06-05 08:54:37 +020043ifneq ($(TEI_FILES),)
44BASENAMES += $(TEI_ZIP_NAME)
45endif
46
Marc Kupietz89a44072025-12-11 07:14:00 +010047BUILD_DIR = build
Marc Kupietz15b313d2025-12-11 12:20:56 +010048TARGET_DIR ?= ./target
Marc Kupietz89a44072025-12-11 07:14:00 +010049MAX_THREADS ?= 8 # $(shell nproc)
50MAKE ?= make -j $(shell nproc)
Marc Kupietz15b313d2025-12-11 12:20:56 +010051# KORAPXMLTOOL_HEAP ?= $(shell echo "$$(($(MAX_THREADS) * 2500))")
Marc Kupietz89a44072025-12-11 07:14:00 +010052KORAPXMLTOOL ?= ./bin/korapxmltool
Marc Kupietz15b313d2025-12-11 12:20:56 +010053KORAPXMLTOOL_MODELS_PATH ?= models
Marc Kupietz4292bc02026-03-07 16:12:52 +010054DOCKER_CPU_SHARES ?= # e.g. 512 for lower priority (default Docker value is 1024)
Marc Kupietz89a44072025-12-11 07:14:00 +010055
56.DELETE_ON_ERROR:
57
Marc Kupietz19fa4482026-06-07 15:03:56 +020058.PHONY: all clean test index korap check-src pre-krill krill meta
Marc Kupietz89a44072025-12-11 07:14:00 +010059
Marc Kupietz19fa4482026-06-07 15:03:56 +020060.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 +010061
Marc Kupietz15b313d2025-12-11 12:20:56 +010062all: check-src korap
Marc Kupietz89a44072025-12-11 07:14:00 +010063
Marc Kupietz15b313d2025-12-11 12:20:56 +010064index: check-src $(TARGET_DIR)/index
65
66check-src:
Marc Kupietz99ff2b02026-06-05 08:54:37 +020067 @if [ ! -d "$(SRC_DIR)" ] && [ ! -d "$(TEI_DIR)" ]; then \
68 echo "Error: Neither SRC_DIR '$(SRC_DIR)' nor TEI_DIR '$(TEI_DIR)' exists."; \
69 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 +010070 exit 1; \
71 fi
Marc Kupietz9adff672026-06-09 17:18:17 +020072 @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 +020073 [ -z "$$(find "$(TEI_DIR)" -maxdepth 1 -name '*.xml' -print -quit 2>/dev/null)" ]; then \
74 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 +010075 exit 1; \
76 fi
Marc Kupietz89a44072025-12-11 07:14:00 +010077
78$(BUILD_DIR)/%.zip: $(SRC_DIR)/%.i5.xml
79 mkdir -p $(BUILD_DIR)
Marc Kupietz00db6b02026-06-11 11:46:23 +020080 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)
81# 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 +010082 printf "%s\t%s\n" "$$(grep -c '<idsText ' $<)" "$$(unzip -l $@ | grep data.xml | wc -l)"
Marc Kupietz89a44072025-12-11 07:14:00 +010083
Marc Kupietz99ff2b02026-06-05 08:54:37 +020084$(BUILD_DIR)/$(TEI_ZIP_NAME).zip: $(TEI_FILES)
85 mkdir -p $(BUILD_DIR)
Marc Kupietz3b2f4402026-06-13 10:30:19 +020086 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 +020087 printf "%s\t%s\n" "$$(cat $^ | grep -c '<teiHeader')" "$$(unzip -l $@ | grep data.xml | wc -l)"
88
Marc Kupietz89a44072025-12-11 07:14:00 +010089
Marc Kupietzb820ece2026-06-11 14:29:05 +020090$(BUILD_DIR)/%.tree_tagger.zip: $(BUILD_DIR)/%.zip | bin/korapxmltool
Marc Kupietz5b3ecc02025-12-15 14:43:09 +010091 $(KORAPXMLTOOL) -j 1 -T treetagger -t zip --force -D $(BUILD_DIR) $<
Marc Kupietz89a44072025-12-11 07:14:00 +010092# $(KORAPXMLTOOL) $< | pv | docker run --rm -i korap/conllu2treetagger -l german | conllu2korapxml > $@
93
Marc Kupietzb820ece2026-06-11 14:29:05 +020094$(BUILD_DIR)/%.spacy.zip: $(BUILD_DIR)/%.zip | bin/korapxmltool
Marc Kupietz15b313d2025-12-11 12:20:56 +010095 $(KORAPXMLTOOL) -P spacy -t zip --force -D $(BUILD_DIR) $<
Marc Kupietz89a44072025-12-11 07:14:00 +010096
Marc Kupietz15b313d2025-12-11 12:20:56 +010097lib/Krill-Indexer.jar:
98 mkdir -p lib
99 curl -sL -o $@ https://github.com/korap/Krill/releases/latest/download/Krill-Indexer.jar
Marc Kupietz5b3ecc02025-12-15 14:43:09 +0100100
Marc Kupietz89a44072025-12-11 07:14:00 +0100101bin/korapxmltool:
102 mkdir -p bin
Marc Kupietz15b313d2025-12-11 12:20:56 +0100103 curl -sL -o $@ https://github.com/korap/korapxmltool/releases/latest/download/korapxmltool
Marc Kupietz89a44072025-12-11 07:14:00 +0100104 chmod +x $@
105
Marc Kupietzc5239812026-06-05 08:56:46 +0200106bin/conllu-gender:
107 mkdir -p bin
108 curl -sL -o $@ https://github.com/KorAP/conllu-gender/releases/latest/download/conllu-gender
109 chmod +x $@
110
Marc Kupietz15b313d2025-12-11 12:20:56 +0100111$(KORAPXMLTOOL_MODELS_PATH)/de.marmot:
112 mkdir -p $(KORAPXMLTOOL_MODELS_PATH)
Marc Kupietz89a44072025-12-11 07:14:00 +0100113 curl -sL -o $@ https://cistern.cis.lmu.de/marmot/models/CURRENT/spmrl/de.marmot
114
Marc Kupietz15b313d2025-12-11 12:20:56 +0100115$(KORAPXMLTOOL_MODELS_PATH)/german.mco:
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)/dereko_domains_s.classifier:
120 mkdir -p $(KORAPXMLTOOL_MODELS_PATH)
121 curl -sL -o $@ https://corpora.ids-mannheim.de/tools/models/$@
122
123$(KORAPXMLTOOL_MODELS_PATH)/german-fast.tagger:
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 Kupietz15b313d2025-12-11 12:20:56 +0100127$(KORAPXMLTOOL_MODELS_PATH)/germanSR.ser.gz:
128 mkdir -p $(KORAPXMLTOOL_MODELS_PATH)
Marc Kupietz89a44072025-12-11 07:14:00 +0100129 curl -sL -o $@ https://corpora.ids-mannheim.de/tools/$@
130
Marc Kupietz15b313d2025-12-11 12:20:56 +0100131$(KORAPXMLTOOL_MODELS_PATH)/de-pos-maxent.bin:
132 mkdir -p $(KORAPXMLTOOL_MODELS_PATH)
Marc Kupietz89a44072025-12-11 07:14:00 +0100133 curl -sL -o $@ https://corpora.ids-mannheim.de/tools/$@
134
Marc Kupietzb820ece2026-06-11 14:29:05 +0200135$(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 +0100136 $(KORAPXMLTOOL) -T marmot:models/de.marmot -P malt:models/german.mco -t zip --force -D $(BUILD_DIR) $<
Marc Kupietz5b3ecc02025-12-15 14:43:09 +0100137
Marc Kupietzb820ece2026-06-11 14:29:05 +0200138$(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 +0100139 $(KORAPXMLTOOL) -T corenlp -P corenlp -t zip --force -D $(BUILD_DIR) $<
140
Marc Kupietzb820ece2026-06-11 14:29:05 +0200141$(BUILD_DIR)/%.opennlp.zip: $(BUILD_DIR)/%.zip $(KORAPXMLTOOL_MODELS_PATH)/de-pos-maxent.bin | bin/korapxmltool
Marc Kupietz89a44072025-12-11 07:14:00 +0100142 $(KORAPXMLTOOL) -T opennlp -t zip --force -D $(BUILD_DIR) $<
143
Marc Kupietzb820ece2026-06-11 14:29:05 +0200144$(BUILD_DIR)/%.cmc.zip: $(BUILD_DIR)/%.zip | bin/korapxmltool
Marc Kupietz5b3ecc02025-12-15 14:43:09 +0100145 $(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 +0100146
Marc Kupietzb820ece2026-06-11 14:29:05 +0200147$(BUILD_DIR)/%.gender.zip: $(BUILD_DIR)/%.zip | bin/conllu-gender bin/korapxmltool
Marc Kupietz463da332026-03-08 13:19:15 +0100148 $(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 +0100149
Marc Kupietz19fa4482026-06-07 15:03:56 +0200150# --- Stand-off metadata annotations -----------------------------------------
Marc Kupietz1ed71232026-07-22 11:52:08 +0200151WIKI_TAXONOMY_IMAGE ?= korap/wiki-taxonomy:1.1.1
Marc Kupietz19fa4482026-06-07 15:03:56 +0200152
Marc Kupietzed8cdf02026-06-09 18:26:22 +0200153# Pass --gpus=all to GPU-capable docker runs when a CUDA GPU is usable, detected
154# by nvidia-smi succeeding on the host. (The nvidia runtime is not listed in
155# `docker info` when the Container Toolkit uses CDI, so we don't probe for it.)
Marc Kupietz49773a62026-06-09 17:35:45 +0200156# Probed once at parse time; override by setting GPU_FLAG (e.g. GPU_FLAG= to disable).
Marc Kupietzed8cdf02026-06-09 18:26:22 +0200157GPU_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 +0200158
Marc Kupietz1ed71232026-07-22 11:52:08 +0200159# wiki-taxonomy >= 1.1.1 requires --model-type: the embeddings model only pays
160# off with fp16 acceleration, i.e. on GPUs with tensor cores. Mirror the tool's
161# own fp16 whitelist (FP16_FAST_GPUS in now_to_tei.py; a name list because e.g.
162# GTX 16xx reports compute capability 7.5 despite lacking tensor cores) against
163# the first GPU's name; unknown GPUs and CPU-only hosts get distilbert.
164WIKI_TAXONOMY_FP16_GPUS ?= H100|H200|A100|A30|A40|A10|A16|L4|L40S|L40|T4|V100|RTX
165WIKI_TAXONOMY_MODEL_TYPE ?= $(shell if command -v nvidia-smi >/dev/null 2>&1 \
166 && nvidia-smi --query-gpu=name --format=csv,noheader 2>/dev/null | head -n1 \
167 | grep -qwE '$(WIKI_TAXONOMY_FP16_GPUS)'; \
168 then echo embeddings; else echo distilbert; fi)
169
Marc Kupietz19fa4482026-06-07 15:03:56 +0200170# Wikipedia top-level topic-domain classification (annotation: wikidomain).
171# Unlike the foundry annotations this yields a single stand-off metadata XML per
172# corpus, not a per-text zip. The model is baked into the image, so nothing to mount.
Marc Kupietzb820ece2026-06-11 14:29:05 +0200173$(BUILD_DIR)/%.wikidomain.meta.xml: $(BUILD_DIR)/%.zip | bin/korapxmltool
Marc Kupietz19fa4482026-06-07 15:03:56 +0200174 set -o pipefail; $(KORAPXMLTOOL) -t now $< \
Marc Kupietz49773a62026-06-09 17:35:45 +0200175 | docker run --rm -i $(GPU_FLAG) $(if $(DOCKER_CPU_SHARES),--cpu-shares $(DOCKER_CPU_SHARES)) \
Marc Kupietz1ed71232026-07-22 11:52:08 +0200176 $(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 +0200177
Marc Kupietz89a44072025-12-11 07:14:00 +0100178# udpipe target removed as requested
179# %.ud.zip: %.zip
180# $(KORAPXMLTOOL) $< | pv | ./scripts/udpipe2 | conllu2korapxml > $@
181
Marc Kupietz19fa4482026-06-07 15:03:56 +0200182# Active annotation layers to run and package into Krill. Most produce a per-text
183# foundry zip; those also listed in META_ANNOTATIONS instead produce a single
184# stand-off metadata XML per corpus (auto-detected by korapxmltool).
185ANNOTATIONS ?= marmot-malt tree_tagger spacy corenlp opennlp wikidomain
186META_ANNOTATIONS ?= wikidomain
Marc Kupietzc5239812026-06-05 08:56:46 +0200187
Marc Kupietz19fa4482026-06-07 15:03:56 +0200188# Build artifact produced by annotation $(2) for base $(1): a stand-off .meta.xml
189# for meta-type annotations, otherwise a foundry .zip.
190artifact = $(BUILD_DIR)/$(1).$(2).$(if $(filter $(2),$(META_ANNOTATIONS)),meta.xml,zip)
191# Meta-type annotations that are actually active (must be passed to krill explicitly,
192# since the recipe globs *.zip only).
193ACTIVE_META := $(filter $(META_ANNOTATIONS),$(ANNOTATIONS))
194
195KRILL_PREREQS := $(foreach base,$(BASENAMES),$(BUILD_DIR)/$(base).zip $(foreach ann,$(ANNOTATIONS),$(call artifact,$(base),$(ann))))
Marc Kupietz4292bc02026-03-07 16:12:52 +0100196
197pre-krill: check-src $(KRILL_PREREQS)
198
Marc Kupietz19fa4482026-06-07 15:03:56 +0200199# Build just the stand-off metadata layers without indexing.
200meta: check-src $(foreach base,$(BASENAMES),$(foreach ann,$(ACTIVE_META),$(BUILD_DIR)/$(base).$(ann).meta.xml))
201
202# The base zip, every annotation zip, and every stand-off meta XML are folded into
203# one Krill tar. korapxmltool auto-detects the .meta.xml stand-off inputs.
Marc Kupietzb820ece2026-06-11 14:29:05 +0200204$(BUILD_DIR)/%.krill.tar: $(BUILD_DIR)/%.zip $(foreach ann,$(ANNOTATIONS),$(call artifact,%,$(ann))) | bin/korapxmltool
Marc Kupietz19fa4482026-06-07 15:03:56 +0200205 $(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 +0100206
Marc Kupietz463da332026-03-08 13:19:15 +0100207krill: $(foreach base,$(BASENAMES),$(BUILD_DIR)/$(base).krill.tar)
208
Marc Kupietz15b313d2025-12-11 12:20:56 +0100209$(TARGET_DIR)/index: $(foreach base,$(BASENAMES),$(BUILD_DIR)/$(base).krill.tar)
210 make lib/Krill-Indexer.jar
211 touch lib/krill.cfg
212 mkdir -p $(TARGET_DIR)
213 java -jar lib/Krill-Indexer.jar -c lib/krill.cfg --progress -i $(subst " ",;,$^) -o $@
214
215korap: check-src $(TARGET_DIR)/index
Marc Kupietza67cc042026-08-06 07:27:50 +0200216 mkdir -p '$(KALAMAR_TEMPLATES_DIR)'
217 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' KALAMAR_TEMPLATES='$(KALAMAR_TEMPLATES_DIR)' $(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 +0100218
219$(TARGET_DIR)/index.tar.xz: $(TARGET_DIR)/index
220 tar -I 'xz -T0' -C $(dir $<) -cf $@ $(notdir $<)
221
222clean:
223 rm -rf $(BUILD_DIR) $(TARGET_DIR)