#!/usr/bin/env python

# keyword2lexicon.py given a carrel and an integer, output a lexicon based on keywords

# Eric Lease Morgan <emorgan@nd.edu>
# (c) University of Notre Dame; distributed under a GNU Public License

# July 11, 2024 - first documentation; made it more mature


# configure
LIBRARY = 'localLibrary'

# require
from rdr import keywords, configuration, ETC, LEXICON
from sys import argv, exit, stderr

# get input
if len( argv ) !=3 : exit( 'Usage: ' + argv[ 0 ] + " <carrel> <number>" )
carrel = argv[ 1 ]
number = int( argv[ 2 ] )

# get frequencies; create a list of keywords
frequencies = keywords( carrel, count=True ).splitlines()[ : number ]
keywords    = [ frequency.split( '\t' )[ 0 ] for frequency in frequencies ]

# debug
stderr.write( '\n'.join( sorted( keywords ) ) + '\n' )

# save
library = configuration( LIBRARY )
lexicon = library/carrel/ETC/LEXICON
with open( lexicon, 'w' ) as handle: handle.write( '\n'.join( keywords ) )

# done
exit()
