1. import sys
  2. g = {'#': []} # graph/tree
  3. cur_lex = '' # current continuation lexicon
  4. lines = [] # cache of the lines
  5. nodes = {'#': 0} # node to id mapping
  6. i = 1 # current id
  7. # First read through the lexc file and make a list of the continuation lexica
  8. for line in sys.stdin.readlines():
  9. line = line.strip()
  10. if line == '': continue
  11. if line[0] == '!': continue
  12. row = line.replace('% ', '%<SPACE>').replace(' ;', ';').replace(';', ' ;').split(' ')
  13. lines.append(row)
  14. if row[0] == 'LEXICON':
  15. if row[1] not in g:
  16. g[row[1]] = []
  17. nodes[row[1]] = i
  18. i += 1
  19. continue
  20. # Now read through again and do a map of lexicon -> outgoing lexicons
  21. cur_lex = ''
  22. for row in lines:
  23. s = 0
  24. if row[0] == 'LEXICON':
  25. cur_lex = row[1]
  26. continue
  27. if row[0].count(':') > 0 and row[1] != '' and row[1] != ';':
  28. s = 1
  29. g[cur_lex].append(row[1]);
  30. elif len(row) >= 2 and row[1] == ';' and row[0] not in ['',';'] and row[0].count('<') == 0:
  31. s = 2
  32. g[cur_lex].append(row[0]);
  33. # print('!', s, cur_lex, row, g, file=sys.stderr)
  34. # Print out the graph
  35. print('digraph G { rankdir="LR"')
  36. print('node [fontname="Tahoma",shape=box,fontsize=14,fixedsize=false,fillcolor="grey",style=filled]')
  37. print('edge [fontname="FreeMono",fontsize=14]')
  38. for lex in g:
  39. print('%s [label="%s"];' % (nodes[lex], lex))
  40. g[lex] = list(set(g[lex]))
  41. for cont in g[lex]:
  42. print('@@', lex, cont, file=sys.stderr)
  43. print('%s -> %s ;' % (nodes[lex], nodes[cont]))
  44. print('}')