1. A method comprising: comparing a structure of at least a portion of a document with a structure of an initial template; in response to determining a mismatch between the structure of the portion of the document and the structure of the initial template, determining a cost of generalizing the structure of the initial template to create a generalized template that has a structure that: (a) will match the portion of the document, and (b) will match any structure that the structure of the initial template would match; and if the cost is less than a specified threshold, then creating the generalized template by generalizing the structure of the initial template to match the structure of the portion of the document.