本文整理汇总了Python中composes.semantic_space.space.Space.get_neighbours方法的典型用法代码示例。如果您正苦于以下问题:Python Space.get_neighbours方法的具体用法?Python Space.get_neighbours怎么用?Python Space.get_neighbours使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类composes.semantic_space.space.Space
的用法示例。
在下文中一共展示了Space.get_neighbours方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TextExtraction
# 需要导入模块: from composes.semantic_space.space import Space [as 别名]
# 或者: from composes.semantic_space.space.Space import get_neighbours [as 别名]
def TextExtraction(text,nthreads):
pattern = re.compile('\W')
# Split the text into data parts
data=[]; # Data container. This one is a list whose elements are list of
# tokens that will be proccessed by a thred. This token itself
#is a list or tokens inside a document. It is a list of list of list
for i in xrange(0,nthreads):
data.append([]);
# Push data to list
ln_count=0;
text2 = set([]);
for ln in set(text):
ln = ln.replace(".", "").replace(",", "").replace("-", " ");
ln = re.sub(pattern, ' ', ln.strip());
ln = re.sub(' +',' ',ln);
text2.add(ln.strip(" \n"));
# print text2;
for line in text2:
ln_count+=1;
tokens=line.strip().split(" ");
data[ln_count%nthreads-1].append(tokens);
print " Splited data to ",len(data), " parts";
# for ds in data:
# print len(ds);
# Now start the threads
threads=[];
try:
for i in xrange(0,nthreads):
new_thread = Extract_vectors("Thread-"+str(i),data[i]);
new_thread.start();
threads.append(new_thread);
except:
print "Error: unable to run multithreading";
traceback.print_exc();
# Wait for all threads to complete
for t in threads:
t.join()
# Release data
data=None;
#my_space=None;
# Collect results
final_matrix = threads[0].resMatrix;
rows = threads[0].proceedRows;
total_succecced = threads[nthreads-1].nsucceed;
total_proccessed = threads[nthreads-1].size;
print " --> Size of result matrix of ",threads[nthreads-1].threadName\
,"is ",threads[nthreads-1].resMatrix.get_shape();
for i in xrange(1,nthreads,1):
final_matrix = final_matrix.vstack(threads[i].resMatrix); # Stack mats
total_succecced += threads[i].nsucceed;
total_proccessed += threads[i].size;
print " --> Size of result matrix of ", threads[i].threadName ,\
"is ",threads[i].resMatrix.get_shape();
rows.extend(threads[i].proceedRows);
print " Size of vector final matrix: ", final_matrix.get_shape();
print " Successed ",total_succecced, " in ",total_proccessed," with "\
, total_proccessed - total_succecced,\
" unsupported document(s) was corrected.";
cols = [i for i in xrange(0,space_dim)];
try:
print " --> Starting space build. Size to matrix " + \
str(final_matrix.get_shape()) + \
". Size of rows " + str(len(rows)) +\
". Size of columns " + str(len(cols));
heads_space = Space(final_matrix,rows,cols,None,None,[],None);
print " Toy neigbors: ",heads_space.get_neighbours(rows[0], 3\
,CosSimilarity());
# print heads_space.get_id2row();
except:
print "/!\ Error building semantic space!"
traceback.print_exc();
# Now write to file
# space_out = open(sys.argv[3]+".space", 'w')
return heads_space;
# pickle.dump(heads_space, space_out);
# space_out.close(); # Close space file
print " Saved the space file!"