Views
No views yet
B-TIMEX, I-TIMEX, O1pip install transformers datasets evaluate
2
3# Loading the Fine-Tuned Model
4
5from transformers import RobertaTokenizerFast, RobertaForTokenClassification
6import torch
7
8# Load model and tokenizer
9model = RobertaForTokenClassification.from_pretrained("./temporal_model")
10tokenizer = RobertaTokenizerFast.from_pretrained("./temporal_model", add_prefix_space=True)
11
12# Inference function
13def extract_temporal_entities(text):
14 tokens = text.split()
15 inputs = tokenizer(tokens, return_tensors="pt", is_split_into_words=True)
16 outputs = model(**inputs)
17 predictions = outputs.logits.argmax(dim=-1).squeeze().tolist()
18 word_ids = inputs.word_ids()[0]
19
20 temporal_spans = []
21 current = []
22 for idx, word_idx in enumerate(word_ids):
23 if word_idx is None:
24 continue
25 label = id2label[predictions[idx]]
26 if label == "B-TIMEX":
27 if current:
28 temporal_spans.append(" ".join(current))
29 current = [tokens[word_idx]]
30 elif label == "I-TIMEX":
31 current.append(tokens[word_idx])
32 else:
33 if current:
34 temporal_spans.append(" ".join(current))
35 current = []
36 if current:
37 temporal_spans.append(" ".join(current))
38 return temporal_spans
39
40
41# Performance Metrics
42Evaluation Accuracy: ~0.76
43
44F1 Score: Tracked using seqeval (BIO format)
45
46Evaluation Strategy: epoch
47
48# Fine-Tuning Details
49Dataset
50The dataset consists of manually or script-labeled SPO-style JSON entries with the following fields:
51
52text: Raw input string
53
54spo_list: A list of subject-predicate-object relations, including:
55
56Subject & Object Span
57
58Type (e.g., Date, Location)
59
60The text is tokenized, and BIO labels are applied for token classification.
61
62# Training Configuration
63
64Epochs: 3
65
66Batch Size: 16
67
68Learning Rate: 2e-5
69
70Max Sequence Length: 128 tokens
71
72Tokenizer: roberta-base with add_prefix_space=True
73
74# Repository Structure
75pgsql
76Copy
77Edit
78.
79├── temporal_model/ # Fine-tuned model and tokenizer
80│ ├── config.json
81│ ├── pytorch_model.bin
82│ ├── tokenizer_config.json
83│ ├── vocab.json
84│ └── special_tokens_map.json
85├── temporal-information-extraction.ipynb
86├── README.md
87
88# Limitations
89
90The model is domain-specific; generalization to other types of temporal expressions (e.g., informal text) may require additional training.
91
92BIO tagging may fail in overlapping or nested entity scenarios.
93
94# Contributing
95Contributions are welcome! Feel free to open an issue or submit a pull request to improve model performance or add new datasets.
96