[liblouis-liblouisxml] Re: Here's the Python formatter

  • From: Jake Kyle <jake@xxxxxxxxxxxxxxxxxx>
  • To: liblouis-liblouisxml@xxxxxxxxxxxxx
  • Date: Mon, 21 Dec 2020 10:35:30 +0000

Hi Christian

Of course - go ahead.

I have just given a quick example using lxml.html. Similar scripts can be written for XML in general using lxml.etree. Also there is much fine grained control possible by selecting different elements and attributes more specifically, using xpath and other methods available within lxml, to do different things.

Jake

On 21/12/2020 10:21, Christian Egli wrote:

Hi Jake

I like the example that you provide. Do you mind if I add this to the
examples in the python directory of the liblouis distribution? Can I use
the a very liberal BSD-style license?

There is already another example there that extends the xslt processor
ith liblouis for more fine grained control on how to translate different
parts of the XML document, see
https://github.com/liblouis/liblouis/blob/master/python/examples/liblouisxslt.py

Thanks
Christian

Jake Kyle writes:

Hi Devin

If you want to do it within Python you need an HTML parser so that you
can just translate the text nodes. For example lxml.html.

Here is a script using this:

import textwrap
import louis
from lxmlimport html

tableList = ["en-ueb-g2.ctb"]
lineLength =38 fileIn =input("Please enter the input file name: ")
fileOut =input("Please enter the output file name: ")
with open(fileOut,"w")as outputFile:
     html_root = html.parse(fileIn).getroot()
     for head_or_bodyin html_root:
         for elemin head_or_body:
             if elem.xpath("string()").strip() !="":
                 line = elem.xpath("string()")
                 translation = louis.translateString(tableList, line,0,0)
                 outputFile.write(textwrap.fill(translation, lineLength))
                 outputFile.write("\n")
print ("Done.")


Jake

On 19/12/2020 12:50, Lars Bjørndal wrote:
Hi!

You may try out file2brl from liblouisutdml, found here: 
https://github.com/liblouis/liblouisutdml

File2brl can convert from XML/HTML to formatted braille.

Lars

On Sat, Dec 19, 2020 at 12:08:31AM -0600, Devin Prater wrote:
One problem I had with this, is that it seems to only convert plain text. I
tried it with an HTML file, and it just converted the HTML code into
braille, not converting the HTML formatting into formatted braille. I
planned to use this to emboss stuff from Linux, as the translator that
cups-filters is trying to use doesn't seem to be working.

On 12/18/20 2:27 AM, John J. Boyer wrote:
Hello Michael,

Thanks for the list example. I'll probably do something like that. I still have 
to study comprehension.

John

On Fri, Dec 18, 2020 at 07:46:19AM +0000, Michael Whapples wrote:
Hello,

I left out the docstring to keep the email short and as it has little to do
with the functioning of the code. Of course you can add it back in and that
may be a good plan.


The "with" statements are much clearer than the associated
try/except/finally blocks would be for similar reliability of closing the
files. The for iteration of the lines I think is clearer than a while loop
with an if and break. However its your code so do what you think you
understand more.


As for joining many strings, I think using a list and then the str.join
method is the way to go.


str_list = []

str_list.append("First line")

str_list.append("Second line")

long_string = "\n".join(str_list)


I believe this may be even quicker if it can be done with a list
comprehension.


Michael Whapples

On 18/12/2020 03:30, John J. Boyer wrote:
Hello Michael,

Thanks for the suggestions. However, I felt the program was much easier to 
understand as I wrote it, without so many Pyth-y features.
I also wanted to make it self-contained with a doc string at the beginning.

Now I'm trying to find a way to combine many short strings which are lines from 
the input file into a long string that approximates a paragraph.
This would be passed to translateString to produce a more readable outpout. The 
hard part is to do this efficiently, without a lot of garbage collection.

John

For a description of the software, to download it and links to
project pages go to http://liblouis.org
Donate: http://liblouis.org/sponsoring
For a description of the software, to download it and links to
project pages go to http://liblouis.org
Donate: http://liblouis.org/sponsoring
For a description of the software, to download it and links to
project pages go to http://liblouis.org
Donate: http://liblouis.org/sponsoring

--
Christian Egli
Swiss Library for the Blind, Visually Impaired and Print Disabled
Grubenstrasse 12, CH-8045 Zürich, Switzerland
For a description of the software, to download it and links to
project pages go to http://liblouis.org
Donate: http://liblouis.org/sponsoring

For a description of the software, to download it and links to
project pages go to http://liblouis.org
Donate: http://liblouis.org/sponsoring

Other related posts: