<s> import os <EOL> import sys <EOL> import time <EOL> import heapq <EOL> def read_n_merge ( fileLst ) : <EOL> '''<STR_LIT>''' <EOL> total_rules = <NUM_LIT:0> <EOL> total_1nt_rules = <NUM_LIT:0> <EOL> candLst = [ ] <EOL> ruleDict = { } <EOL> rulesDict1NT = { } <EOL> fileTrackLst = [ <NUM_LIT:1> for file in fileLst ] <EOL> stop_iteration = False <EOL> print "<STR_LIT>" <EOL> fHLst = [ open ( file , '<STR_LIT:r>' ) for file in fileLst ] <EOL> while True : <EOL> stop_iteration = True <EOL> for f_track in fileTrackLst : <EOL> if f_track != <NUM_LIT:9> : <EOL> stop_iteration = False <EOL> break <EOL> if stop_iteration : <EOL> break <EOL> for indx , f_track in enumerate ( fileTrackLst ) : <EOL> if f_track == <NUM_LIT:0> or f_track == <NUM_LIT:9> : <EOL> continue <EOL> fileTrackLst [ indx ] = <NUM_LIT:0> <EOL> line = fHLst [ indx ] . readline ( ) <EOL> line = line . strip ( ) <EOL> if line == '<STR_LIT>' : <EOL> fileTrackLst [ indx ] = <NUM_LIT:9> <EOL> continue <EOL> ( src , tgt , _ ) = line . split ( '<STR_LIT>' , <NUM_LIT:2> ) <EOL> rule = src + '<STR_LIT>' + tgt <EOL> if ruleDict . has_key ( rule ) : <EOL> ruleDict [ rule ] += [ indx ] <EOL> else : <EOL> ruleDict [ rule ] = [ indx ] <EOL> heapq . heappush ( candLst , rule ) <EOL> if src . find ( '<STR_LIT>' ) == - <NUM_LIT:1> : <EOL> rulesDict1NT [ rule ] = <NUM_LIT:1> <EOL> if len ( candLst ) == <NUM_LIT:0> : continue <EOL> popped_rule = heapq . heappop ( candLst ) <EOL> total_rules += <NUM_LIT:1> <EOL> total_1nt_rules += rulesDict1NT . pop ( popped_rule , <NUM_LIT:0> ) <EOL> for indx1 in ruleDict . pop ( popped_rule ) : <EOL> fileTrackLst [ indx1 ] = <NUM_LIT:1> <EOL> for fH in fHLst : <EOL> fH . close ( ) <EOL> sys . stdout . write ( "<STR_LIT>" % ( total_rules ) ) <EOL> sys . stdout . write ( "<STR_LIT>" % ( total_1nt_rules ) ) <EOL> def main ( ) : <EOL> inDir = sys . argv [ <NUM_LIT:1> ] <EOL> if not inDir . endswith ( '<STR_LIT:/>' ) : inDir += '<STR_LIT:/>' <EOL> fileLst = [ ] <EOL> for file in os . listdir ( inDir ) : <EOL> if file . startswith ( "<STR_LIT>" ) : is_tgt_file = True <EOL> else : is_tgt_file = False <EOL> file = inDir + file <EOL> if os . path . isfile ( file ) : <EOL> if not is_tgt_file : fileLst . append ( file ) <EOL> sys . stderr . write ( "<STR_LIT>" % ( len ( fileLst ) ) ) <EOL> t_beg = time . time ( ) <EOL> read_n_merge ( fileLst ) <EOL> sys . stderr . write ( "<STR_LIT>" % ( time . time ( ) - t_beg ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
