<commit_before>﻿using System;
using System.Linq;
using Premotion.Mansion.Core;
using Premotion.Mansion.Core.Data;
using Premotion.Mansion.Core.Data.Facets;
using Premotion.Mansion.Core.Scripting.TagScript;
using Premotion.Mansion.Core.Types;

namespace Premotion.Mansion.Web.Portal.ScriptTags
{
	/// <summary>
	/// Group the type facets for a givens <see cref="Nodeset"/>.
	/// </summary>
	[ScriptTag(Constants.TagNamespaceUri, "groupTypeFacets")]
	public class GroupTypeFacetsTag : ScriptTag
	{
		#region Constructors
		/// <summary>
		/// 
		/// </summary>
		/// <param name="typeService"></param>
		public GroupTypeFacetsTag(ITypeService typeService)
		{
			// validate arguments
			if (typeService == null)
				throw new ArgumentNullException("typeService");

			// set value
			this.typeService = typeService;
		}
		#endregion
		#region Overrides of ScriptTag
		/// <summary>
		/// Executes this tag.
		/// </summary>
		/// <param name="context">The <see cref="IMansionContext"/>.</param>
		protected override void DoExecute(IMansionContext context)
		{
			// retrieve the nodeset
			var nodeset = GetRequiredAttribute<Nodeset>(context, "source");

			// get the taxonomy facet
			var facet = nodeset.Facets.FirstOrDefault(candidate => "type".Equals(candidate.PropertyName, StringComparison.OrdinalIgnoreCase));
			if (facet == null)
				return;

			// remove the facet
			nodeset.RemoveFacet(facet);

			// get the base types on which to group
			var groupOnTypes = GetRequiredAttribute<string>(context, "baseTypes").Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries).Select(typeName => typeService.Load(context, typeName));

			// group the types
			var grouped = facet.Values.Select(facetValue => new
			                                                {
			                                                	Type = typeService.Load(context, (string) facetValue.Value),
			                                                	Value = facetValue
			                                                }).GroupBy(type => groupOnTypes.FirstOrDefault(candidateParent => type.Type.IsAssignable(candidateParent)));

			// transform the grouped items into facet result
			var result = FacetResult.Create(context, new FacetDefinition(facet.PropertyName, facet.FriendlyName), grouped.Select(group => new FacetValue(
			                                                                                                                              	group.Key != null ? group.Key.GetTypeDefinitionLabel(context) : "misc",
			                                                                                                                              	group.Aggregate(0, (current, item) => current + item.Value.Count))
			                                                                                                      	));

			// add the facet
			nodeset.AddFacet(result);
		}
		#endregion
		#region Private Fields
		private readonly ITypeService typeService;
		#endregion
	}
}<commit_msg>* do not mix value and display value when grouping type facets<commit_after>﻿using System;
using System.Linq;
using Premotion.Mansion.Core;
using Premotion.Mansion.Core.Data;
using Premotion.Mansion.Core.Data.Facets;
using Premotion.Mansion.Core.Scripting.TagScript;
using Premotion.Mansion.Core.Types;

namespace Premotion.Mansion.Web.Portal.ScriptTags
{
	/// <summary>
	/// Group the type facets for a givens <see cref="Nodeset"/>.
	/// </summary>
	[ScriptTag(Constants.TagNamespaceUri, "groupTypeFacets")]
	public class GroupTypeFacetsTag : ScriptTag
	{
		#region Constructors
		/// <summary>
		/// 
		/// </summary>
		/// <param name="typeService"></param>
		public GroupTypeFacetsTag(ITypeService typeService)
		{
			// validate arguments
			if (typeService == null)
				throw new ArgumentNullException("typeService");

			// set value
			this.typeService = typeService;
		}
		#endregion
		#region Overrides of ScriptTag
		/// <summary>
		/// Executes this tag.
		/// </summary>
		/// <param name="context">The <see cref="IMansionContext"/>.</param>
		protected override void DoExecute(IMansionContext context)
		{
			// retrieve the nodeset
			var nodeset = GetRequiredAttribute<Nodeset>(context, "source");

			// get the taxonomy facet
			var facet = nodeset.Facets.FirstOrDefault(candidate => "type".Equals(candidate.PropertyName, StringComparison.OrdinalIgnoreCase));
			if (facet == null)
				return;

			// remove the facet
			nodeset.RemoveFacet(facet);

			// get the base types on which to group
			var groupOnTypes = GetRequiredAttribute<string>(context, "baseTypes").Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries).Select(typeName => typeService.Load(context, typeName));

			// group the types
			var grouped = facet.Values.Select(facetValue => new
			                                                {
			                                                	Type = typeService.Load(context, (string) facetValue.Value),
			                                                	Value = facetValue
			                                                }).GroupBy(type => groupOnTypes.FirstOrDefault(candidateParent => type.Type.IsAssignable(candidateParent)));

			// transform the grouped items into facet result
			var result = FacetResult.Create(context, new FacetDefinition(facet.PropertyName, facet.FriendlyName), grouped.Select(group => new FacetValue(group.Key, group.Aggregate(0, (current, item) => current + item.Value.Count))
			                                                                                                                              {
			                                                                                                                              	DisplayValue = group.Key != null ? group.Key.GetTypeDefinitionLabel(context) : "misc"
			                                                                                                                              }));

			// add the facet
			nodeset.AddFacet(result);
		}
		#endregion
		#region Private Fields
		private readonly ITypeService typeService;
		#endregion
	}
}
{'fraction_non_alphanumeric': 0.1042833607907743, 'fraction_numerical': 0.00032948929159802305, 'mean_word_length': 1.9047846889952154, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 45, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2769129', 'n_tokens_mistral': 1684, 'n_tokens_neox': 1475, 'n_words': 344}