Is there a way of modifying pretty_print output so that it prints the lowest element nodes without any indentation and every other element name with just one tab stop, like the following: <l xml:id="A15310-e90"> <w lemma="to" pos="acp-p" reg="TO" xml:id="A15310-000450">TO</w> <w lemma="call" pos="vvi" reg="call" xml:id="A15310-000460">call</w> <w lemma="Clio" pos="n1-nn" reg="Clio" xml:id="A15310-000470">Clio</w> <w lemma="my" pos="po" reg="my" xml:id="A15310-000480">my</w> <w lemma="dulness" pos="n1" reg="dulness" xml:id="A15310-000490">dulnesse</w> <w lemma="to" pos="acp-cs" reg="to" xml:id="A15310-000500">to</w> <w lemma="redress" pos="vvi" reg="redress" xml:id="A15310-000510">redresse</w> </l> In my project—an enrichment of the 60,000 English books printed before 1700 and transcribed by the Text Creation partnership—every word or punctuation mark is wrapped in a <w> or <pc> element. Since the texts are long and the encoding is very verbose, there isn’t much point to using tab stops or spaces to express the position of an element in the hierarchy. On the other hand, there is value in having no indentation for <w> elements because they will in most cases fit in one line. It’s often necessary to look at output on a screen, and for the purpose of this project, this form of pretty-printing is easier to read. It also saves a lot of space: output formatted with lxml pretty_ print takes up 88 GB. With my form of “outdenting” it’s 67GB. Space is cheap, but not that cheap. So I need a formatting device with the following pseudocode: If element.tag is ‘w’ or ‘pc’: begin a new line without indentation else: indent by one tab stop I know how to write a postprocessing script that treats the pretty_printed file as a plain text file and use regulars expression to replace ‘\n\s*(<w|<pc)’ with ‘\n$1’. There must be a better way than this kludge, and I’ll be grateful for help.
Am .04.2017, 16:59 Uhr, schrieb Martin Mueller <martinmueller@northwestern.edu>:
I know how to write a postprocessing script that treats the pretty_printed file as a plain text file and use regulars expression to replace ‘\n\s*(<w|<pc)’ with ‘\n$1’. There must be a better way than this kludge, and I’ll be grateful for help.
I don't know about lxml but you might be able to create a custom class for this for Python's own pprint module. Charlie -- Charlie Clark Managing Director Clark Consulting & Research German Office Kronenstr. 27a Düsseldorf D- 40217 Tel: +49-211-600-3657 Mobile: +49-178-782-6226
Martin Mueller schrieb am 16.04.2017 um 16:59:
Is there a way of modifying pretty_print output so that it prints the lowest element nodes without any indentation and every other element name with just one tab stop, like the following:
<l xml:id="A15310-e90"> <w lemma="to" pos="acp-p" reg="TO" xml:id="A15310-000450">TO</w> <w lemma="call" pos="vvi" reg="call" xml:id="A15310-000460">call</w> <w lemma="Clio" pos="n1-nn" reg="Clio" xml:id="A15310-000470">Clio</w> <w lemma="my" pos="po" reg="my" xml:id="A15310-000480">my</w> <w lemma="dulness" pos="n1" reg="dulness" xml:id="A15310-000490">dulnesse</w> <w lemma="to" pos="acp-cs" reg="to" xml:id="A15310-000500">to</w> <w lemma="redress" pos="vvi" reg="redress" xml:id="A15310-000510">redresse</w> </l>
In my project—an enrichment of the 60,000 English books printed before 1700 and transcribed by the Text Creation partnership—every word or punctuation mark is wrapped in a <w> or <pc> element. Since the texts are long and the encoding is very verbose, there isn’t much point to using tab stops or spaces to express the position of an element in the hierarchy. On the other hand, there is value in having no indentation for <w> elements because they will in most cases fit in one line. It’s often necessary to look at output on a screen, and for the purpose of this project, this form of pretty-printing is easier to read. It also saves a lot of space: output formatted with lxml pretty_ print takes up 88 GB. With my form of “outdenting” it’s 67GB. Space is cheap, but not that cheap.
So I need a formatting device with the following pseudocode:
If element.tag is ‘w’ or ‘pc’: begin a new line without indentation else: indent by one tab stop
I know how to write a postprocessing script that treats the pretty_printed file as a plain text file and use regulars expression to replace ‘\n\s*(<w|<pc)’ with ‘\n$1’. There must be a better way than this kludge, and I’ll be grateful for help.
Indenting XML simply means setting the .tail text attribute to some whitespace value. You might be able to adapt this plain-Python recipe for your needs: http://effbot.org/zone/element-lib.htm#prettyprint Stefan
participants (3)
-
Charlie Clark -
Martin Mueller -
Stefan Behnel