Xaver Maria Krückl commited on
Commit
6d77bb0
·
verified ·
1 Parent(s): 2aaa212

updated prediction

Browse files
Files changed (1) hide show
  1. README.md +5 -12
README.md CHANGED
@@ -94,16 +94,13 @@ from collections import defaultdict
94
  nlp = spacy.load("en_core_web_sm")
95
 
96
  def predict_text(text: str):
97
- """
98
- Segment `text` into sentences with spaCy (matching training setup),
99
- predict BIO tags per sentence, and return a flat list of (word, tag) pairs.
100
- """
101
  id2label = config.id2label
102
  results = []
103
 
104
  for sent in nlp(text).sents:
 
105
  encoding = tokenizer(
106
- sent.text, return_tensors="pt",
107
  truncation=True, max_length=512,
108
  )
109
 
@@ -112,10 +109,6 @@ def predict_text(text: str):
112
  encoding["input_ids"], encoding["attention_mask"]
113
  )[0]
114
 
115
- # During training only the *first* sub-token of each word received a
116
- # real label; subsequent sub-tokens were masked (-100 → O by the CRF).
117
- # Collect all sub-token ids per word so punctuation attached to a word
118
- # (e.g. "mat.") is decoded together with its root token.
119
  word_ids = encoding.word_ids(batch_index=0)
120
  word_token_ids = defaultdict(list)
121
  for pos, word_idx in enumerate(word_ids):
@@ -127,10 +120,10 @@ def predict_text(text: str):
127
  prev_word_idx = None
128
  for pos, word_idx in enumerate(word_ids):
129
  if word_idx is None:
130
- continue # skip [CLS] / [SEP]
131
  if word_idx != prev_word_idx:
132
  word = tokenizer.decode(word_token_ids[word_idx]).strip()
133
- results.append((word, id2label[raw_tag_ids[pos].item()]))
134
  prev_word_idx = word_idx
135
 
136
  return results
@@ -141,7 +134,7 @@ for word, tag in predict_text(text):
141
  print(f"{word:20s} {tag}")
142
  ```
143
 
144
- > **Note:** The model was trained on spaCy-segmented sentences (`en_core_web_sm`), so inference should follow the same setup — predict one sentence at a time. `B-EDU` marks the start of a new situation-entity segment; `I-EDU` marks its continuation; `O` marks tokens outside any segment.
145
 
146
  ## Limitations
147
 
 
94
  nlp = spacy.load("en_core_web_sm")
95
 
96
  def predict_text(text: str):
 
 
 
 
97
  id2label = config.id2label
98
  results = []
99
 
100
  for sent in nlp(text).sents:
101
+ spacy_tokens = [token.text for token in sent]
102
  encoding = tokenizer(
103
+ spacy_tokens, return_tensors="pt", is_split_into_words=True,
104
  truncation=True, max_length=512,
105
  )
106
 
 
109
  encoding["input_ids"], encoding["attention_mask"]
110
  )[0]
111
 
 
 
 
 
112
  word_ids = encoding.word_ids(batch_index=0)
113
  word_token_ids = defaultdict(list)
114
  for pos, word_idx in enumerate(word_ids):
 
120
  prev_word_idx = None
121
  for pos, word_idx in enumerate(word_ids):
122
  if word_idx is None:
123
+ continue
124
  if word_idx != prev_word_idx:
125
  word = tokenizer.decode(word_token_ids[word_idx]).strip()
126
+ results.append((word, id2label[raw_tag_ids[pos]]))
127
  prev_word_idx = word_idx
128
 
129
  return results
 
134
  print(f"{word:20s} {tag}")
135
  ```
136
 
137
+ > **Note:** The model was trained on spaCy-tokenized sentences (`en_core_web_sm`), so inference must use the same setup — pass spaCy tokens via `is_split_into_words=True` and predict one sentence at a time. `B-EDU` marks the start of a new situation-entity segment; `I-EDU` marks its continuation; `O` marks tokens outside any segment.
138
 
139
  ## Limitations
140