Views
No views yet
| Model | Precision | Recall | F1 Score |
|---|---|---|---|
| specter2_base | 0.57 | 0.61 | 0.57 |
| modernBERT | 0.45 | 0.42 | 0.41 |
| BERT-base | 0.53 | 0.57 | 0.52 |
1from ipymarkup import show_span_box_markup
2from transformers import pipeline
3ner = pipeline("ner", model="nicolauduran45/specter-climate-change-NER", tokenizer="nicolauduran45/specter-climate-change-NER", aggregation_strategy="simple", device=0)
4
5text = 'multi-centennial variability of open ocean deep convection in the Atlantic sector of the Southern Ocean impacts the strength of the Atlantic Meridional Overturning Circulation (AMOC) in the Kiel Climate Model. The northward extent of Antarctic Bottom Water (AABW) strongly depends on the state of Weddell Sea deep convection.'
6
7entities = ner(ex)
8spans = [(s['start'], s['end'], s['entity_group'])for s in entities]
9show_span_box_markup(text, spans)1def predict_with_proper_aggregation(text):
2 # Get the raw predictions
3 raw_entities = ner(text)
4
5 # Aggregate subword pieces into complete entities
6 aggregated_entities = []
7 current_entity = None
8
9 for entity in raw_entities:
10 # Check if this is a continuation token (starts with ##)
11 is_continuation = entity["word"].startswith("##")
12
13 if is_continuation and current_entity:
14 # Update the current entity by removing ## and appending
15 current_entity["word"] += entity["word"][2:]
16 current_entity["end"] = entity["end"]
17
18 # Update the score (average or keep the minimum)
19 current_entity["score"] = min(current_entity["score"], entity["score"])
20
21 # If entity types differ, use the one with higher confidence
22 if entity["entity_group"] != current_entity["entity_group"] and entity["score"] > current_entity["score"]:
23 current_entity["entity_group"] = entity["entity_group"]
24 current_entity["score"] = entity["score"]
25 else:
26 # If we have a previous entity, add it to results
27 if current_entity:
28 aggregated_entities.append(current_entity)
29
30 # Start a new entity
31 current_entity = entity.copy()
32
33 # Don't forget the last entity
34 if current_entity:
35 aggregated_entities.append(current_entity)
36
37 # Further aggregation: detect split entities that might not use ## notation
38 # but should be merged based on adjacent positions
39 i = 0
40 while i < len(aggregated_entities) - 1:
41 current = aggregated_entities[i]
42 next_entity = aggregated_entities[i + 1]
43
44 # Check if entities are adjacent and should be merged
45 if (current["end"] == next_entity["start"] and
46 current["entity_group"] == next_entity["entity_group"]):
47 # Merge entities
48 current["word"] += next_entity["word"]
49 current["end"] = next_entity["end"]
50 current["score"] = (current["score"] + next_entity["score"]) / 2
51 # Remove the next entity as it's now merged
52 aggregated_entities.pop(i + 1)
53 else:
54 i += 1
55
56 return aggregated_entities
57
58entities = predict_with_proper_aggregation(ex)
59spans = [(s['start'], s['end'], s['entity_group'])for s in entities]
60show_span_box_markup(text, spans)