inference¶
topic_segmentation.inference
Segment sentence JSONL files with ModernBERT or Chonkie.
python -m topic_segmentation.inference --input data/news/test.jsonl
python -m topic_segmentation.inference --model-type chonkie_semantic --input data/news/test.jsonl
Input rows contain sentences and optional articleId and labels fields. Output rows contain articleId, sentences, and predictions: 1 marks a boundary after a sentence, including the final sentence. Labeled inputs also receive precision, recall, F1, Pk, and WindowDiff scores.
unit_tokens(tokenizer, units)
¶
Tokenize each unit with a leading BOS; return token IDs and BOS offsets.
Source code in src/topic_segmentation/inference.py
31 32 33 34 35 36 37 38 | |
windows(starts, total, window, stride, protect_window_end)
¶
Yield (start, end, scored_units), with unit positions relative to the prepended CLS.
With end protection, keep the last unit as context and score it in the next window. A unit longer than a window is scored from its visible prefix.
Source code in src/topic_segmentation/inference.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | |
center_weight(rank, count)
¶
Weight a unit by its rank in the window, with the highest weight at the center.
Source code in src/topic_segmentation/inference.py
64 65 66 | |
ModernBertSegmenter
¶
Segment articles with ModernBERT using weighted sliding windows.
Source code in src/topic_segmentation/inference.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
model = ModernBert.from_pretrained(model_dir, ignore_mismatched_sizes=True).eval().to(device)
instance-attribute
¶
tokenizer = AutoTokenizer.from_pretrained(model_dir)
instance-attribute
¶
add_margins(batch, margins, weights)
¶
Accumulate center-weighted boundary logit margins for each unit.
Source code in src/topic_segmentation/inference.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 | |
segment_batch(articles)
¶
Segment articles by batching their token windows.
Source code in src/topic_segmentation/inference.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
Chonkie
¶
Map Chonkie chunk ends to sentence boundaries.
Source code in src/topic_segmentation/inference.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | |
chunker = SemanticChunker(embedding_model=Model2VecEmbeddings(path), threshold=0.8)
instance-attribute
¶
segment(sentences)
¶
Source code in src/topic_segmentation/inference.py
132 133 134 135 136 137 138 139 140 141 142 | |
segment_batch(articles)
¶
Source code in src/topic_segmentation/inference.py
144 145 | |
main()
¶
Source code in src/topic_segmentation/inference.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |