This repository was archived by the owner on Aug 6, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcontext2vec.py
More file actions
39 lines (33 loc) · 1.35 KB
/
Copy pathcontext2vec.py
File metadata and controls
39 lines (33 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import gensim
from gensim import utils
class ContextCorpus(object):
def __init__(self, contexts, context_prefix='C', example_prefix='I'):
"""
ContextCorpus(contexts)
Parameters
---------
contexts : dict of sets
Each set in the dict should be an observation occurring within
the same context. E.g., every set should contain all pins by
one client or all fixes by one client.
context_prefix : str, optional
This string will be prepended to all context keys
example_prefix : str, optional
This string will be prepended to all non-context words
Examples
------
# If user 1 bought items 0, 1, 2, 3 and user 2
# bought 3, 4, 5 and user bought item 10
contexts = {1:{0,1,2,3}, 2:{3,4,5}, 3:{10}}
cc = ContextCorpus(contexts)
"""
self.contexts = contexts
self.context_prefix = context_prefix
self.example_prefix = example_prefix
def __iter__(self):
"""Create 'sentences' that start with the context as a word
and then also have items as words on the same line """
for context, items in self.contexts.iteritems():
line = [self.context_prefix + str(context)]
line += [self.example_prefix + str(i) for i in items]
yield line