Skip to content
Snippets Groups Projects

Appeler unlTools en python (jar ou webservice)

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by David Beniamine
    Edited
    unltools_jar.py 1.12 KiB
    def convertUnl(unl, outputs, path):
        '''
        Using unlTools jar, available for instance here : https://gitlab.tetras-libre.fr/unl/unlTools/-/releases
        Converts a unl string to rdf, dot or svg, returns a dictionnary containing results
                Parameters:
                        unlFilePath (string): The unl input file path
                        outType (string): the type of output in ['dot', 'rdf']
                        jarPath (string): path to unlTools jar
                        outPrefix (string): path to desired outfile, without filetype suffix
    
                Returns:
                        nothing but create the desired file under outPrefix+outType
        '''
        def unl2stuff(unlFilePath, jarPath, outPrefix, outType):
        # Run java parser
        cmd = ['java', '-jar', jarPath,
               '--input-file', unlFilePath,
               '--output-file', outPrefix,
               '--output-type', outType]
        with Popen(cmd, stdout=PIPE, stderr=STDOUT) as p:
            p.wait()
            p.stdout.flush()
            if p.returncode != 0:
                print("Error in unl2rdf: \n\n"+p.stdout.read().decode())
                print('UNL;')
                print(text)
    
    unltools_webservice.py 1.25 KiB
    
    import re
    import requests
    import html
    
    def convertUnl(unldata) :
        '''
        Using unlToolsdemo  webservice, available for instance here : https://unl.demo.tetras-libre.fr/unl2rdf
        Converts a unl string to rdf, dot or svg, returns a dictionnary tuple
                Parameters:
                        unldata (string): The unl input
                    
                Returns:
                        res (dictionnary): {'rdf': ...}
        '''
        
        data={'unl': unldata, 'outputs':['dot', 'svg', 'rdf']}
    
        try:
            r = requests.post('https://unl.demo.tetras-libre.fr/unl2rdf', data=data)
        except Exception as e:
            return 'Error calling https://unl.demo.tetras-libre.fr/unl2rdf : "{error}"'.format(error=e)
        
        htmlResult = r.text
        # On utilise une regex au lieu de parser le html car ce dernier est mal formé
        regexSvg = re.compile('<svg.*svg>',re.MULTILINE|re.DOTALL)
        regexSvg = re.compile("<code id='dot'>(.*?)</code>",re.MULTILINE|re.DOTALL)
        regexRdf = re.compile("<code id='rdf' class='collapse show'>(.*?)</code>",re.MULTILINE|re.DOTALL)
        svg = regexSvg.search(htmlResult).group()
        dot = html.unescape(  regexDot.search(htmlResult).group(1)  )      
        rdf = html.unescape(  regexRdf.search(htmlResult).group(1)  )   
    
        return(svg, dot, rdf)
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Please register or to comment