Views
No views yet
1import pickle
2
3with open("model.pkl", "rb") as f:
4 model = pickle.load(f)
5
6def extract_features(tokens, idx):
7 token = tokens[idx]
8 features = {
9 'word.lower': token.lower(),
10 'word.length': len(token),
11 'word.has_at': '@' in token,
12 'word.is_digit': token.isdigit(),
13 }
14 if idx > 0: features['prev.lower'] = tokens[idx-1].lower()
15 if idx < len(tokens)-1: features['next.lower'] = tokens[idx+1].lower()
16 return features
17
18text = "Contact jane@test.com or call 555-123-4567"
19tokens = text.split()
20features = [extract_features(tokens, i) for i in range(len(tokens))]
21predictions = model.predict(features)
22entities = [(t, l) for t, l in zip(tokens, predictions) if l != "O"]