English
SpEL
Entity Linking
Structured Prediction
Hassan Shavarani commited on
Commit
68f0c2d
1 Parent(s): 7bbcb81

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +43 -0
README.md CHANGED
@@ -17,6 +17,49 @@ SpEL model finetuned on English Wikipedia as well as the training portion of CoN
17
  It is introduced in the paper [SPEL: Structured Prediction for Entity Linking (EMNLP 2023)](https://arxiv.org/abs/2310.14684).
18
  The code and data are available in [this repository](https://github.com/shavarani/SpEL).
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  ## Evaluation Results
21
  Entity Linking evaluation results of *SpEL* compared to that of the literature over AIDA test sets:
22
 
 
17
  It is introduced in the paper [SPEL: Structured Prediction for Entity Linking (EMNLP 2023)](https://arxiv.org/abs/2310.14684).
18
  The code and data are available in [this repository](https://github.com/shavarani/SpEL).
19
 
20
+ ### Usage
21
+ The following snippet demonstrates a quick way that SpEL can be used to generate subword-level, word-level, and phrase-level annotations for a sentence.
22
+
23
+ ```python
24
+ # download SpEL from https://github.com/shavarani/SpEL
25
+ from transformers import AutoTokenizer
26
+ from spel.model import SpELAnnotator
27
+ from spel.configuration import device
28
+ from spel.utils import get_subword_to_word_mapping
29
+ from spel.span_annotation import WordAnnotation, PhraseAnnotation
30
+ finetuned_after_step = 4
31
+ sentence = "Grace Kelly by Mika reached the top of the UK Singles Chart in 2007."
32
+ tokenizer = AutoTokenizer.from_pretrained("roberta-base")
33
+ # ############################################# LOAD SpEL #############################################################
34
+ spel = SpELAnnotator()
35
+ spel.init_model_from_scratch(device=device)
36
+ if finetuned_after_step == 3:
37
+ spel.shrink_classification_head_to_aida(device)
38
+ spel.load_checkpoint(None, device=device, load_from_torch_hub=True, finetuned_after_step=finetuned_after_step)
39
+ # ############################################# RUN SpEL ##############################################################
40
+ inputs = tokenizer(sentence, return_tensors="pt")
41
+ token_offsets = list(zip(inputs.encodings[0].tokens,inputs.encodings[0].offsets))
42
+ subword_annotations = spel.annotate_subword_ids(inputs.input_ids, k_for_top_k_to_keep=10, token_offsets=token_offsets)
43
+ # #################################### CREATE WORD-LEVEL ANNOTATIONS ##################################################
44
+ tokens_offsets = token_offsets[1:-1]
45
+ subword_annotations = subword_annotations[1:]
46
+ word_annotations = [WordAnnotation(subword_annotations[m[0]:m[1]], tokens_offsets[m[0]:m[1]])
47
+ for m in get_subword_to_word_mapping(inputs.tokens(), sentence)]
48
+ # ################################## CREATE PHRASE-LEVEL ANNOTATIONS ##################################################
49
+ phrase_annotations = []
50
+ for w in word_annotations:
51
+ if not w.annotations:
52
+ continue
53
+ if phrase_annotations and phrase_annotations[-1].resolved_annotation == w.resolved_annotation:
54
+ phrase_annotations[-1].add(w)
55
+ else:
56
+ phrase_annotations.append(PhraseAnnotation(w))
57
+ ```
58
+
59
+
60
+
61
+
62
+
63
  ## Evaluation Results
64
  Entity Linking evaluation results of *SpEL* compared to that of the literature over AIDA test sets:
65