DOM-LM extends RoBERTa-Base with structure-aware position embeddings that encode the DOM tree (node index, parent node index, depth, sibling index, HTML tag) and pre-trains with a masked language modeling objective on HTML documents.
1pip install lxml "transformers>=4.26"
2git clone https://github.com/LahadMbacke/DOM-LM.git
1import sys, torch
2sys.path.insert(0, "DOM-LM/src")
3
4from transformers import AutoModelForMaskedLM
5from src.preprocess import extract_features
6from domlm.configuration_domlm import DOMLMConfig
7
8model = AutoModelForMaskedLM.from_pretrained("Lahad/dom-lm-pretrained", trust_remote_code=True)
9config = DOMLMConfig.from_pretrained("Lahad/dom-lm-pretrained", trust_remote_code=True)
10
11html = open("page.html").read() # raw HTML string
12subtrees = extract_features(html, config)
13
14# each subtree is a dict with input_ids, attention_mask,
15# node_ids, parent_node_ids, sibling_node_ids, depth_ids, tag_ids
16batch = {k: torch.tensor([subtrees[0][k]]) for k in subtrees[0]}
17outputs = model(**batch)
18# outputs.last_hidden_state → (1, seq_len, 768)
The model was pre-trained with 5 structural tensors encoding the DOM tree
(node index, parent index, sibling rank, depth, HTML tag). Passing only
input_ids silently falls back to padding values — the learned structural
embeddings are present in the weights but unused. Always go through
extract_features to get representations that reflect both text and structure.
1from transformers import AutoTokenizer
2tokenizer = AutoTokenizer.from_pretrained("roberta-base")
1@article{deng2022domlm,
2 title={DOM-LM: Learning Generalizable Representations for HTML Documents},
3 author={Deng, Xiang and Shiralkar, Prashant and Lockard, Colin and Huang, Binxuan and Sun, Huan},
4 journal={arXiv preprint arXiv:2201.10608},
5 year={2022}
6}