Skip to content

Instantly share code, notes, and snippets.

@kljensen
Created May 18, 2016 20:59
Show Gist options
  • Select an option

  • Save kljensen/1dbec7f5ada90108bbff10ef44f640c8 to your computer and use it in GitHub Desktop.

Select an option

Save kljensen/1dbec7f5ada90108bbff10ef44f640c8 to your computer and use it in GitHub Desktop.
def transpose_corpus(corpus, output, limit):
""" Transpose a corpus in Matrix Market format.
"""
corpus = corpora.MmCorpus(corpus)
corpus2 = [list() for i in range(corpus.num_terms)]
for (i, doc) in enumerate(corpus):
for (j, count) in doc:
corpus2[j].append((i, count))
if limit > 0 and i >= limit:
break
corpora.MmCorpus.serialize(output, corpus2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment