blob: d4cca60e463184dc79c62775edbdcae044039771 [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 Kupietz9adff672026-06-09 17:18:17 +02005# Discover all *.i5.xml files in SRC_DIR, excluding inlined tagged copies (*-TAG.i5.xml)
6I5_FILES := $(filter-out %-TAG.i5.xml,$(wildcard $(SRC_DIR)/*.i5.xml))
Marc Kupietz89a44072025-12-11 07:14:00 +01007BASENAMES := $(patsubst %.i5.xml,%,$(notdir $(I5_FILES)))
8
Marc Kupietz99ff2b02026-06-05 08:54:37 +02009# Standard TEI P5 support
10TEI_DIR ?= TEI
11TEI_FILES := $(wildcard $(TEI_DIR)/*.xml)
Marc Kupietz3b2f4402026-06-13 10:30:19 +020012# Same files as seen from inside the container (the dir is mounted at /input).
13TEI_INPUTS := $(patsubst $(TEI_DIR)/%,/input/%,$(TEI_FILES))
Marc Kupietz99ff2b02026-06-05 08:54:37 +020014TEI_ZIP_NAME ?= tei
15TEI_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 +020016
Marc Kupietz00db6b02026-06-11 11:46:23 +020017# If set to a foundry name (e.g. gingko), tei2korapxml will use inline annotations and tokens from the source XML
18USE_INLINE_ANNOTATIONS_AS ?=
19TEI2KORAPXML_FLAGS ?= $(if $(USE_INLINE_ANNOTATIONS_AS),--no-tokenizer --inline-tokens $(USE_INLINE_ANNOTATIONS_AS),-s -tk)
20
Marc Kupietz99ff2b02026-06-05 08:54:37 +020021ifneq ($(TEI_FILES),)
22BASENAMES += $(TEI_ZIP_NAME)
23endif
24
Marc Kupietz89a44072025-12-11 07:14:00 +010025BUILD_DIR = build
Marc Kupietz15b313d2025-12-11 12:20:56 +010026TARGET_DIR ?= ./target
Marc Kupietz89a44072025-12-11 07:14:00 +010027MAX_THREADS ?= 8 # $(shell nproc)
28MAKE ?= make -j $(shell nproc)
Marc Kupietz15b313d2025-12-11 12:20:56 +010029# KORAPXMLTOOL_HEAP ?= $(shell echo "$$(($(MAX_THREADS) * 2500))")
Marc Kupietz89a44072025-12-11 07:14:00 +010030KORAPXMLTOOL ?= ./bin/korapxmltool
Marc Kupietz15b313d2025-12-11 12:20:56 +010031KORAPXMLTOOL_MODELS_PATH ?= models
Marc Kupietz4292bc02026-03-07 16:12:52 +010032DOCKER_CPU_SHARES ?= # e.g. 512 for lower priority (default Docker value is 1024)
Marc Kupietz89a44072025-12-11 07:14:00 +010033
34.DELETE_ON_ERROR:
35
Marc Kupietz19fa4482026-06-07 15:03:56 +020036.PHONY: all clean test index korap check-src pre-krill krill meta
Marc Kupietz89a44072025-12-11 07:14:00 +010037
Marc Kupietz19fa4482026-06-07 15:03:56 +020038.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 +010039
Marc Kupietz15b313d2025-12-11 12:20:56 +010040all: check-src korap
Marc Kupietz89a44072025-12-11 07:14:00 +010041
Marc Kupietz15b313d2025-12-11 12:20:56 +010042index: check-src $(TARGET_DIR)/index
43
44check-src:
Marc Kupietz99ff2b02026-06-05 08:54:37 +020045 @if [ ! -d "$(SRC_DIR)" ] && [ ! -d "$(TEI_DIR)" ]; then \
46 echo "Error: Neither SRC_DIR '$(SRC_DIR)' nor TEI_DIR '$(TEI_DIR)' exists."; \
47 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 +010048 exit 1; \
49 fi
Marc Kupietz9adff672026-06-09 17:18:17 +020050 @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 +020051 [ -z "$$(find "$(TEI_DIR)" -maxdepth 1 -name '*.xml' -print -quit 2>/dev/null)" ]; then \
52 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 +010053 exit 1; \
54 fi
Marc Kupietz89a44072025-12-11 07:14:00 +010055
56$(BUILD_DIR)/%.zip: $(SRC_DIR)/%.i5.xml
57 mkdir -p $(BUILD_DIR)
Marc Kupietz00db6b02026-06-11 11:46:23 +020058 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)
59# 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 +010060 printf "%s\t%s\n" "$$(grep -c '<idsText ' $<)" "$$(unzip -l $@ | grep data.xml | wc -l)"
Marc Kupietz89a44072025-12-11 07:14:00 +010061
Marc Kupietz99ff2b02026-06-05 08:54:37 +020062$(BUILD_DIR)/$(TEI_ZIP_NAME).zip: $(TEI_FILES)
63 mkdir -p $(BUILD_DIR)
Marc Kupietz3b2f4402026-06-13 10:30:19 +020064 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 +020065 printf "%s\t%s\n" "$$(cat $^ | grep -c '<teiHeader')" "$$(unzip -l $@ | grep data.xml | wc -l)"
66
Marc Kupietz89a44072025-12-11 07:14:00 +010067
Marc Kupietzb820ece2026-06-11 14:29:05 +020068$(BUILD_DIR)/%.tree_tagger.zip: $(BUILD_DIR)/%.zip | bin/korapxmltool
Marc Kupietz5b3ecc02025-12-15 14:43:09 +010069 $(KORAPXMLTOOL) -j 1 -T treetagger -t zip --force -D $(BUILD_DIR) $<
Marc Kupietz89a44072025-12-11 07:14:00 +010070# $(KORAPXMLTOOL) $< | pv | docker run --rm -i korap/conllu2treetagger -l german | conllu2korapxml > $@
71
Marc Kupietzb820ece2026-06-11 14:29:05 +020072$(BUILD_DIR)/%.spacy.zip: $(BUILD_DIR)/%.zip | bin/korapxmltool
Marc Kupietz15b313d2025-12-11 12:20:56 +010073 $(KORAPXMLTOOL) -P spacy -t zip --force -D $(BUILD_DIR) $<
Marc Kupietz89a44072025-12-11 07:14:00 +010074
Marc Kupietz15b313d2025-12-11 12:20:56 +010075lib/Krill-Indexer.jar:
76 mkdir -p lib
77 curl -sL -o $@ https://github.com/korap/Krill/releases/latest/download/Krill-Indexer.jar
Marc Kupietz5b3ecc02025-12-15 14:43:09 +010078
Marc Kupietz89a44072025-12-11 07:14:00 +010079bin/korapxmltool:
80 mkdir -p bin
Marc Kupietz15b313d2025-12-11 12:20:56 +010081 curl -sL -o $@ https://github.com/korap/korapxmltool/releases/latest/download/korapxmltool
Marc Kupietz89a44072025-12-11 07:14:00 +010082 chmod +x $@
83
Marc Kupietzc5239812026-06-05 08:56:46 +020084bin/conllu-gender:
85 mkdir -p bin
86 curl -sL -o $@ https://github.com/KorAP/conllu-gender/releases/latest/download/conllu-gender
87 chmod +x $@
88
Marc Kupietz15b313d2025-12-11 12:20:56 +010089$(KORAPXMLTOOL_MODELS_PATH)/de.marmot:
90 mkdir -p $(KORAPXMLTOOL_MODELS_PATH)
Marc Kupietz89a44072025-12-11 07:14:00 +010091 curl -sL -o $@ https://cistern.cis.lmu.de/marmot/models/CURRENT/spmrl/de.marmot
92
Marc Kupietz15b313d2025-12-11 12:20:56 +010093$(KORAPXMLTOOL_MODELS_PATH)/german.mco:
94 mkdir -p $(KORAPXMLTOOL_MODELS_PATH)
Marc Kupietz89a44072025-12-11 07:14:00 +010095 curl -sL -o $@ https://corpora.ids-mannheim.de/tools/$@
96
Marc Kupietz15b313d2025-12-11 12:20:56 +010097$(KORAPXMLTOOL_MODELS_PATH)/dereko_domains_s.classifier:
98 mkdir -p $(KORAPXMLTOOL_MODELS_PATH)
99 curl -sL -o $@ https://corpora.ids-mannheim.de/tools/models/$@
100
101$(KORAPXMLTOOL_MODELS_PATH)/german-fast.tagger:
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)/germanSR.ser.gz:
106 mkdir -p $(KORAPXMLTOOL_MODELS_PATH)
Marc Kupietz89a44072025-12-11 07:14:00 +0100107 curl -sL -o $@ https://corpora.ids-mannheim.de/tools/$@
108
Marc Kupietz15b313d2025-12-11 12:20:56 +0100109$(KORAPXMLTOOL_MODELS_PATH)/de-pos-maxent.bin:
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 Kupietzb820ece2026-06-11 14:29:05 +0200113$(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 +0100114 $(KORAPXMLTOOL) -T marmot:models/de.marmot -P malt:models/german.mco -t zip --force -D $(BUILD_DIR) $<
Marc Kupietz5b3ecc02025-12-15 14:43:09 +0100115
Marc Kupietzb820ece2026-06-11 14:29:05 +0200116$(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 +0100117 $(KORAPXMLTOOL) -T corenlp -P corenlp -t zip --force -D $(BUILD_DIR) $<
118
Marc Kupietzb820ece2026-06-11 14:29:05 +0200119$(BUILD_DIR)/%.opennlp.zip: $(BUILD_DIR)/%.zip $(KORAPXMLTOOL_MODELS_PATH)/de-pos-maxent.bin | bin/korapxmltool
Marc Kupietz89a44072025-12-11 07:14:00 +0100120 $(KORAPXMLTOOL) -T opennlp -t zip --force -D $(BUILD_DIR) $<
121
Marc Kupietzb820ece2026-06-11 14:29:05 +0200122$(BUILD_DIR)/%.cmc.zip: $(BUILD_DIR)/%.zip | bin/korapxmltool
Marc Kupietz5b3ecc02025-12-15 14:43:09 +0100123 $(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 +0100124
Marc Kupietzb820ece2026-06-11 14:29:05 +0200125$(BUILD_DIR)/%.gender.zip: $(BUILD_DIR)/%.zip | bin/conllu-gender bin/korapxmltool
Marc Kupietz463da332026-03-08 13:19:15 +0100126 $(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 +0100127
Marc Kupietz19fa4482026-06-07 15:03:56 +0200128# --- Stand-off metadata annotations -----------------------------------------
129WIKI_TAXONOMY_IMAGE ?= korap/wiki-taxonomy
130
Marc Kupietzed8cdf02026-06-09 18:26:22 +0200131# Pass --gpus=all to GPU-capable docker runs when a CUDA GPU is usable, detected
132# by nvidia-smi succeeding on the host. (The nvidia runtime is not listed in
133# `docker info` when the Container Toolkit uses CDI, so we don't probe for it.)
Marc Kupietz49773a62026-06-09 17:35:45 +0200134# Probed once at parse time; override by setting GPU_FLAG (e.g. GPU_FLAG= to disable).
Marc Kupietzed8cdf02026-06-09 18:26:22 +0200135GPU_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 +0200136
Marc Kupietz19fa4482026-06-07 15:03:56 +0200137# Wikipedia top-level topic-domain classification (annotation: wikidomain).
138# Unlike the foundry annotations this yields a single stand-off metadata XML per
139# corpus, not a per-text zip. The model is baked into the image, so nothing to mount.
Marc Kupietzb820ece2026-06-11 14:29:05 +0200140$(BUILD_DIR)/%.wikidomain.meta.xml: $(BUILD_DIR)/%.zip | bin/korapxmltool
Marc Kupietz19fa4482026-06-07 15:03:56 +0200141 set -o pipefail; $(KORAPXMLTOOL) -t now $< \
Marc Kupietz49773a62026-06-09 17:35:45 +0200142 | docker run --rm -i $(GPU_FLAG) $(if $(DOCKER_CPU_SHARES),--cpu-shares $(DOCKER_CPU_SHARES)) \
Marc Kupietz19fa4482026-06-07 15:03:56 +0200143 $(WIKI_TAXONOMY_IMAGE) --topk 2 --threshold 0.4 > $@ 2> >(tee $(@:.xml=.log) >&2)
144
Marc Kupietz89a44072025-12-11 07:14:00 +0100145# udpipe target removed as requested
146# %.ud.zip: %.zip
147# $(KORAPXMLTOOL) $< | pv | ./scripts/udpipe2 | conllu2korapxml > $@
148
Marc Kupietz19fa4482026-06-07 15:03:56 +0200149# Active annotation layers to run and package into Krill. Most produce a per-text
150# foundry zip; those also listed in META_ANNOTATIONS instead produce a single
151# stand-off metadata XML per corpus (auto-detected by korapxmltool).
152ANNOTATIONS ?= marmot-malt tree_tagger spacy corenlp opennlp wikidomain
153META_ANNOTATIONS ?= wikidomain
Marc Kupietzc5239812026-06-05 08:56:46 +0200154
Marc Kupietz19fa4482026-06-07 15:03:56 +0200155# Build artifact produced by annotation $(2) for base $(1): a stand-off .meta.xml
156# for meta-type annotations, otherwise a foundry .zip.
157artifact = $(BUILD_DIR)/$(1).$(2).$(if $(filter $(2),$(META_ANNOTATIONS)),meta.xml,zip)
158# Meta-type annotations that are actually active (must be passed to krill explicitly,
159# since the recipe globs *.zip only).
160ACTIVE_META := $(filter $(META_ANNOTATIONS),$(ANNOTATIONS))
161
162KRILL_PREREQS := $(foreach base,$(BASENAMES),$(BUILD_DIR)/$(base).zip $(foreach ann,$(ANNOTATIONS),$(call artifact,$(base),$(ann))))
Marc Kupietz4292bc02026-03-07 16:12:52 +0100163
164pre-krill: check-src $(KRILL_PREREQS)
165
Marc Kupietz19fa4482026-06-07 15:03:56 +0200166# Build just the stand-off metadata layers without indexing.
167meta: check-src $(foreach base,$(BASENAMES),$(foreach ann,$(ACTIVE_META),$(BUILD_DIR)/$(base).$(ann).meta.xml))
168
169# The base zip, every annotation zip, and every stand-off meta XML are folded into
170# one Krill tar. korapxmltool auto-detects the .meta.xml stand-off inputs.
Marc Kupietzb820ece2026-06-11 14:29:05 +0200171$(BUILD_DIR)/%.krill.tar: $(BUILD_DIR)/%.zip $(foreach ann,$(ANNOTATIONS),$(call artifact,%,$(ann))) | bin/korapxmltool
Marc Kupietz19fa4482026-06-07 15:03:56 +0200172 $(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 +0100173
Marc Kupietz463da332026-03-08 13:19:15 +0100174krill: $(foreach base,$(BASENAMES),$(BUILD_DIR)/$(base).krill.tar)
175
Marc Kupietz15b313d2025-12-11 12:20:56 +0100176$(TARGET_DIR)/index: $(foreach base,$(BASENAMES),$(BUILD_DIR)/$(base).krill.tar)
177 make lib/Krill-Indexer.jar
178 touch lib/krill.cfg
179 mkdir -p $(TARGET_DIR)
180 java -jar lib/Krill-Indexer.jar -c lib/krill.cfg --progress -i $(subst " ",;,$^) -o $@
181
182korap: check-src $(TARGET_DIR)/index
Marc Kupietze7807322026-06-05 08:55:41 +0200183 curl -s https://raw.githubusercontent.com/KorAP/KorAP-Docker/master/compose.yaml | sed 's/64543:64543/$(KORAP_PORT):64543/g' | COMPOSE_PROFILES='export' INDEX='$(TARGET_DIR)/index' docker compose -p korap -f - --profile=lite --profile=example up
Marc Kupietz89a44072025-12-11 07:14:00 +0100184
185$(TARGET_DIR)/index.tar.xz: $(TARGET_DIR)/index
186 tar -I 'xz -T0' -C $(dir $<) -cf $@ $(notdir $<)
187
188clean:
189 rm -rf $(BUILD_DIR) $(TARGET_DIR)