# ==============================================================================
"""RILEX : print the lexical richness of a list of text files"""
# ==============================================================================
__author__  = "Christophe Schlick modified by Philippe Blasi"
__version__ = "1.0"
__date__    = "2022-11-12"
__usage__   = """
User input : <filename> [filename ...]
App output : lexical richness of all provided files"""
# ==============================================================================
from ezCLI import *
# ------------------------------------------------------------------------------
def rilex(name:str) -> str:
  """return the lexical richness of file 'name'"""
  words = read_txt(name).split() # split file content into words
  words = [word.upper() for word in words if len(word) > 3] # filter words
  histo = {} # initialize the word's histogram as an empty dictionary 
  for word in words: histo[word] = histo.get(word,0) + 1
  return f"{name} : rilex = {len(histo)/max(1,len(words)):0.2}"
# ------------------------------------------------------------------------------
def parser(command:str) -> str:
  """parse 'command' and return lexical richness of all provided files"""
  names = parse(command)
  return '\n'.join(rilex(name) for name in names)
# ==============================================================================
if __name__ == '__main__':
  userloop(parser, "Enter <filename> [filename ...]")
# ==============================================================================
