docs: def write_documents(corpus, target, featureset_name, metadata_fields=[]):
      """
      Parameters
      ----------
  
  
      """
  
      docpath = target + '_docs.txt'
      metapath = target + '_meta.csv'
  
      features = corpus.features[featureset_name].features
      ftype = type(corpus.features[featureset_name])
      index = corpus.features[featureset_name].index
  
      try:
          docFile = open(docpath, 'wb')
      except IOError:
          raise IOError('Invalid target. Could not open files for writing.')
  
      # Generate metadata.
      with codecs.open(metapath, 'w', encoding='utf-8') as f:
          writer = csv.writer(f)
          writer.writerow([corpus.index_by] + list(metadata_fields))
          for i, p in corpus.indexed_papers.iteritems():
              getter = lambda m: getattr(p, m) if hasattr(p, m) else None
              writer.writerow([i] + list(map(getter, metadata_fields)))
  
      # Write documents content.
      with codecs.open(docpath, 'w', encoding='utf-8') as f:
          for i, p in corpus.indexed_papers.iteritems():
              if i in features:
                  row = [i, u'en']
                  if ftype is FeatureSet:
                      row += [u' '.join(repeat(e, c)) for e, c in features[i]]
                  elif ftype is StructuredFeatureSet:
                      row += features[i]
                  f.write(u'\t'.join(row) + u'\n')
  
      return docpath, metapath