FunctionPatternFrequency
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RicardoSantos

//@version=5

// @description Counts the word or integer number pattern frequency on a array.
// reference:
// 		http://rosettacode.org/wiki/Word_frequency
library(title='FunctionPatternFrequency')

import RicardoSantos/DebugConsole/10 as console


// @function counts the number a pattern is repeated.
// @param pattern : array<string> : array with patterns to be counted.
// @returns
//		array<string>	: list of unique patterns.
//		array<int>		: list of counters per pattern.
//	usage:
//		count(array.from('a','b','c','a','b','a'))
export count (array<string> patterns) => //{
	array<string> _patterns = array.copy(patterns)
	array<string> _freq_patterns = array.new<string>(0)
	array<int>	  _freq_counters = array.new<int>(0)
	while array.size(_patterns) > 0
		string _pattern = array.shift(_patterns)
		int _pattern_idx = array.indexof(_freq_patterns, _pattern)
		if _pattern_idx >= 0
			array.set(_freq_counters, _pattern_idx, array.get(_freq_counters, _pattern_idx) + 1)
		else
			array.push(_freq_patterns, _pattern)
			array.push(_freq_counters, 1)
	[_freq_patterns, _freq_counters]
// example: {
//string str1 = str.lower('Cras pharetra lorem non tempus mollis orci ipsum consequat sem non finibus odio augue vitae arcu Lorem ipsum dolor sit amet consectetur adipiscing elit Integer condimentum semper semper. Ut arcu purus, condimentum a lorem ac, pretium semper risus. Aliquam sit amet magna non lorem varius pellentesque. Maecenas in enim sed purus sodales lacinia in a turpis. In hac habitasse platea dictumst. Nullam placerat, ante non imperdiet dignissim, lectus arcu bibendum nisi, eu bibendum est libero ut urna. Etiam eget neque suscipit, semper ligula id, laoreet magna. Vivamus id erat id lacus elementum pretium. Donec felis justo, dapibus at condimentum sit amet, ultricies ut enim. Nulla facilisi. Ut porttitor vel enim sed interdum. Donec sed quam viverra laoreet nisl sed interdum dui Nam suscipit eros eu velit posuere et elementum nisl consequat')
//string str2 = 'the red fox jumped the fence of the farmer and ate the chicken of the farmer'
//array<string> txt = str.split(str1, ' ')
//[fp, fc] = count(txt)
//if barstate.islast
//	console.log(
//	  str.tostring(array.slice(fp, 0, math.min(10, array.size(fp)))) + 
//	  '\n' + 
//	  str.tostring(array.slice(fc, 0, math.min(10, array.size(fc)))))
//}
//}

// @function counts the number a pattern is repeated.
// @param pattern : array<int> : array with patterns to be counted.
// @returns
//		array<int>	: list of unique patterns.
//		array<int>	: list of counters per pattern.
//	usage:
//		count(array.from(1,2,3,1,2,1))
export count (array<int> patterns) => //{
	array<int> _patterns = array.copy(patterns)
	array<int> _freq_patterns = array.new<int>(0)
	array<int> _freq_counters = array.new<int>(0)
	while array.size(_patterns) > 0
		int _pattern = array.shift(_patterns)
		int _pattern_idx = array.indexof(_freq_patterns, _pattern)
		if _pattern_idx >= 0
			array.set(_freq_counters, _pattern_idx, array.get(_freq_counters, _pattern_idx) + 1)
		else
			array.push(_freq_patterns, _pattern)
			array.push(_freq_counters, 1)
	[_freq_patterns, _freq_counters]
// example: {
array<int> int1 = array.from(1, 2, 3, 44, 1, 2, 3, 1, 2, 1)
[fp1, fc1] = count(int1)
if barstate.islast
	console.log(
	  str.tostring(array.slice(fp1, 0, math.min(10, array.size(fp1)))) + 
	  '\n' + 
	  str.tostring(array.slice(fc1, 0, math.min(10, array.size(fc1)))))
//}
//}
