PYTHON   22

lexicon py

Guest on 5th August 2022 02:59:34 PM

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. """
  5. A module with a simplified version of a word-lexicon holds terms of
  6. the form: (concept, forms). The concept is a basic word such as
  7. \"age\" and the forms are all the forms in which the concept occurs in
  8. a text, such as \"aging\", \"ages\" and so forth.
  9. """
  10.  
  11. def new_lexicon():
  12.     """
  13.    Creates an empty lexicon.
  14.    """
  15.     return {}
  16.  
  17. def add_term(concept, lexicon):
  18.     """
  19.    Add a term to the lexicon.
  20.  
  21.    @type concept: string
  22.    @param concept: the word that is stored as a concept
  23.    @type lexicon: a lexicon (created by this module)
  24.    @param lexicon: the lexicon where the concept is stored
  25.    """
  26.     lexicon[concept] = (concept, [])
  27.  
  28. def add_form(form, concept, lexicon):
  29.     """
  30.    Add a form to the lexicon for given concept
  31.  
  32.    @type form: string
  33.    @param form: the word that is stored as a new form
  34.    @type concept: string
  35.    @param concept: the concept for wich the new form is its form
  36.    @type lexicon: a lexicon (created by this module)
  37.    @param lexicon: the lexicon where the concept is stored
  38.    """
  39.     term = lexicon[concept]
  40.     term[1].append(form)
  41.     lexicon[form] = term
  42.  
  43. def get_term(form, lexicon):
  44.     """
  45.    Return a term (concept plus forms) from the lexicon.
  46.  
  47.    @type form: string
  48.    @param form: the form
  49.    @type lexicon: a lexicon (created by this module)
  50.    @param lexicon: the lexicon where the concept is stored
  51.    """
  52.     return lexicon[form]
  53.  
  54. def get_all_forms(lexicon):
  55.     """
  56.    Return all forms occurring in the lexicon.
  57.  
  58.    @type lexicon: a lexicon (created by this module)
  59.    @param lexicon: the lexicon where the concept is stored
  60.    """
  61.     return lexicon.keys()
  62.  
  63. def get_frequence_table(lexicon):
  64.     """
  65.    Return a frequency table over concepts in the lexicon.
  66.  
  67.    Number of occurrences are returned as integer values in a dictionary.
  68.    
  69.    @type lexicon: a lexicon (created by this module)
  70.    @param lexicon: the lexicon where the concept is stored
  71.    @return: a dictionary with concept as keys and an integer as value.
  72.    """
  73.     freq_table = {}
  74.     for term in lexicon.values():
  75.         concept = term[0]
  76.         if not concept in freq_table.keys():
  77.             freq_table[concept] = 1
  78.         else:
  79.             freq_table[concept] = freq_table[concept] + 1
  80.     return freq_table
  81.        
  82.    
  83. # Test code
  84. # Examples in swedish
  85. if __name__ == '__main__':    
  86.     lexicon = new_lexicon()
  87.     add_term('ro', lexicon)
  88.     add_form('rodde', 'ro', lexicon)
  89.     add_form('ror', 'ro', lexicon)
  90.     add_form('roende', 'ro', lexicon)
  91.     print "Hela lexikonet: ", lexicon
  92.     add_term('bygga', lexicon)
  93.     add_form('byggde', 'bygga', lexicon)
  94.     add_form('bygger', 'bygga', lexicon)
  95.     add_form('byggare', 'bygga', lexicon)
  96.     add_form('byggande', 'bygga', lexicon)
  97.     print "Term för 'ror': " " + str(get_term('ror', lexicon))
  98.     print "Term för 'rodde': " + str(get_term('rodde', lexicon))
  99.     print "Alla former i lexikonet:" + str(get_all_forms(lexicon))
  100.     print "Frekvenstabell: " + str(get_frequence_table(lexic

Raw Paste


Login or Register to edit or fork this paste. It's free.