new_contents: # This script extracts specific sets of instructions generated by the prohow-crawler
  # The urls to extract are found in the extract_specific_sets_instructions.txt file
  # The extract_specific_sets_instructions.txt file should contain one URL per line
  # only the sets of instructions corresponding to these URLs will be extracted
  # the URLs will be matched in a non-case sensitive way
  
  # This parameter can be changed to only select instructions with specific language tags.
  # More specifically, the algorighm will only consider files having a name which starts with at least one 
  list_of_allowed_languages = []
  # for example, for English and Spanish: 
  # list_of_allowed_languages = ["en","es"]
  
  import os, ntpath
  
  list_of_urls = []
  conf = open("extract_specific_sets_instructions.txt",'r')
  for line in conf:
      extracted = line.lower().strip()
      if len(extracted) > 0:
          list_of_urls.append(extracted);
  conf.close()
  
  for line in list_of_urls:
      print line
  	
  
  
  out = open('extracted.txt','w')
  
  def save(line):
      out.write(line+"\n\n")
  
  def parse_file(file):
      f = open(file,'r')
      full_instruction = ""	
      found = False
      found_num = 0
      for line in f:
          if 'oa:hasTarget <' in line:
              for url in list_of_urls:
                  if "<"+str(url.lower())+">" in line.lower():
                      found = True
          if 'rdf:type prohow:instruction_set .' in line:
              # if the previous instruction set was selected, save it in output
              if found:
                  save(full_instruction)
                  found_num += 1
              # start collecting a new instructions set
              found = False
              full_instruction = line
          else:
              if len(full_instruction) > 0:
                  full_instruction = full_instruction+line        
      if 'rdf:type prohow:instruction_set .' in line:
          if found:
              save(full_instruction)
              found_num += 1				
      f.close()
      print "Found in file: "+str(found_num)
      return found_num
  
  
  rootdir = os.getcwd()
  total_found = 0
  for subdir, dirs, files in os.walk(rootdir):
      for file in files:
          #print os.path.join(subdir, file)
          filepath = subdir + os.sep + file
          if filepath.endswith(".ttl"):
              if len(list_of_allowed_languages) > 0:
                  for lang in list_of_allowed_languages:
                      if ntpath.basename(filepath).startswith(lang):
                          print filepath
                          total_found += parse_file(filepath)
              else:
                  total_found += parse_file(filepath)
  out.close()
  
  print 'In total, '+str(total_found)+' were found'
  
subject: Create extract_specific_instruction_sets.py