示例#1
0
def word_length (pylinguistObj):
	if (pylinguistObj.language == "pt"):
		#from source.syllable import silva2011
		#import source.syllable.silva2011
		from resources.syllable.silva2011 import syllable_separator

		array = []
		count_error=0
		for w in pylinguistObj.tokens:
			count=0

			w = tools.clear_string(w.encode('utf-8'))
			if len(w)>1:
				#print ('separando palavra:%s' %w_clean)
				try:
					syllables = syllable_separator.separate(w)
					leng = len(syllables)
					count+=leng
					#print ('word:%s syllables:%s %i ' %(w, syllables,leng))
				except:
					count_error+=1
					#print ('ERROR ON WORD: %s' %w)
			array.append(count)
			
		#pylinguistObj.word_length = array 			#Not working
		return array
	else:
		#print "ELSE"
		array = []
		count_error=0
		for w in pylinguistObj.tokens:
			count=0
			#w_clean = tools.clear_string(w)
			w = tools.clear_string(w.encode('utf-8'))
			if len(w)>1:
				#result = 
				#print ('separando palavra:%s' %w_clean)
				try:
					leng = syllable_counter_english(w)
					count+=leng
					#print ('word:%s syllables:%i ' %(w,leng))
				except:
					count_error+=1
					#print ('ERROR ON WORD: %s' %w)
			array.append(count)
			
		return array
示例#2
0
 def format_product(self, json):
     """ return a a dict of product field data
         Keyword arguments:
         json -- dict product data
     """
     return {
         'link':
         json.get('url'),
         'name':
         clear_string(json.get('product_name')),
         'nutri_code':
         json.get('nutrition_grade_fr'),
         'picture':
         json.get('image_front_url'),
         'fat':
         str_to_float(json.get('nutriments', {}).get('fat_100g', 'NA')),
         'saturated_fat':
         str_to_float(
             json.get('nutriments', {}).get('saturated-fat_100g', 'NA')),
         'sugars':
         str_to_float(json.get('nutriments', {}).get('sugars_100g', 'NA')),
         'salt':
         str_to_float(json.get('nutriments', {}).get('salt_100g', 'NA')),
     }
示例#3
0
def syllable_count (pylinguistObj):
	x=0
	if (pylinguistObj.language == "pt"):
		#from source.syllable import silva2011
		#import source.syllable.silva2011
		from resources.syllable.silva2011 import syllable_separator
		count=0
		count_error=0
		for w in pylinguistObj.tokens:
			w_clean = tools.clear_string(w)
			w = tools.clear_string(w.encode('utf-8'))
			if len(w)>1:
				#result = 
				#print ('separando palavra:%s' %w_clean)
				try:
					syllables = syllable_separator.separate(w)
					leng = len(syllables)
					count+=leng
					#print ('word:%s syllables:%s %i ' %(w, syllables,leng))
				except:
					count_error+=1
					#print ('ERROR ON WORD: %s' %w)
			
		pylinguistObj.syllable_count = count
		return count
	else:
		#print "ELSE"
		count=0
		count_error=0
		for w in pylinguistObj.tokens:
			#w_clean = tools.clear_string(w)
			w = tools.clear_string(w.encode('utf-8'))
			if len(w)>1:
				#result = 
				#print ('separando palavra:%s' %w_clean)
				try:
					leng = syllable_counter_english(w)
					count+=leng
					#print ('word:%s syllables:%i ' %(w,leng))
				except:
					count_error+=1
					#print ('ERROR ON WORD: %s' %w)
			
		pylinguistObj.syllable_count = count
		return count
	# try:

	# 	count = 0
	# 	vowels = 'aeiouy'
	# 	text = pylinguistObj.text.lower().strip(".:;?!)(")
	# 	if text is not None and text != "":
	# 		if text[0] in vowels:
	# 			count += 1
	# 		for index in range(1, len(text)):
	# 			if text[index] in vowels and text[index-1] not in vowels:
	# 				count += 1
	# 		if text.endswith('e'):
	# 			count -= 1
	# 		if text.endswith('le'):
	# 			count += 1
	# 		if count == 0:
	# 			count += 1
	# 		count = count - (0.1*count)
	# 	x= (round(count))
	# except:
	# 	x=0
	# pylinguistObj.syllable_count = x
	return x
示例#4
0
    def add_by_upload_file(self, qty):
        # total entry save
        entry = -1
        loop = 1
        if qty > 500:
            loop = math.floor(qty / 500)
            qty = 500

        # Read Csv file from url
        filename = os.path.join(BASE_DIR, 'product/uploads/food.csv')
        with open(filename, newline='', encoding='utf-8') as csvfile:
            # Associating header with value in a dictionary
            reader = csv.DictReader(csvfile, delimiter='\t')
            if qty == -1:
                qty = sum(1 for row in reader)

            # Max line to save
            save_qty = 0

            # Start loop to read each line
            for row in reader:
                # Create a list of food object for each line
                if row['product_name'] and row['code']:
                    # format product
                    self.add_product_list({
                        'link':
                        row['url'],
                        'name':
                        clear_string(row['product_name']),
                        # 'description': row['ingredients_text'],
                        'nutri_code':
                        row['nutrition_grade_fr'],
                        'picture':
                        row['image_url'],
                        'categories':
                        self.categories.str_to_list(row['categories_fr']),
                        # 'shops': self.shops.str_to_list(row['stores']),
                        'fat':
                        row['fat_100g'],
                        'saturated_fat':
                        row['saturated-fat_100g'],
                        'sugars':
                        row['sugars_100g'],
                        'salt':
                        row['salt_100g'],
                    })

                    # add categories to object
                    self.categories.extract(row['categories_fr'])
                    # add shops to object
                    # self.shops.extract(row['stores'])
                    save_qty += 1
                    entry += 1
                    if loop > 0 and save_qty > qty + 1:
                        # create all categories
                        self.categories.create_categories()

                        # create all shops
                        # self.shops.create_shops()

                        # create all product
                        self.create_products()
                        # reset data
                        save_qty = 0
                        loop -= 1
                        self.reset_components()

                    if loop == 1:
                        break

            if qty < 500:
                # create all categories
                self.categories.create_categories()

                # create all shops
                # self.shops.create_shops()

                # create all product
                self.create_products()
            return 'Total entry : ' + str(entry)