gensim
Eine Bibliothek von in Python implementierten Themenmodellen. Die Details der Funktion werden hier nicht behandelt. Dieses Mal werde ich die Formate verschiedener Formate zusammenfassen, die konvertiert werden können, wenn eine Zeichenfolge mit gensim in das BoW-Format konvertiert wird.
Ausgabe als Offizielle Referenz.
from gensim import corpora
from collections import defaultdict
from pprint import pprint
documents = ["Human machine interface for lab abc computer applications",
"A survey of user opinion of computer system response time",
"The EPS user interface management system",
"System and human system engineering testing of EPS",
"Relation of user perceived response time to error measurement",
"The generation of random binary unordered trees",
"The intersection graph of paths in trees",
"Graph minors IV Widths of trees and well quasi ordering",
"Graph minors A survey"]
stoplist = set('for a of the and to in'.split())
texts = [[word for word in document.lower().split() if word not in stoplist]
for document in documents]
frequency = defaultdict(int)
for text in texts:
for token in text:
frequency[token] += 1
texts = [[token for token in text if frequency[token] > 1]
for text in texts]
dictionary = corpora.Dictionary(texts)
corpus = [dictionary.doc2bow(text) for text in texts]
corpora.MmCorpus.serialize("./corpus.mm", corpus)
corpora.BleiCorpus.serialize("./corpus.blei", corpus)
corpora.LowCorpus.serialize("./corpus.low", corpus)
corpora.SvmLightCorpus.serialize("./corpus.svmlight", corpus)
corpora.UciCorpus.serialize("./corpus.low", corpus)
pprint(texts)
print("\n")
pprint(dictionary.token2id)
print("\n")
pprint(corpus)
Output
[['human', 'interface', 'computer'],
['survey', 'user', 'computer', 'system', 'response', 'time'],
['eps', 'user', 'interface', 'system'],
['system', 'human', 'system', 'eps'],
['user', 'response', 'time'],
['trees'],
['graph', 'trees'],
['graph', 'minors', 'trees'],
['graph', 'minors', 'survey']]
{'computer': 1,
'eps': 8,
'graph': 10,
'human': 2,
'interface': 0,
'minors': 11,
'response': 6,
'survey': 4,
'system': 5,
'time': 7,
'trees': 9,
'user': 3}
[[(0, 1), (1, 1), (2, 1)],
[(1, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1)],
[(0, 1), (3, 1), (5, 1), (8, 1)],
[(2, 1), (5, 2), (8, 1)],
[(3, 1), (6, 1), (7, 1)],
[(9, 1)],
[(9, 1), (10, 1)],
[(9, 1), (10, 1), (11, 1)],
[(4, 1), (10, 1), (11, 1)]]
Matrix Market format
corpus.mm
%%MatrixMarket matrix coordinate real general
9 12 28
1 1 1
1 2 1
1 3 1
2 2 1
2 4 1
2 5 1
2 6 1
2 7 1
2 8 1
3 1 1
3 4 1
3 6 1
3 9 1
4 3 1
4 6 2
4 9 1
5 4 1
5 7 1
5 8 1
6 10 1
7 10 1
7 11 1
8 10 1
8 11 1
8 12 1
9 5 1
9 11 1
9 12 1
Blei format
corpus.blei
3 0:1 1:1 2:1
6 1:1 3:1 4:1 5:1 6:1 7:1
4 0:1 3:1 5:1 8:1
3 2:1 5:2 8:1
3 3:1 6:1 7:1
1 9:1
2 9:1 10:1
3 9:1 10:1 11:1
3 4:1 10:1 11:1
text:corpus.blei.vocab
0
1
2
3
4
5
6
7
8
9
10
11
UCI format
corpus.uci
9
12
28
1 1 1
1 2 1
1 3 1
2 2 1
2 4 1
2 5 1
2 6 1
2 7 1
2 8 1
3 1 1
3 4 1
3 6 1
3 9 1
4 3 1
4 6 2
4 9 1
5 4 1
5 7 1
5 8 1
6 10 1
7 10 1
7 11 1
8 10 1
8 11 1
8 12 1
9 5 1
9 11 1
9 12 1
text:corpus.uci.vocab
0
1
2
3
4
5
6
7
8
9
10
11
Low format
corpus.low
9
0 1 2
1 3 4 5 6 7
0 3 5 8
2 5 5 8
3 6 7
9
9 10
9 10 11
4 10 11
text:corpus.low.vocab
0
1
2
3
4
5
6
7
8
9
10
11
SvmLight format
corpus.svmlight
0 1:1 2:1 3:1
0 2:1 4:1 5:1 6:1 7:1 8:1
0 1:1 4:1 6:1 9:1
0 3:1 6:2 9:1
0 4:1 7:1 8:1
0 10:1
0 10:1 11:1
0 10:1 11:1 12:1
0 5:1 11:1 12:1
Recommended Posts