import re, sys, string, os, urllib

RegExp = r'\n([^\s]+?)\s[^\[\n]+?\[([^\]]+?)\]'

def fm(y, flag): 
   if flag == 'c': 
      y = string.replace(y, '/', '_')
      y = string.replace(y, '#', 's')
      y = string.replace(y, '(', '_')
      y = string.replace(y, ')', '')
      y = urllib.quote(y) # Just in case
   if flag == 's': 
      x = string.split(y, ' ')
      if len(x) == 6: 
         y = ':first "'+x[0]+'"; :second "'+x[1]+'"; '
         y += ':third "'+x[2]+'"; :fourth "'+x[3]+'"; '
         y += ':fifth "'+x[4]+'"; :sixth "'+x[5]+'"'
   return y

def run(): 
   print '@prefix : <http://purl.org/swag/sbp/tab#> .'
   print '@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n'
   f = open(sys.argv[1],'r'); s = f.read(); f.close()
   s = string.replace(s, ' #5', '_#5')
   results = re.compile(RegExp, re.S).findall(s)
   for m in results: 
      print ':%s rdfs:label "%s"; :tab [ rdfs:label "%s"; \n   %s ] .' \
           % (fm(m[0], 'c'), fm(m[0], 'l'), fm(m[1], 'v'), fm(m[1], 's'))

if __name__ == "__main__": 
   run()
