# Copyright (c) 2001 LOGILAB S.A. (Paris, FRANCE).
# http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
""" wrapper for guess_language """

__revision__ = "$Id: Language.py,v 1.2 2002/02/14 12:57:48 alf Exp $"

import string, re
from narval.lib import AL_NS, NO_NS, select_one, select_many, load_extension_module
language = load_extension_module('language','guess_language')

MOD_XML = """<?xml version="1.0" encoding="ISO-8859-1"?>
<module xmlns:al='%s'>""" % (AL_NS)

def guess_language_f(args, doc):
    s_u = args['data-unit']
    v = {}
    for n in select_many(s_u, 'vector/entry'):
        v[select_one(n, 'text()').data] = int(n.getAttributeNS(NO_NS,
                                                               'value'))
    s_u.setAttributeNS(NO_NS, 'lang', language.identify_language(v))
    return {'output': s_u}
    
MOD_XML = MOD_XML +  """
<al:action name='guess_language' func='guess_language_f'>
 <al:description>guess the language of a text document</al:description>
 <al:input id='data-unit'>
  <al:match>data-unit[vector]</al:match>
  <al:description lang='en'>
data unit, with a data child containing text to analyse
  </al:description>
 </al:input>
 <al:output id='output'>
  <al:match>data-unit[@lang]</al:match>
  <al:description lang='en'>
groups containing data-unit childs and a common child with the common words of
the group
  </al:description>
 </al:output>
</al:action>
"""
MOD_XML = MOD_XML + "</module>"
