Views
No views yet
1{
2 0: "O",
3 1: "VEHICLEVRM",
4 2: "HEIGHT",
5 3: "USERNAME",
6 4: "FIRSTNAME",
7 5: "BUILDINGNUMBER",
8 6: "SEX",
9 7: "PHONENUMBER",
10 8: "CURRENCY",
11 9: "CREDITCARDISSUER",
12 10: "CURRENCYNAME",
13 11: "MAC",
14 12: "MIDDLENAME",
15 13: "TIME",
16 14: "EYECOLOR",
17 15: "CURRENCYSYMBOL",
18 16: "GENDER",
19 17: "URL",
20 18: "CURRENCYCODE",
21 19: "ZIPCODE",
22 20: "CREDITCARDCVV",
23 21: "JOBTITLE",
24 22: "PHONEIMEI",
25 23: "COUNTY",
26 24: "JOBTYPE",
27 25: "LITECOINADDRESS",
28 26: "COMPANYNAME",
29 27: "ORDINALDIRECTION",
30 28: "MASKEDNUMBER",
31 29: "USERAGENT",
32 30: "LASTNAME",
33 31: "SSN",
34 32: "STREET",
35 33: "SECONDARYADDRESS",
36 34: "STATE",
37 35: "ETHEREUMADDRESS",
38 36: "AMOUNT",
39 37: "ACCOUNTNUMBER",
40 38: "CITY",
41 39: "CREDITCARDNUMBER",
42 40: "BIC",
43 41: "EMAIL",
44 42: "NEARBYGPSCOORDINATE",
45 43: "PIN",
46 44: "ACCOUNTNAME",
47 45: "VEHICLEVIN",
48 46: "PREFIX",
49 47: "JOBAREA",
50 48: "AGE",
51 49: "PASSWORD",
52 50: "DOB",
53 51: "BITCOINADDRESS",
54 52: "IBAN",
55 53: "IP",
56 54: "DATE"
57}1import torch
2from transformers import AutoTokenizer, AutoModelForTokenClassification
3
4model_id = "LocalDoc/private_ner_azerbaijani"
5
6tokenizer = AutoTokenizer.from_pretrained(model_id)
7model = AutoModelForTokenClassification.from_pretrained(model_id)
8
9test_text = (
10 "Salam, mənim adım Əli Hüseynovdur. Doğum tarixim 15.05.1990-dır. Bakı şəhərində, Nizami küçəsində, 25/31 ünvanında yaşayıram. Telefon nömrəm +994552345678-dir."
11)
12
13inputs = tokenizer(test_text, return_tensors="pt", return_offsets_mapping=True)
14
15offset_mapping = inputs.pop("offset_mapping")
16
17with torch.no_grad():
18 outputs = model(**inputs)
19
20predictions = torch.argmax(outputs.logits, dim=2)
21
22tokens = tokenizer.convert_ids_to_tokens(inputs["input_ids"][0])
23offset_mapping = offset_mapping[0].tolist()
24predicted_labels = [model.config.id2label[pred.item()] for pred in predictions[0]]
25word_ids = inputs.word_ids(batch_index=0)
26
27aggregated = []
28prev_word_id = None
29for idx, word_id in enumerate(word_ids):
30 if word_id is None:
31 continue
32 if word_id != prev_word_id:
33 aggregated.append({
34 "word_id": word_id,
35 "tokens": [tokens[idx]],
36 "offsets": [offset_mapping[idx]],
37 "label": predicted_labels[idx]
38 })
39 else:
40 aggregated[-1]["tokens"].append(tokens[idx])
41 aggregated[-1]["offsets"].append(offset_mapping[idx])
42 prev_word_id = word_id
43
44entities = []
45current_entity = None
46for word in aggregated:
47 if word["label"] == "O":
48 if current_entity is not None:
49 entities.append(current_entity)
50 current_entity = None
51 else:
52 if current_entity is None:
53 current_entity = {
54 "type": word["label"],
55 "start": word["offsets"][0][0],
56 "end": word["offsets"][-1][1]
57 }
58 else:
59 if word["label"] == current_entity["type"]:
60 current_entity["end"] = word["offsets"][-1][1]
61 else:
62 entities.append(current_entity)
63 current_entity = {
64 "type": word["label"],
65 "start": word["offsets"][0][0],
66 "end": word["offsets"][-1][1]
67 }
68if current_entity is not None:
69 entities.append(current_entity)
70
71for entity in entities:
72 entity["text"] = test_text[entity["start"]:entity["end"]]
73
74for entity in entities:
75 print(entity)1{'type': 'FIRSTNAME', 'start': 18, 'end': 21, 'text': 'Əli'}
2{'type': 'LASTNAME', 'start': 22, 'end': 34, 'text': 'Hüseynovdur.'}
3{'type': 'DOB', 'start': 49, 'end': 64, 'text': '15.05.1990-dır.'}
4{'type': 'STREET', 'start': 81, 'end': 87, 'text': 'Nizami'}
5{'type': 'BUILDINGNUMBER', 'start': 99, 'end': 104, 'text': '25/31'}
6{'type': 'PHONENUMBER', 'start': 141, 'end': 159, 'text': '+994552345678-dir.'}Attribution: You must give appropriate credit, provide a link to the license, and indicate if changes were made.
Non-Commercial: You may not use the material for commercial purposes.
No Derivatives: If you remix, transform, or build upon the material, you may not distribute the modified material.