"""  This file defines miscellaneous functions used
     by infopal extensions and test units
"""

# 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.

__revision__ = '$Id: misc.py,v 1.1 2001/10/10 08:05:34 syt Exp $'

EPSILON = 0.0000001
def float_equal(float1, float2):
    """ compare 2 float numbers """
    from math import fabs
    
    return fabs(float1 - float2) <= EPSILON


def square(val):
    """ return val square """
    return val*val


def norm(vect):
    """ perform vector Euclidienne norm """
    import math
    return math.sqrt(reduce(lambda x,y: square(y)+x, vect, 0.0))


def cmp_eq_words(d1, d2):
    """ compare two ('word':#occ),
    decreasing order occurrence and words. """
    if d1[1] > d2[1] :
         return -1
    elif d1[1] == d2[1]:
         l1 = len(d1[0])
         l2 = len(d2[0])
         if l1 > l2: return -1
         elif l1 == l2 : return 0
         else :
             return 1
    else :
         return 1


def classes(args, vector_url_list):
    """ search a classes number value or performs it """
    
    from narval.lib import select_many
    class_nb_elmt = select_many(args, './number-of-classes/text()')
    if len(class_nb_elmt) != 1:
        if len(vector_url_list) < 12:
            return int(4)
        else:
            return int(len(vector_url_list)/5)
    else: 
         return int(class_nb_elmt[0].data)


def common_words(classe, vector_url_list, debug):
    """ search common words in all articles from classe
    return the first 5 words separated by ', '  
    """
    import types
    com_words = {}
    for index in classe:
        if type(index) == types.IntType:
            for duo in vector_url_list[index][1].items():
                if com_words.has_key(duo[0]):
                    com_words[duo[0]] = com_words[duo[0]] + duo[1]
                else:
                    com_words[duo[0]] = duo[1]
    # com_words => list and sort
    list_duo = com_words.items()
    list_duo.sort(cmp_eq_words)
    txt_title = ''

    if debug:
        for i in range(10):
            if len(list_duo)-1 >= i:
                print list_duo[i]
            else:
                break
                
    for i in range(5):
        if len(list_duo)-1 >= i:
            txt_title = txt_title + str(list_duo[i][0]) + ', '
        else:
            break
    txt_title = txt_title[:-2]
    if len(txt_title) != 0:
        return txt_title
    else:
        return 'No words'
