示例#1
0
def divide_corpus_kfold(corpus, k):
    n = sys.maxsize
    for label in corpus.cuisines.keys():
        c = corpus.cuisines[label]
        n = min(n, len(c.recipes))

    ret = []
    prev_idx = -1
    window_sz = math.floor(n / k)
    for i in range(0, k):
        idx1 = prev_idx + 1
        idx2 = window_sz + idx1 - 1
        prev_idx = idx2
        #print('idx1=%d idx2=%d'%(idx1,idx2));
        ctrain = []
        ctest = []
        for label in corpus.cuisines.keys():
            c = corpus.cuisines[label]
            rtest = c.recipes[idx1:idx2 + 1]
            rtrain = c.recipes[0:idx1] + c.recipes[idx2 + 1:]
            ctrain.append(cuisine(label=label, recipes=rtrain))
            ctest.append(cuisine(label=label, recipes=rtest))
        cctrain = cuisine_corpus(cuisines=ctrain)
        cctest = cuisine_corpus(cuisines=ctest)
        ret.append([cctrain, cctest])
    return ret
示例#2
0
def divide_corpus(corpus,n):
	cuisines = []; 
	for label in corpus.cuisines.keys():
		c = corpus.cuisines[label]; 
		recipes = c.select_recipes(n);
		cuisines.append(cuisine(label=label,recipes=recipes));
	dcorpus = cuisine_corpus(cuisines=cuisines);
	return dcorpus;
示例#3
0
def divide_corpus(corpus, n):
    cuisines = []
    for label in corpus.cuisines.keys():
        c = corpus.cuisines[label]
        recipes = c.select_recipes(n)
        cuisines.append(cuisine(label=label, recipes=recipes))
    dcorpus = cuisine_corpus(cuisines=cuisines)
    return dcorpus
示例#4
0
def divide_corpus_kfold(corpus,k):
	n = sys.maxsize;
	for label in corpus.cuisines.keys():	
		c = corpus.cuisines[label]; 
		n = min(n,len(c.recipes));

	ret = [];
	prev_idx = -1;
	window_sz = math.floor(n/k);
	for i in range(0,k):
		idx1 = prev_idx+1; idx2 = window_sz + idx1-1; prev_idx = idx2;
		#print('idx1=%d idx2=%d'%(idx1,idx2));
		ctrain = []; ctest = [];
		for label in corpus.cuisines.keys():
			c = corpus.cuisines[label]; 
			rtest = c.recipes[idx1:idx2+1];
			rtrain = c.recipes[0:idx1] + c.recipes[idx2+1:];
			ctrain.append(cuisine(label=label,recipes=rtrain));
			ctest.append(cuisine(label=label,recipes=rtest));
		cctrain = cuisine_corpus(cuisines=ctrain);
		cctest = cuisine_corpus(cuisines=ctest);
		ret.append([cctrain,cctest]);
	return ret;
示例#5
0
def get_cuisine(fname):
	base = os.path.basename(fname);
	stext = os.path.splitext(base);
	label = stext[0];
	ext = stext[1];
	if ext!='.csv':
		return None;
	print(fname);
	with open(fname) as f:
		#lines = f.readlines();
		lines = csv.reader(f);
		c = cuisine(label=label);
		i = 0;
		for l in lines:
			try:
				i = i+1;
				c.add_recipe(recipe(l));	
			except Exception as E:
				print('lines:%s'%(i));
				print(E);
		return c;
示例#6
0
def get_cuisine(fname):
    base = os.path.basename(fname)
    stext = os.path.splitext(base)
    label = stext[0]
    ext = stext[1]
    if ext != '.csv':
        return None
    print(fname)
    with open(fname) as f:
        #lines = f.readlines();
        lines = csv.reader(f)
        c = cuisine(label=label)
        i = 0
        for l in lines:
            try:
                i = i + 1
                c.add_recipe(recipe(l))
            except Exception as E:
                print('lines:%s' % (i))
                print(E)
        return c