Views
No views yet
| Label | Description |
|---|---|
| RUA | Street / Avenue / Road name |
| NUMERO | Street number |
| BAIRRO | Neighborhood |
| CIDADE | City |
| ESTADO | State (UF) |
| CEP | ZIP code |
| COMPLEMENTO | Address complement (apartment, block, lot, etc.) |
| REFERENCIA | Reference point / landmark |
| Entity | Precision | Recall | F1 |
|---|---|---|---|
| RUA | 1.0000 | 1.0000 | 1.0000 |
| NUMERO | 1.0000 | 1.0000 | 1.0000 |
| BAIRRO | 1.0000 | 1.0000 | 1.0000 |
| CIDADE | 1.0000 | 1.0000 | 1.0000 |
| ESTADO | 1.0000 | 1.0000 | 1.0000 |
| CEP | 1.0000 | 1.0000 | 1.0000 |
| COMPLEMENTO | 0.8571 | 0.6000 | 0.7059 |
| REFERENCIA | 0.8182 | 0.9000 | 0.8571 |
| Overall | 0.9744 | 0.9580 | 0.9661 |
1from transformers import AutoTokenizer, AutoModelForTokenClassification
2import torch
3
4tokenizer = AutoTokenizer.from_pretrained("ottema/bert-addresses-brazil")
5model = AutoModelForTokenClassification.from_pretrained("ottema/bert-addresses-brazil")
6
7text = "Rua das Flores 123, Apto 402, Centro, Sao Paulo - SP. CEP 01310-100"
8
9encoding = tokenizer(text, return_tensors="pt", return_offsets_mapping=True, truncation=True, max_length=128)
10offsets = encoding["offset_mapping"][0].tolist()
11
12with torch.no_grad():
13 logits = model(input_ids=encoding["input_ids"], attention_mask=encoding["attention_mask"]).logits
14 preds = torch.argmax(logits, dim=-1)[0].tolist()
15
16id2label = model.config.id2label
17entities = []
18current_type = None
19current_start = None
20current_end = None
21
22for pred, (start, end) in zip(preds, offsets):
23 if start == end:
24 continue
25 label = id2label[str(pred)]
26 if label.startswith("B-"):
27 if current_type:
28 entities.append((current_type, text[current_start:current_end].strip()))
29 current_type = label[2:]
30 current_start = start
31 current_end = end
32 elif label.startswith("I-") and current_type == label[2:]:
33 current_end = end
34 else:
35 if current_type:
36 entities.append((current_type, text[current_start:current_end].strip()))
37 current_type = None
38
39if current_type:
40 entities.append((current_type, text[current_start:current_end].strip()))
41
42for entity_type, value in entities:
43 print(f"{entity_type}: {value}")RUA: Rua das Flores
NUMERO: 123
COMPLEMENTO: Apto 402
BAIRRO: Centro
CIDADE: Sao Paulo
ESTADO: SP
CEP: 01310-100