Last active
October 27, 2017 09:57
-
-
Save ps-team/35708b6b6fc8135a9b43c4a7dde478a8 to your computer and use it in GitHub Desktop.
A set of custom utility functions for use in App_code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Web; | |
using System.Collections.Generic; | |
using System.Text; | |
using System.Linq; | |
using Contensis.Framework.Web; | |
using Zengenti.Contensis.Delivery; | |
using Zengenti.Contensis; | |
using Zengenti.Populo; | |
namespace Custom.Utilities | |
{ | |
public class locationData | |
{ | |
/// <summary> | |
/// Check provided country code against UK and EU codes and return grouped value | |
/// </summary> | |
/// <param name="countrycode">countrycode as string</param> | |
/// <returns>string</returns> | |
public static string checkLocation(string userLocation) { | |
// List of all EU country 2 letter ISO codes | |
string[] euCodes = {"AL","BE","BG","HR","CY","CZ","DK","EE","FI","FR","DE","GR","HU","IE","IT","LV","LT","LU","MT","NL","PL","PT","RO","SK","SI","ES","SE","VA"}; | |
// Check if the user is in the UK | |
if (userLocation == "GB" || String.IsNullOrEmpty(userLocation) || userLocation == "-") { | |
return "UK"; | |
} | |
else if (euCodes.Contains(userLocation)) { | |
return "EU"; | |
} | |
else { | |
return "INT"; | |
} | |
} | |
} | |
public class stringFunctions | |
{ | |
public static string FirstLetterToUpper(string str) | |
{ | |
if (str == null) | |
return null; | |
if (str.Length > 1) | |
return char.ToUpper(str[0]) + str.Substring(1); | |
return str.ToUpper(); | |
} | |
public static string DateSuffix(int day) | |
{ | |
switch (day) | |
{ | |
case 1: | |
case 21: | |
case 31: | |
return "st"; | |
case 2: | |
case 22: | |
return "nd"; | |
case 3: | |
case 23: | |
return "rd"; | |
default: | |
return "th"; | |
} | |
} | |
} | |
public class popFunctions | |
{ | |
public static void redirectToFiltered() | |
{ | |
string path = HttpContext.Current.Request.Url.PathAndQuery; | |
string redirectURL = Contensis.Framework.Web.QueryString.AppendOrReplaceQuerystring(path, "filtered", "1"); | |
System.Web.HttpContext.Current.Response.CacheControl = "private"; | |
System.Web.HttpContext.Current.Response.BufferOutput = true; | |
System.Web.HttpContext.Current.Response.Redirect(redirectURL, false); | |
System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest(); | |
} | |
public static bool IsEnabled() | |
{ | |
return true; | |
} | |
} | |
public static class StopWords | |
{ | |
static Dictionary<string, bool> _stops = new Dictionary<string, bool> | |
{ | |
{ "a", true }, | |
{ "about", true }, | |
{ "above", true }, | |
{ "across", true }, | |
{ "after", true }, | |
{ "afterwards", true }, | |
{ "again", true }, | |
{ "against", true }, | |
{ "all", true }, | |
{ "almost", true }, | |
{ "alone", true }, | |
{ "along", true }, | |
{ "already", true }, | |
{ "also", true }, | |
{ "although", true }, | |
{ "always", true }, | |
{ "am", true }, | |
{ "among", true }, | |
{ "amongst", true }, | |
{ "amount", true }, | |
{ "an", true }, | |
{ "and", true }, | |
{ "another", true }, | |
{ "any", true }, | |
{ "anyhow", true }, | |
{ "anyone", true }, | |
{ "anything", true }, | |
{ "anyway", true }, | |
{ "anywhere", true }, | |
{ "are", true }, | |
{ "around", true }, | |
{ "as", true }, | |
{ "at", true }, | |
{ "back", true }, | |
{ "be", true }, | |
{ "became", true }, | |
{ "because", true }, | |
{ "become", true }, | |
{ "becomes", true }, | |
{ "becoming", true }, | |
{ "been", true }, | |
{ "before", true }, | |
{ "beforehand", true }, | |
{ "behind", true }, | |
{ "being", true }, | |
{ "below", true }, | |
{ "beside", true }, | |
{ "besides", true }, | |
{ "between", true }, | |
{ "beyond", true }, | |
{ "bill", true }, | |
{ "both", true }, | |
{ "bottom", true }, | |
{ "but", true }, | |
{ "by", true }, | |
{ "call", true }, | |
{ "can", true }, | |
{ "cannot", true }, | |
{ "cant", true }, | |
{ "co", true }, | |
{ "con", true }, | |
{ "could", true }, | |
{ "couldnt", true }, | |
{ "cry", true }, | |
{ "de", true }, | |
{ "describe", true }, | |
{ "detail", true }, | |
{ "do", true }, | |
{ "done", true }, | |
{ "down", true }, | |
{ "due", true }, | |
{ "during", true }, | |
{ "each", true }, | |
{ "eg", true }, | |
{ "eight", true }, | |
{ "either", true }, | |
{ "eleven", true }, | |
{ "else", true }, | |
{ "elsewhere", true }, | |
{ "empty", true }, | |
{ "enough", true }, | |
{ "etc", true }, | |
{ "even", true }, | |
{ "ever", true }, | |
{ "every", true }, | |
{ "everyone", true }, | |
{ "everything", true }, | |
{ "everywhere", true }, | |
{ "except", true }, | |
{ "few", true }, | |
{ "fifteen", true }, | |
{ "fify", true }, | |
{ "fill", true }, | |
{ "find", true }, | |
{ "fire", true }, | |
{ "first", true }, | |
{ "five", true }, | |
{ "for", true }, | |
{ "former", true }, | |
{ "formerly", true }, | |
{ "forty", true }, | |
{ "found", true }, | |
{ "four", true }, | |
{ "from", true }, | |
{ "front", true }, | |
{ "full", true }, | |
{ "further", true }, | |
{ "get", true }, | |
{ "give", true }, | |
{ "go", true }, | |
{ "had", true }, | |
{ "has", true }, | |
{ "have", true }, | |
{ "he", true }, | |
{ "hence", true }, | |
{ "her", true }, | |
{ "here", true }, | |
{ "hereafter", true }, | |
{ "hereby", true }, | |
{ "herein", true }, | |
{ "hereupon", true }, | |
{ "hers", true }, | |
{ "herself", true }, | |
{ "him", true }, | |
{ "himself", true }, | |
{ "his", true }, | |
{ "how", true }, | |
{ "however", true }, | |
{ "hundred", true }, | |
{ "i", true }, | |
{ "ie", true }, | |
{ "if", true }, | |
{ "in", true }, | |
{ "inc", true }, | |
{ "indeed", true }, | |
{ "interest", true }, | |
{ "into", true }, | |
{ "is", true }, | |
{ "it", true }, | |
{ "its", true }, | |
{ "itself", true }, | |
{ "keep", true }, | |
{ "last", true }, | |
{ "latter", true }, | |
{ "latterly", true }, | |
{ "least", true }, | |
{ "less", true }, | |
{ "ltd", true }, | |
{ "made", true }, | |
{ "many", true }, | |
{ "may", true }, | |
{ "me", true }, | |
{ "meanwhile", true }, | |
{ "might", true }, | |
{ "mill", true }, | |
{ "mine", true }, | |
{ "more", true }, | |
{ "moreover", true }, | |
{ "most", true }, | |
{ "mostly", true }, | |
{ "move", true }, | |
{ "much", true }, | |
{ "must", true }, | |
{ "my", true }, | |
{ "myself", true }, | |
{ "name", true }, | |
{ "namely", true }, | |
{ "neither", true }, | |
{ "never", true }, | |
{ "nevertheless", true }, | |
{ "next", true }, | |
{ "nine", true }, | |
{ "no", true }, | |
{ "nobody", true }, | |
{ "none", true }, | |
{ "nor", true }, | |
{ "not", true }, | |
{ "nothing", true }, | |
{ "now", true }, | |
{ "nowhere", true }, | |
{ "of", true }, | |
{ "off", true }, | |
{ "often", true }, | |
{ "on", true }, | |
{ "once", true }, | |
{ "one", true }, | |
{ "only", true }, | |
{ "onto", true }, | |
{ "or", true }, | |
{ "other", true }, | |
{ "others", true }, | |
{ "otherwise", true }, | |
{ "our", true }, | |
{ "ours", true }, | |
{ "ourselves", true }, | |
{ "out", true }, | |
{ "over", true }, | |
{ "own", true }, | |
{ "part", true }, | |
{ "per", true }, | |
{ "perhaps", true }, | |
{ "please", true }, | |
{ "put", true }, | |
{ "rather", true }, | |
{ "re", true }, | |
{ "same", true }, | |
{ "see", true }, | |
{ "seem", true }, | |
{ "seemed", true }, | |
{ "seeming", true }, | |
{ "seems", true }, | |
{ "serious", true }, | |
{ "several", true }, | |
{ "she", true }, | |
{ "should", true }, | |
{ "show", true }, | |
{ "side", true }, | |
{ "since", true }, | |
{ "sincere", true }, | |
{ "six", true }, | |
{ "sixty", true }, | |
{ "so", true }, | |
{ "some", true }, | |
{ "somehow", true }, | |
{ "someone", true }, | |
{ "something", true }, | |
{ "sometime", true }, | |
{ "sometimes", true }, | |
{ "somewhere", true }, | |
{ "still", true }, | |
{ "such", true }, | |
{ "system", true }, | |
{ "take", true }, | |
{ "ten", true }, | |
{ "than", true }, | |
{ "that", true }, | |
{ "the", true }, | |
{ "their", true }, | |
{ "them", true }, | |
{ "themselves", true }, | |
{ "then", true }, | |
{ "thence", true }, | |
{ "there", true }, | |
{ "thereafter", true }, | |
{ "thereby", true }, | |
{ "therefore", true }, | |
{ "therein", true }, | |
{ "thereupon", true }, | |
{ "these", true }, | |
{ "they", true }, | |
{ "thick", true }, | |
{ "thin", true }, | |
{ "third", true }, | |
{ "this", true }, | |
{ "those", true }, | |
{ "though", true }, | |
{ "three", true }, | |
{ "through", true }, | |
{ "throughout", true }, | |
{ "thru", true }, | |
{ "thus", true }, | |
{ "to", true }, | |
{ "together", true }, | |
{ "too", true }, | |
{ "top", true }, | |
{ "toward", true }, | |
{ "towards", true }, | |
{ "twelve", true }, | |
{ "twenty", true }, | |
{ "two", true }, | |
{ "un", true }, | |
{ "under", true }, | |
{ "until", true }, | |
{ "up", true }, | |
{ "upon", true }, | |
{ "us", true }, | |
{ "very", true }, | |
{ "via", true }, | |
{ "was", true }, | |
{ "we", true }, | |
{ "well", true }, | |
{ "were", true }, | |
{ "what", true }, | |
{ "whatever", true }, | |
{ "when", true }, | |
{ "whence", true }, | |
{ "whenever", true }, | |
{ "where", true }, | |
{ "whereafter", true }, | |
{ "whereas", true }, | |
{ "whereby", true }, | |
{ "wherein", true }, | |
{ "whereupon", true }, | |
{ "wherever", true }, | |
{ "whether", true }, | |
{ "which", true }, | |
{ "while", true }, | |
{ "whither", true }, | |
{ "who", true }, | |
{ "whoever", true }, | |
{ "whole", true }, | |
{ "whom", true }, | |
{ "whose", true }, | |
{ "why", true }, | |
{ "will", true }, | |
{ "with", true }, | |
{ "within", true }, | |
{ "without", true }, | |
{ "would", true }, | |
{ "yet", true }, | |
{ "you", true }, | |
{ "your", true }, | |
{ "yours", true }, | |
{ "yourself", true }, | |
{ "yourselves", true } | |
}; | |
static char[] _delimiters = new char[] | |
{ | |
' ', | |
',', | |
';', | |
'.' | |
}; | |
public static string RemoveStopwords(string input) | |
{ | |
// Split parameter into words | |
var words = input.Split(_delimiters, | |
StringSplitOptions.RemoveEmptyEntries); | |
// Allocate new dictionary to store found words | |
var found = new Dictionary<string, bool>(); | |
// Store results in this StringBuilder | |
StringBuilder builder = new StringBuilder(); | |
// Loop through all words | |
foreach (string currentWord in words) | |
{ | |
// Convert to lowercase | |
string lowerWord = currentWord.ToLower(); | |
// If this is a usable word, add it | |
if (!_stops.ContainsKey(lowerWord) && | |
!found.ContainsKey(lowerWord)) | |
{ | |
builder.Append(currentWord).Append(' '); | |
found.Add(lowerWord, true); | |
} | |
} | |
// Return string with words removed | |
return builder.ToString().Trim(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment