blob: be1cfefd79c30530fd144a464030a48559b6d3ca [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 Kupietz9adff672026-06-09 17:18:17 +020013# Discover all *.i5.xml files in SRC_DIR, excluding inlined tagged copies (*-TAG.i5.xml)
14I5_FILES := $(filter-out %-TAG.i5.xml,$(wildcard $(SRC_DIR)/*.i5.xml))
Marc Kupietz89a44072025-12-11 07:14:00 +010015BASENAMES := $(patsubst %.i5.xml,%,$(notdir $(I5_FILES)))
16
Marc Kupietz99ff2b02026-06-05 08:54:37 +020017# Standard TEI P5 support
18TEI_DIR ?= TEI
19TEI_FILES := $(wildcard $(TEI_DIR)/*.xml)
Marc Kupietz3b2f4402026-06-13 10:30:19 +020020# Same files as seen from inside the container (the dir is mounted at /input).
21TEI_INPUTS := $(patsubst $(TEI_DIR)/%,/input/%,$(TEI_FILES))
Marc Kupietz99ff2b02026-06-05 08:54:37 +020022TEI_ZIP_NAME ?= tei
23TEI_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 +020024
Marc Kupietz00db6b02026-06-11 11:46:23 +020025# If set to a foundry name (e.g. gingko), tei2korapxml will use inline annotations and tokens from the source XML
26USE_INLINE_ANNOTATIONS_AS ?=
27TEI2KORAPXML_FLAGS ?= $(if $(USE_INLINE_ANNOTATIONS_AS),--no-tokenizer --inline-tokens $(USE_INLINE_ANNOTATIONS_AS),-s -tk)
28
Marc Kupietz99ff2b02026-06-05 08:54:37 +020029ifneq ($(TEI_FILES),)
30BASENAMES += $(TEI_ZIP_NAME)
31endif
32
Marc Kupietz89a44072025-12-11 07:14:00 +010033BUILD_DIR = build
Marc Kupietz15b313d2025-12-11 12:20:56 +010034TARGET_DIR ?= ./target
Marc Kupietz89a44072025-12-11 07:14:00 +010035MAX_THREADS ?= 8 # $(shell nproc)
36MAKE ?= make -j $(shell nproc)
Marc Kupietz15b313d2025-12-11 12:20:56 +010037# KORAPXMLTOOL_HEAP ?= $(shell echo "$$(($(MAX_THREADS) * 2500))")
Marc Kupietz89a44072025-12-11 07:14:00 +010038KORAPXMLTOOL ?= ./bin/korapxmltool
Marc Kupietz15b313d2025-12-11 12:20:56 +010039KORAPXMLTOOL_MODELS_PATH ?= models
Marc Kupietz4292bc02026-03-07 16:12:52 +010040DOCKER_CPU_SHARES ?= # e.g. 512 for lower priority (default Docker value is 1024)
Marc Kupietz89a44072025-12-11 07:14:00 +010041
42.DELETE_ON_ERROR:
43
Marc Kupietz19fa4482026-06-07 15:03:56 +020044.PHONY: all clean test index korap check-src pre-krill krill meta
Marc Kupietz89a44072025-12-11 07:14:00 +010045
Marc Kupietz19fa4482026-06-07 15:03:56 +020046.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 +010047
Marc Kupietz15b313d2025-12-11 12:20:56 +010048all: check-src korap
Marc Kupietz89a44072025-12-11 07:14:00 +010049
Marc Kupietz15b313d2025-12-11 12:20:56 +010050index: check-src $(TARGET_DIR)/index
51
52check-src:
Marc Kupietz99ff2b02026-06-05 08:54:37 +020053 @if [ ! -d "$(SRC_DIR)" ] && [ ! -d "$(TEI_DIR)" ]; then \
54 echo "Error: Neither SRC_DIR '$(SRC_DIR)' nor TEI_DIR '$(TEI_DIR)' exists."; \
55 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 +010056 exit 1; \
57 fi
Marc Kupietz9adff672026-06-09 17:18:17 +020058 @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 +020059 [ -z "$$(find "$(TEI_DIR)" -maxdepth 1 -name '*.xml' -print -quit 2>/dev/null)" ]; then \
60 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 +010061 exit 1; \
62 fi
Marc Kupietz89a44072025-12-11 07:14:00 +010063
64$(BUILD_DIR)/%.zip: $(SRC_DIR)/%.i5.xml
65 mkdir -p $(BUILD_DIR)
Marc Kupietz00db6b02026-06-11 11:46:23 +020066 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)
67# 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 +010068 printf "%s\t%s\n" "$$(grep -c '<idsText ' $<)" "$$(unzip -l $@ | grep data.xml | wc -l)"
Marc Kupietz89a44072025-12-11 07:14:00 +010069
Marc Kupietz99ff2b02026-06-05 08:54:37 +020070$(BUILD_DIR)/$(TEI_ZIP_NAME).zip: $(TEI_FILES)
71 mkdir -p $(BUILD_DIR)
Marc Kupietz3b2f4402026-06-13 10:30:19 +020072 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 +020073 printf "%s\t%s\n" "$$(cat $^ | grep -c '<teiHeader')" "$$(unzip -l $@ | grep data.xml | wc -l)"
74
Marc Kupietz89a44072025-12-11 07:14:00 +010075
Marc Kupietzb820ece2026-06-11 14:29:05 +020076$(BUILD_DIR)/%.tree_tagger.zip: $(BUILD_DIR)/%.zip | bin/korapxmltool
Marc Kupietz5b3ecc02025-12-15 14:43:09 +010077 $(KORAPXMLTOOL) -j 1 -T treetagger -t zip --force -D $(BUILD_DIR) $<
Marc Kupietz89a44072025-12-11 07:14:00 +010078# $(KORAPXMLTOOL) $< | pv | docker run --rm -i korap/conllu2treetagger -l german | conllu2korapxml > $@
79
Marc Kupietzb820ece2026-06-11 14:29:05 +020080$(BUILD_DIR)/%.spacy.zip: $(BUILD_DIR)/%.zip | bin/korapxmltool
Marc Kupietz15b313d2025-12-11 12:20:56 +010081 $(KORAPXMLTOOL) -P spacy -t zip --force -D $(BUILD_DIR) $<
Marc Kupietz89a44072025-12-11 07:14:00 +010082
Marc Kupietz15b313d2025-12-11 12:20:56 +010083lib/Krill-Indexer.jar:
84 mkdir -p lib
85 curl -sL -o $@ https://github.com/korap/Krill/releases/latest/download/Krill-Indexer.jar
Marc Kupietz5b3ecc02025-12-15 14:43:09 +010086
Marc Kupietz89a44072025-12-11 07:14:00 +010087bin/korapxmltool:
88 mkdir -p bin
Marc Kupietz15b313d2025-12-11 12:20:56 +010089 curl -sL -o $@ https://github.com/korap/korapxmltool/releases/latest/download/korapxmltool
Marc Kupietz89a44072025-12-11 07:14:00 +010090 chmod +x $@
91
Marc Kupietzc5239812026-06-05 08:56:46 +020092bin/conllu-gender:
93 mkdir -p bin
94 curl -sL -o $@ https://github.com/KorAP/conllu-gender/releases/latest/download/conllu-gender
95 chmod +x $@
96
Marc Kupietz15b313d2025-12-11 12:20:56 +010097$(KORAPXMLTOOL_MODELS_PATH)/de.marmot:
98 mkdir -p $(KORAPXMLTOOL_MODELS_PATH)
Marc Kupietz89a44072025-12-11 07:14:00 +010099 curl -sL -o $@ https://cistern.cis.lmu.de/marmot/models/CURRENT/spmrl/de.marmot
100
Marc Kupietz15b313d2025-12-11 12:20:56 +0100101$(KORAPXMLTOOL_MODELS_PATH)/german.mco:
102 mkdir -p $(KORAPXMLTOOL_MODELS_PATH)
Marc Kupietz89a44072025-12-11 07:14:00 +0100103 curl -sL -o $@ https://corpora.ids-mannheim.de/tools/$@
104
Marc Kupietz15b313d2025-12-11 12:20:56 +0100105$(KORAPXMLTOOL_MODELS_PATH)/dereko_domains_s.classifier:
106 mkdir -p $(KORAPXMLTOOL_MODELS_PATH)
107 curl -sL -o $@ https://corpora.ids-mannheim.de/tools/models/$@
108
109$(KORAPXMLTOOL_MODELS_PATH)/german-fast.tagger:
110 mkdir -p $(KORAPXMLTOOL_MODELS_PATH)
Marc Kupietz89a44072025-12-11 07:14:00 +0100111 curl -sL -o $@ https://corpora.ids-mannheim.de/tools/$@
112
Marc Kupietz15b313d2025-12-11 12:20:56 +0100113$(KORAPXMLTOOL_MODELS_PATH)/germanSR.ser.gz:
114 mkdir -p $(KORAPXMLTOOL_MODELS_PATH)
Marc Kupietz89a44072025-12-11 07:14:00 +0100115 curl -sL -o $@ https://corpora.ids-mannheim.de/tools/$@
116
Marc Kupietz15b313d2025-12-11 12:20:56 +0100117$(KORAPXMLTOOL_MODELS_PATH)/de-pos-maxent.bin:
118 mkdir -p $(KORAPXMLTOOL_MODELS_PATH)
Marc Kupietz89a44072025-12-11 07:14:00 +0100119 curl -sL -o $@ https://corpora.ids-mannheim.de/tools/$@
120
Marc Kupietzb820ece2026-06-11 14:29:05 +0200121$(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 +0100122 $(KORAPXMLTOOL) -T marmot:models/de.marmot -P malt:models/german.mco -t zip --force -D $(BUILD_DIR) $<
Marc Kupietz5b3ecc02025-12-15 14:43:09 +0100123
Marc Kupietzb820ece2026-06-11 14:29:05 +0200124$(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 +0100125 $(KORAPXMLTOOL) -T corenlp -P corenlp -t zip --force -D $(BUILD_DIR) $<
126
Marc Kupietzb820ece2026-06-11 14:29:05 +0200127$(BUILD_DIR)/%.opennlp.zip: $(BUILD_DIR)/%.zip $(KORAPXMLTOOL_MODELS_PATH)/de-pos-maxent.bin | bin/korapxmltool
Marc Kupietz89a44072025-12-11 07:14:00 +0100128 $(KORAPXMLTOOL) -T opennlp -t zip --force -D $(BUILD_DIR) $<
129
Marc Kupietzb820ece2026-06-11 14:29:05 +0200130$(BUILD_DIR)/%.cmc.zip: $(BUILD_DIR)/%.zip | bin/korapxmltool
Marc Kupietz5b3ecc02025-12-15 14:43:09 +0100131 $(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 +0100132
Marc Kupietzb820ece2026-06-11 14:29:05 +0200133$(BUILD_DIR)/%.gender.zip: $(BUILD_DIR)/%.zip | bin/conllu-gender bin/korapxmltool
Marc Kupietz463da332026-03-08 13:19:15 +0100134 $(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 +0100135
Marc Kupietz19fa4482026-06-07 15:03:56 +0200136# --- Stand-off metadata annotations -----------------------------------------
Marc Kupietz1ed71232026-07-22 11:52:08 +0200137WIKI_TAXONOMY_IMAGE ?= korap/wiki-taxonomy:1.1.1
Marc Kupietz19fa4482026-06-07 15:03:56 +0200138
Marc Kupietzed8cdf02026-06-09 18:26:22 +0200139# Pass --gpus=all to GPU-capable docker runs when a CUDA GPU is usable, detected
140# by nvidia-smi succeeding on the host. (The nvidia runtime is not listed in
141# `docker info` when the Container Toolkit uses CDI, so we don't probe for it.)
Marc Kupietz49773a62026-06-09 17:35:45 +0200142# Probed once at parse time; override by setting GPU_FLAG (e.g. GPU_FLAG= to disable).
Marc Kupietzed8cdf02026-06-09 18:26:22 +0200143GPU_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 +0200144
Marc Kupietz1ed71232026-07-22 11:52:08 +0200145# wiki-taxonomy >= 1.1.1 requires --model-type: the embeddings model only pays
146# off with fp16 acceleration, i.e. on GPUs with tensor cores. Mirror the tool's
147# own fp16 whitelist (FP16_FAST_GPUS in now_to_tei.py; a name list because e.g.
148# GTX 16xx reports compute capability 7.5 despite lacking tensor cores) against
149# the first GPU's name; unknown GPUs and CPU-only hosts get distilbert.
150WIKI_TAXONOMY_FP16_GPUS ?= H100|H200|A100|A30|A40|A10|A16|L4|L40S|L40|T4|V100|RTX
151WIKI_TAXONOMY_MODEL_TYPE ?= $(shell if command -v nvidia-smi >/dev/null 2>&1 \
152 && nvidia-smi --query-gpu=name --format=csv,noheader 2>/dev/null | head -n1 \
153 | grep -qwE '$(WIKI_TAXONOMY_FP16_GPUS)'; \
154 then echo embeddings; else echo distilbert; fi)
155
Marc Kupietz19fa4482026-06-07 15:03:56 +0200156# Wikipedia top-level topic-domain classification (annotation: wikidomain).
157# Unlike the foundry annotations this yields a single stand-off metadata XML per
158# corpus, not a per-text zip. The model is baked into the image, so nothing to mount.
Marc Kupietzb820ece2026-06-11 14:29:05 +0200159$(BUILD_DIR)/%.wikidomain.meta.xml: $(BUILD_DIR)/%.zip | bin/korapxmltool
Marc Kupietz19fa4482026-06-07 15:03:56 +0200160 set -o pipefail; $(KORAPXMLTOOL) -t now $< \
Marc Kupietz49773a62026-06-09 17:35:45 +0200161 | docker run --rm -i $(GPU_FLAG) $(if $(DOCKER_CPU_SHARES),--cpu-shares $(DOCKER_CPU_SHARES)) \
Marc Kupietz1ed71232026-07-22 11:52:08 +0200162 $(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 +0200163
Marc Kupietz89a44072025-12-11 07:14:00 +0100164# udpipe target removed as requested
165# %.ud.zip: %.zip
166# $(KORAPXMLTOOL) $< | pv | ./scripts/udpipe2 | conllu2korapxml > $@
167
Marc Kupietz19fa4482026-06-07 15:03:56 +0200168# Active annotation layers to run and package into Krill. Most produce a per-text
169# foundry zip; those also listed in META_ANNOTATIONS instead produce a single
170# stand-off metadata XML per corpus (auto-detected by korapxmltool).
171ANNOTATIONS ?= marmot-malt tree_tagger spacy corenlp opennlp wikidomain
172META_ANNOTATIONS ?= wikidomain
Marc Kupietzc5239812026-06-05 08:56:46 +0200173
Marc Kupietz19fa4482026-06-07 15:03:56 +0200174# Build artifact produced by annotation $(2) for base $(1): a stand-off .meta.xml
175# for meta-type annotations, otherwise a foundry .zip.
176artifact = $(BUILD_DIR)/$(1).$(2).$(if $(filter $(2),$(META_ANNOTATIONS)),meta.xml,zip)
177# Meta-type annotations that are actually active (must be passed to krill explicitly,
178# since the recipe globs *.zip only).
179ACTIVE_META := $(filter $(META_ANNOTATIONS),$(ANNOTATIONS))
180
181KRILL_PREREQS := $(foreach base,$(BASENAMES),$(BUILD_DIR)/$(base).zip $(foreach ann,$(ANNOTATIONS),$(call artifact,$(base),$(ann))))
Marc Kupietz4292bc02026-03-07 16:12:52 +0100182
183pre-krill: check-src $(KRILL_PREREQS)
184
Marc Kupietz19fa4482026-06-07 15:03:56 +0200185# Build just the stand-off metadata layers without indexing.
186meta: check-src $(foreach base,$(BASENAMES),$(foreach ann,$(ACTIVE_META),$(BUILD_DIR)/$(base).$(ann).meta.xml))
187
188# The base zip, every annotation zip, and every stand-off meta XML are folded into
189# one Krill tar. korapxmltool auto-detects the .meta.xml stand-off inputs.
Marc Kupietzb820ece2026-06-11 14:29:05 +0200190$(BUILD_DIR)/%.krill.tar: $(BUILD_DIR)/%.zip $(foreach ann,$(ANNOTATIONS),$(call artifact,%,$(ann))) | bin/korapxmltool
Marc Kupietz19fa4482026-06-07 15:03:56 +0200191 $(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 +0100192
Marc Kupietz463da332026-03-08 13:19:15 +0100193krill: $(foreach base,$(BASENAMES),$(BUILD_DIR)/$(base).krill.tar)
194
Marc Kupietz15b313d2025-12-11 12:20:56 +0100195$(TARGET_DIR)/index: $(foreach base,$(BASENAMES),$(BUILD_DIR)/$(base).krill.tar)
196 make lib/Krill-Indexer.jar
197 touch lib/krill.cfg
198 mkdir -p $(TARGET_DIR)
199 java -jar lib/Krill-Indexer.jar -c lib/krill.cfg --progress -i $(subst " ",;,$^) -o $@
200
201korap: check-src $(TARGET_DIR)/index
Marc Kupietza6effde2026-08-03 16:04:02 +0200202 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)') docker compose -p korap -f - $(if $(KALAMAR_INSTANCE),-f '$(KALAMAR_INSTANCE_DIR)/compose.instance.yaml') up
Marc Kupietz89a44072025-12-11 07:14:00 +0100203
204$(TARGET_DIR)/index.tar.xz: $(TARGET_DIR)/index
205 tar -I 'xz -T0' -C $(dir $<) -cf $@ $(notdir $<)
206
207clean:
208 rm -rf $(BUILD_DIR) $(TARGET_DIR)