-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
871 lines (698 loc) · 28.5 KB
/
Copy pathutils.py
File metadata and controls
871 lines (698 loc) · 28.5 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
from __future__ import with_statement
from contextlib import contextmanager
import collections.abc
import logging
import warnings
import numbers
from html.entities import name2codepoint as n2cp
import pickle as _pickle
import re
import unicodedata
import os
import random
import itertools
import tempfile
from functools import wraps
import multiprocessing
import shutil
import sys
import subprocess
import inspect
import heapq
from copy import deepcopy
from datetime import datetime
import platform
import types
import numpy as np
import scipy.sparse
from smart_open import open
gensim_version = '4.2.0'
logger = logging.getLogger(__name__)
PICKLE_PROTOCOL = 4
PAT_ALPHABETIC = re.compile(r'(((?![\d])\w)+)', re.UNICODE)
RE_HTML_ENTITY = re.compile(r'&(#?)([xX]?)(\w{1,8});', re.UNICODE)
NO_CYTHON = RuntimeError(
"Compiled extensions are unavailable. "
"If you've installed from a package, ask the package maintainer to include compiled extensions. "
"If you're building Gensim from source yourself, install Cython and a C compiler, and then "
"run `python setup.py build_ext --inplace` to retry. "
)
default_prng = np.random.default_rng()
def get_random_state(seed):
if seed is None or seed is np.random:
return np.random.mtrand._rand
if isinstance(seed, (numbers.Integral, np.integer)):
return np.random.RandomState(seed)
if isinstance(seed, np.random.RandomState):
return seed
raise ValueError('%r cannot be used to seed a np.random.RandomState instance' % seed)
def synchronous(tlockname):
def _synched(func):
@wraps(func)
def _synchronizer(self, *args, **kwargs):
tlock = getattr(self, tlockname)
logger.debug("acquiring lock %r for %s", tlockname, func.__name__)
with tlock:
logger.debug("acquired lock %r for %s", tlockname, func.__name__)
result = func(self, *args, **kwargs)
logger.debug("releasing lock %r for %s", tlockname, func.__name__)
return result
return _synchronizer
return _synched
def file_or_filename(input):
if isinstance(input, str):
return open(input, 'rb')
else:
input.seek(0)
return input
@contextmanager
def open_file(input):
mgr = file_or_filename(input)
exc = False
try:
yield mgr
except Exception:
exc = True
if not isinstance(input, str) or not mgr.__exit__(*sys.exc_info()):
raise
finally:
if not exc and isinstance(input, str):
mgr.__exit__(None, None, None)
def deaccent(text):
if not isinstance(text, str):
text = text.decode('utf8')
norm = unicodedata.normalize("NFD", text)
result = ''.join(ch for ch in norm if unicodedata.category(ch) != 'Mn')
return unicodedata.normalize("NFC", result)
def copytree_hardlink(source, dest):
copy2 = shutil.copy2
try:
shutil.copy2 = os.link
shutil.copytree(source, dest)
finally:
shutil.copy2 = copy2
def tokenize(text, lowercase=False, deacc=False, encoding='utf8', errors="strict", to_lower=False, lower=False):
lowercase = lowercase or to_lower or lower
text = to_unicode(text, encoding, errors=errors)
if lowercase:
text = text.lower()
if deacc:
text = deaccent(text)
return simple_tokenize(text)
def simple_tokenize(text):
for match in PAT_ALPHABETIC.finditer(text):
yield match.group()
def simple_preprocess(doc, deacc=False, min_len=2, max_len=15):
tokens = [
token for token in tokenize(doc, lower=True, deacc=deacc, errors='ignore')
if min_len <= len(token) <= max_len and not token.startswith('_')
]
return tokens
def any2utf8(text, errors='strict', encoding='utf8'):
if isinstance(text, str):
return text.encode('utf8')
return str(text, encoding, errors=errors).encode('utf8')
to_utf8 = any2utf8
def any2unicode(text, encoding='utf8', errors='strict'):
if isinstance(text, str):
return text
return str(text, encoding, errors=errors)
to_unicode = any2unicode
def call_on_class_only(*args, **kwargs):
raise AttributeError('This method should be called on a class object.')
class SaveLoad:
def add_lifecycle_event(self, event_name, log_level=logging.INFO, **event):
event_dict = deepcopy(event)
event_dict['datetime'] = datetime.now().isoformat()
event_dict['gensim'] = gensim_version
event_dict['python'] = sys.version
event_dict['platform'] = platform.platform()
event_dict['event'] = event_name
if not hasattr(self, 'lifecycle_events'):
logger.debug("starting a new internal lifecycle event log for %s", self.__class__.__name__)
self.lifecycle_events = []
if log_level:
logger.log(log_level, "%s lifecycle event %s", self.__class__.__name__, event_dict)
if self.lifecycle_events is not None:
self.lifecycle_events.append(event_dict)
@classmethod
def load(cls, fname, mmap=None):
logger.info("loading %s object from %s", cls.__name__, fname)
compress, subname = SaveLoad._adapt_by_suffix(fname)
obj = unpickle(fname)
obj._load_specials(fname, mmap, compress, subname)
obj.add_lifecycle_event("loaded", fname=fname)
return obj
def _load_specials(self, fname, mmap, compress, subname):
def mmap_error(obj, filename):
return IOError(
'Cannot mmap compressed object %s in file %s. ' % (obj, filename)
+ 'Use `load(fname, mmap=None)` or uncompress files manually.'
)
for attrib in getattr(self, '__recursive_saveloads', []):
cfname = '.'.join((fname, attrib))
logger.info("loading %s recursively from %s.* with mmap=%s", attrib, cfname, mmap)
with ignore_deprecation_warning():
getattr(self, attrib)._load_specials(cfname, mmap, compress, subname)
for attrib in getattr(self, '__numpys', []):
logger.info("loading %s from %s with mmap=%s", attrib, subname(fname, attrib), mmap)
if compress:
if mmap:
raise mmap_error(attrib, subname(fname, attrib))
val = np.load(subname(fname, attrib))['val']
else:
val = np.load(subname(fname, attrib), mmap_mode=mmap)
with ignore_deprecation_warning():
setattr(self, attrib, val)
for attrib in getattr(self, '__scipys', []):
logger.info("loading %s from %s with mmap=%s", attrib, subname(fname, attrib), mmap)
sparse = unpickle(subname(fname, attrib))
if compress:
if mmap:
raise mmap_error(attrib, subname(fname, attrib))
with np.load(subname(fname, attrib, 'sparse')) as f:
sparse.data = f['data']
sparse.indptr = f['indptr']
sparse.indices = f['indices']
else:
sparse.data = np.load(subname(fname, attrib, 'data'), mmap_mode=mmap)
sparse.indptr = np.load(subname(fname, attrib, 'indptr'), mmap_mode=mmap)
sparse.indices = np.load(subname(fname, attrib, 'indices'), mmap_mode=mmap)
with ignore_deprecation_warning():
setattr(self, attrib, sparse)
for attrib in getattr(self, '__ignoreds', []):
logger.info("setting ignored attribute %s to None", attrib)
with ignore_deprecation_warning():
setattr(self, attrib, None)
@staticmethod
def _adapt_by_suffix(fname):
compress, suffix = (True, 'npz') if fname.endswith('.gz') or fname.endswith('.bz2') else (False, 'npy')
return compress, lambda *args: '.'.join(args + (suffix,))
def _smart_save(
self, fname,separately=None, sep_limit=10 * 1024**2, ignore=frozenset(), pickle_protocol=PICKLE_PROTOCOL):
compress, subname = SaveLoad._adapt_by_suffix(fname)
restores = self._save_specials(
fname, separately, sep_limit, ignore, pickle_protocol, compress, subname,
)
try:
pickle(self, fname, protocol=pickle_protocol)
finally:
for obj, asides in restores:
for attrib, val in asides.items():
with ignore_deprecation_warning():
setattr(obj, attrib, val)
logger.info("saved %s", fname)
def _save_specials(self, fname, separately, sep_limit, ignore, pickle_protocol, compress, subname):
asides = {}
sparse_matrices = (scipy.sparse.csr_matrix, scipy.sparse.csc_matrix)
if separately is None:
separately = []
for attrib, val in self.__dict__.items():
if isinstance(val, np.ndarray) and val.size >= sep_limit:
separately.append(attrib)
elif isinstance(val, sparse_matrices) and val.nnz >= sep_limit:
separately.append(attrib)
with ignore_deprecation_warning():
for attrib in separately + list(ignore):
if hasattr(self, attrib):
asides[attrib] = getattr(self, attrib)
delattr(self, attrib)
recursive_saveloads = []
restores = []
for attrib, val in self.__dict__.items():
if hasattr(val, '_save_specials'):
recursive_saveloads.append(attrib)
cfname = '.'.join((fname, attrib))
restores.extend(val._save_specials(cfname, None, sep_limit, ignore, pickle_protocol, compress, subname))
try:
numpys, scipys, ignoreds = [], [], []
for attrib, val in asides.items():
if isinstance(val, np.ndarray) and attrib not in ignore:
numpys.append(attrib)
logger.info("storing np array '%s' to %s", attrib, subname(fname, attrib))
if compress:
np.savez_compressed(subname(fname, attrib), val=np.ascontiguousarray(val))
else:
np.save(subname(fname, attrib), np.ascontiguousarray(val))
elif isinstance(val, (scipy.sparse.csr_matrix, scipy.sparse.csc_matrix)) and attrib not in ignore:
scipys.append(attrib)
logger.info("storing scipy.sparse array '%s' under %s", attrib, subname(fname, attrib))
if compress:
np.savez_compressed(
subname(fname, attrib, 'sparse'),
data=val.data,
indptr=val.indptr,
indices=val.indices
)
else:
np.save(subname(fname, attrib, 'data'), val.data)
np.save(subname(fname, attrib, 'indptr'), val.indptr)
np.save(subname(fname, attrib, 'indices'), val.indices)
data, indptr, indices = val.data, val.indptr, val.indices
val.data, val.indptr, val.indices = None, None, None
try:
pickle(val, subname(fname, attrib), protocol=pickle_protocol)
finally:
val.data, val.indptr, val.indices = data, indptr, indices
else:
logger.info("not storing attribute %s", attrib)
ignoreds.append(attrib)
self.__dict__['__numpys'] = numpys
self.__dict__['__scipys'] = scipys
self.__dict__['__ignoreds'] = ignoreds
self.__dict__['__recursive_saveloads'] = recursive_saveloads
except Exception:
for attrib, val in asides.items():
setattr(self, attrib, val)
raise
return restores + [(self, asides)]
def save(
self, fname_or_handle,
separately=None, sep_limit=10 * 1024**2, ignore=frozenset(), pickle_protocol=PICKLE_PROTOCOL,
):
self.add_lifecycle_event(
"saving",
fname_or_handle=str(fname_or_handle),
separately=str(separately),
sep_limit=sep_limit,
ignore=ignore,
)
try:
_pickle.dump(self, fname_or_handle, protocol=pickle_protocol)
logger.info("saved %s object", self.__class__.__name__)
except TypeError:
self._smart_save(fname_or_handle, separately, sep_limit, ignore, pickle_protocol=pickle_protocol)
def identity(p):
return p
def get_max_id(corpus):
maxid = -1
for document in corpus:
if document:
maxid = max(maxid, max(fieldid for fieldid, _ in document))
return maxid
class FakeDict:
def __init__(self, num_terms):
self.num_terms = num_terms
def __str__(self):
return "%s<num_terms=%s>" % (self.__class__.__name__, self.num_terms)
def __getitem__(self, val):
if 0 <= val < self.num_terms:
return str(val)
raise ValueError("internal id out of bounds (%s, expected <0..%s))" % (val, self.num_terms))
def __contains__(self, val):
return 0 <= val < self.num_terms
def iteritems(self):
for i in range(self.num_terms):
yield i, str(i)
def keys(self):
return [self.num_terms - 1]
def __len__(self):
return self.num_terms
def get(self, val, default=None):
if 0 <= val < self.num_terms:
return str(val)
return default
def dict_from_corpus(corpus):
num_terms = 1 + get_max_id(corpus)
id2word = FakeDict(num_terms)
return id2word
def is_corpus(obj):
try:
if 'Corpus' in obj.__class__.__name__:
return True, obj
except Exception:
pass
try:
if hasattr(obj, 'next') or hasattr(obj, '__next__'):
doc1 = next(obj)
obj = itertools.chain([doc1], obj)
else:
doc1 = next(iter(obj))
if len(doc1) == 0:
return True, obj
id1, val1 = next(iter(doc1))
except Exception:
return False, obj
return True, obj
def get_my_ip():
import socket
try:
from Pyro4.naming import locateNS
ns = locateNS()
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect((ns._pyroUri.host, ns._pyroUri.port))
result, port = s.getsockname()
except Exception:
try:
import commands
result = commands.getoutput("ifconfig").split("\n")[1].split()[1][5:]
if len(result.split('.')) != 4:
raise Exception()
except Exception:
result = socket.gethostbyname(socket.gethostname())
return result
class RepeatCorpus(SaveLoad):
def __init__(self, corpus, reps):
self.corpus = corpus
self.reps = reps
def __iter__(self):
return itertools.islice(itertools.cycle(self.corpus), self.reps)
class RepeatCorpusNTimes(SaveLoad):
def __init__(self, corpus, n):
self.corpus = corpus
self.n = n
def __iter__(self):
for _ in range(self.n):
for document in self.corpus:
yield document
class ClippedCorpus(SaveLoad):
def __init__(self, corpus, max_docs=None):
self.corpus = corpus
self.max_docs = max_docs
def __iter__(self):
return itertools.islice(self.corpus, self.max_docs)
def __len__(self):
return min(self.max_docs, len(self.corpus))
class SlicedCorpus(SaveLoad):
def __init__(self, corpus, slice_):
self.corpus = corpus
self.slice_ = slice_
self.length = None
def __iter__(self):
if hasattr(self.corpus, 'index') and len(self.corpus.index) > 0:
return (self.corpus.docbyoffset(i) for i in self.corpus.index[self.slice_])
return itertools.islice(self.corpus, self.slice_.start, self.slice_.stop, self.slice_.step)
def __len__(self):
if self.length is None:
if isinstance(self.slice_, (list, np.ndarray)):
self.length = len(self.slice_)
elif isinstance(self.slice_, slice):
(start, end, step) = self.slice_.indices(len(self.corpus.index))
diff = end - start
self.length = diff // step + (diff % step > 0)
else:
self.length = sum(1 for x in self)
return self.length
def safe_unichr(intval):
try:
return chr(intval)
except ValueError:
s = "\\U%08x" % intval
return s.decode('unicode-escape')
def decode_htmlentities(text):
def substitute_entity(match):
try:
ent = match.group(3)
if match.group(1) == "#":
if match.group(2) == '':
return safe_unichr(int(ent))
elif match.group(2) in ['x', 'X']:
return safe_unichr(int(ent, 16))
else:
cp = n2cp.get(ent)
if cp:
return safe_unichr(cp)
else:
return match.group()
except Exception:
return match.group()
return RE_HTML_ENTITY.sub(substitute_entity, text)
def chunkize_serial(iterable, chunksize, as_numpy=False, dtype=np.float32):
it = iter(iterable)
while True:
if as_numpy:
wrapped_chunk = [[np.array(doc, dtype=dtype) for doc in itertools.islice(it, int(chunksize))]]
else:
wrapped_chunk = [list(itertools.islice(it, int(chunksize)))]
if not wrapped_chunk[0]:
break
yield wrapped_chunk.pop()
grouper = chunkize_serial
class InputQueue(multiprocessing.Process):
def __init__(self, q, corpus, chunksize, maxsize, as_numpy):
super(InputQueue, self).__init__()
self.q = q
self.maxsize = maxsize
self.corpus = corpus
self.chunksize = chunksize
self.as_numpy = as_numpy
def run(self):
it = iter(self.corpus)
while True:
chunk = itertools.islice(it, self.chunksize)
if self.as_numpy:
wrapped_chunk = [[np.asarray(doc) for doc in chunk]]
else:
wrapped_chunk = [list(chunk)]
if not wrapped_chunk[0]:
self.q.put(None, block=True)
break
try:
qsize = self.q.qsize()
except NotImplementedError:
qsize = '?'
logger.debug("prepared another chunk of %i documents (qsize=%s)", len(wrapped_chunk[0]), qsize)
self.q.put(wrapped_chunk.pop(), block=True)
if os.name == 'nt' or (sys.platform == "darwin" and sys.version_info >= (3, 8)):
def chunkize(corpus, chunksize, maxsize=0, as_numpy=False):
if maxsize > 0:
entity = "Windows" if os.name == 'nt' else "OSX with python3.8+"
warnings.warn("detected %s; aliasing chunkize to chunkize_serial" % entity)
for chunk in chunkize_serial(corpus, chunksize, as_numpy=as_numpy):
yield chunk
else:
def chunkize(corpus, chunksize, maxsize=0, as_numpy=False):
assert chunksize > 0
if maxsize > 0:
q = multiprocessing.Queue(maxsize=maxsize)
worker = InputQueue(q, corpus, chunksize, maxsize=maxsize, as_numpy=as_numpy)
worker.daemon = True
worker.start()
while True:
chunk = [q.get(block=True)]
if chunk[0] is None:
break
yield chunk.pop()
else:
for chunk in chunkize_serial(corpus, chunksize, as_numpy=as_numpy):
yield chunk
def smart_extension(fname, ext):
fname, oext = os.path.splitext(fname)
if oext.endswith('.bz2'):
fname = fname + oext[:-4] + ext + '.bz2'
elif oext.endswith('.gz'):
fname = fname + oext[:-3] + ext + '.gz'
else:
fname = fname + oext + ext
return fname
def pickle(obj, fname, protocol=PICKLE_PROTOCOL):
with open(fname, 'wb') as fout:
_pickle.dump(obj, fout, protocol=protocol)
def unpickle(fname):
with open(fname, 'rb') as f:
return _pickle.load(f, encoding='latin1')
def revdict(d):
return {v: k for (k, v) in dict(d).items()}
def deprecated(reason):
if isinstance(reason, str):
def decorator(func):
fmt = "Call to deprecated `{name}` ({reason})."
@wraps(func)
def new_func1(*args, **kwargs):
warnings.warn(
fmt.format(name=func.__name__, reason=reason),
category=DeprecationWarning,
stacklevel=2
)
return func(*args, **kwargs)
return new_func1
return decorator
elif inspect.isclass(reason) or inspect.isfunction(reason):
func = reason
fmt = "Call to deprecated `{name}`."
@wraps(func)
def new_func2(*args, **kwargs):
warnings.warn(
fmt.format(name=func.__name__),
category=DeprecationWarning,
stacklevel=2
)
return func(*args, **kwargs)
return new_func2
else:
raise TypeError(repr(type(reason)))
@contextmanager
def ignore_deprecation_warning():
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
yield
@deprecated("Function will be removed in 4.0.0")
def toptexts(query, texts, index, n=10):
sims = index[query]
sims = sorted(enumerate(sims), key=lambda item: -item[1])
return [(topid, topcosine, texts[topid]) for topid, topcosine in sims[:n]]
def randfname(prefix='gensim'):
randpart = hex(random.randint(0, 0xffffff))[2:]
return os.path.join(tempfile.gettempdir(), prefix + randpart)
@deprecated("Function will be removed in 4.0.0")
def upload_chunked(server, docs, chunksize=1000, preprocess=None):
start = 0
for chunk in grouper(docs, chunksize):
end = start + len(chunk)
logger.info("uploading documents %i-%i", start, end - 1)
if preprocess is not None:
pchunk = []
for doc in chunk:
doc['tokens'] = preprocess(doc['text'])
del doc['text']
pchunk.append(doc)
chunk = pchunk
server.buffer(chunk)
start = end
def getNS(host=None, port=None, broadcast=True, hmac_key=None):
import Pyro4
try:
return Pyro4.locateNS(host, port, broadcast, hmac_key)
except Pyro4.errors.NamingError:
raise RuntimeError("Pyro name server not found")
def pyro_daemon(name, obj, random_suffix=False, ip=None, port=None, ns_conf=None):
if ns_conf is None:
ns_conf = {}
if random_suffix:
name += '.' + hex(random.randint(0, 0xffffff))[2:]
import Pyro4
with getNS(**ns_conf) as ns:
with Pyro4.Daemon(ip or get_my_ip(), port or 0) as daemon:
uri = daemon.register(obj, name)
ns.remove(name)
ns.register(name, uri)
logger.info("%s registered with nameserver (URI '%s')", name, uri)
daemon.requestLoop()
def mock_data_row(dim=1000, prob_nnz=0.5, lam=1.0):
nnz = np.random.uniform(size=(dim,))
return [(i, float(np.random.poisson(lam=lam) + 1.0)) for i in range(dim) if nnz[i] < prob_nnz]
def mock_data(n_items=1000, dim=1000, prob_nnz=0.5, lam=1.0):
return [mock_data_row(dim=dim, prob_nnz=prob_nnz, lam=lam) for _ in range(n_items)]
def prune_vocab(vocab, min_reduce, trim_rule=None):
result = 0
old_len = len(vocab)
for w in list(vocab):
if not keep_vocab_item(w, vocab[w], min_reduce, trim_rule):
result += vocab[w]
del vocab[w]
logger.info(
"pruned out %i tokens with count <=%i (before %i, after %i)",
old_len - len(vocab), min_reduce, old_len, len(vocab)
)
return result
def trim_vocab_by_freq(vocab, topk, trim_rule=None):
if topk >= len(vocab):
return
min_count = heapq.nlargest(topk, vocab.values())[-1]
prune_vocab(vocab, min_count, trim_rule=trim_rule)
def merge_counts(dict1, dict2):
for word, freq in dict2.items():
if word in dict1:
dict1[word] += freq
else:
dict1[word] = freq
return dict1
def qsize(queue):
try:
return queue.qsize()
except NotImplementedError:
return -1
RULE_DEFAULT = 0
RULE_DISCARD = 1
RULE_KEEP = 2
def keep_vocab_item(word, count, min_count, trim_rule=None):
default_res = count >= min_count
if trim_rule is None:
return default_res
else:
rule_res = trim_rule(word, count, min_count)
if rule_res == RULE_KEEP:
return True
elif rule_res == RULE_DISCARD:
return False
else:
return default_res
def check_output(stdout=subprocess.PIPE, *popenargs, **kwargs):
try:
logger.debug("COMMAND: %s %s", popenargs, kwargs)
process = subprocess.Popen(stdout=stdout, *popenargs, **kwargs)
output, unused_err = process.communicate()
retcode = process.poll()
if retcode:
cmd = kwargs.get("args")
if cmd is None:
cmd = popenargs[0]
error = subprocess.CalledProcessError(retcode, cmd)
error.output = output
raise error
return output
except KeyboardInterrupt:
process.terminate()
raise
def sample_dict(d, n=10, use_random=True):
selected_keys = random.sample(list(d), min(len(d), n)) if use_random else itertools.islice(d.keys(), n)
return [(key, d[key]) for key in selected_keys]
def strided_windows(ndarray, window_size):
ndarray = np.asarray(ndarray)
if window_size == ndarray.shape[0]:
return np.array([ndarray])
elif window_size > ndarray.shape[0]:
return np.ndarray((0, 0))
stride = ndarray.strides[0]
return np.lib.stride_tricks.as_strided(
ndarray, shape=(ndarray.shape[0] - window_size + 1, window_size),
strides=(stride, stride))
def iter_windows(texts, window_size, copy=False, ignore_below_size=True, include_doc_num=False):
for doc_num, document in enumerate(texts):
for window in _iter_windows(document, window_size, copy, ignore_below_size):
if include_doc_num:
yield (doc_num, window)
else:
yield window
def _iter_windows(document, window_size, copy=False, ignore_below_size=True):
doc_windows = strided_windows(document, window_size)
if doc_windows.shape[0] == 0:
if not ignore_below_size:
yield document.copy() if copy else document
else:
for doc_window in doc_windows:
yield doc_window.copy() if copy else doc_window
def flatten(nested_list):
return list(lazy_flatten(nested_list))
def lazy_flatten(nested_list):
for el in nested_list:
if isinstance(el, collections.abc.Iterable) and not isinstance(el, str):
for sub in flatten(el):
yield sub
else:
yield el
def save_as_line_sentence(corpus, filename):
with open(filename, mode='wb', encoding='utf8') as fout:
for sentence in corpus:
line = any2unicode(' '.join(sentence) + '\n')
fout.write(line)
def effective_n_jobs(n_jobs):
if n_jobs == 0:
raise ValueError('n_jobs == 0 in Parallel has no meaning')
elif n_jobs is None:
return 1
elif n_jobs < 0:
n_jobs = max(multiprocessing.cpu_count() + 1 + n_jobs, 1)
return n_jobs
def is_empty(corpus):
if scipy.sparse.issparse(corpus):
return corpus.shape[1] == 0
if isinstance(corpus, types.GeneratorType):
return False
try:
first_doc = next(iter(corpus))
return False
except StopIteration:
return True
except Exception:
return False