Create a gist now

Instantly share code, notes, and snippets.

@sklivvz /Birthday.cs Secret
Created Jul 1, 2014

Embed
What would you like to do?
Randomly generates a birthday wish for me using Markov chains
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Sklivvz.Models
{
public static class Birthday
{
private static readonly Random Random = new Random();
private static readonly Dictionary<string, List<string>> Chains = new Dictionary<string, List<string>>();
private static readonly string[] Data =
{
"poweeeeerrrrrrrrrrrr auguri",
"Buon compleanno compagno di merende!!!! Buoni festeggiamenti!!!!!",
"Happy Birthday!",
"Happy B-day!",
"Eppi bordei Marco",
"Auguri :-)",
"Happy Birthday Marco!",
"Marco, tanti auguri :-)",
"Buon Compleanno!!!!",
"Auguri",
"Happy Birthday Marco!",
"Auguri",
"buon compleanno Marco",
"Happy birthday sir!",
"Ciao Marco! Auguri! :)",
"Happy B'day dear.. Have a Great day & a Beautiful year ahead! :)",
"Auguri !!!",
"auguri Marco",
"augurii",
"Tanti auguri a te, tanti auguri a te...",
"Auguri Marco di buon compleanno!",
"Well, happy birthday!",
"Ciao Asterix! Buon compleanno! Tantissimi sinceri AUGURIII!!! Felicita', benessere e tanto METAL!!!!! \\m/ :D \\m/",
"Brother, happy birthday! Have an awesome one!",
"Tanti auguri !! Gianni e Silvia",
"Muitos parabéns! Bjs",
"happy birthday!",
"Happy Birthday!",
"AUGURI AMMOREMIO!!! E la torta a me! \\m/",
"Happy birthday!! Auguri!",
"Auguri!",
"Auguriiii",
"ciao marco ! auguroni !!!! :)",
"felicitazioni",
"Auguri!!!",
"a u g u r i ; )",
"Auguri carissimo!! E chissà che prima o poi non ci si riesca a fare due chiacchiere davanti a una birra allo Chalet!! :)",
"Tanti Auguri!!!!!!!!!-)",
"Happy birthday, Marco!",
"Muitos parabéns Marcozzzzzzz",
"AAAuuuuuuuuuuGuuuuuuuuuuuuuuuuuuuuuuuuRiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii !!!!!!!",
"Happy Birthday :)",
"Buon Compleanno! Hope you are having a great day!",
"Happy birthday...",
"Auguri!!!!!!",
"Happy b'day old friend. Keep rockin'.",
"Happy Birthday!",
"Ciao cognatone!!! auguri e continua cosi che vai alla grande",
"Auguri!",
"Happy birthday Marco!!! Have an awesome day!! xxx",
"Auguri!!",
"Happy birthday!",
"Auguri :)",
"Congrats :) Have anice one!",
"auguriiiiiiiiiiiiiiiiiiiiiiiii!!!!!",
"Auguroni di buon compleanno!!!",
"Un distinto augurio di un piacevole genetliaco.",
"auguri!!!",
"auuuuggguuriiiiiiiiiiiii :-)",
"Parabéns Marco!:)",
"Happy Birthday!",
"Happy Birthday!",
"tantissimi auguri!"
};
static Birthday()
{
Train();
}
/// <summary>
/// Returns a randomly generated greeting
/// </summary>
/// <returns></returns>
public static string GreetMe()
{
var sb = new StringBuilder();
string cur = " ";
while (true)
{
cur = Predict(cur);
if (cur == " ") break;
sb.AppendFormat(" {0}", cur);
}
return sb.ToString();
}
/// <summary>
/// Picks a random value from a list
/// </summary>
/// <param name="choices"></param>
/// <returns></returns>
private static string PickOne(this IReadOnlyList<string> choices)
{
if (choices == null || choices.Count == 0) return null;
return choices[Random.Next(choices.Count)];
}
/// <summary>
/// Given the last word chosen choses a random next one
/// </summary>
/// <param name="predecessor"></param>
/// <returns></returns>
private static string Predict(string predecessor)
{
List<string> choices;
Chains.TryGetValue(predecessor, out choices);
return choices.PickOne();
}
/// <summary>
/// Adds a link to the Markow chain
/// </summary>
/// <param name="predecessor"></param>
/// <param name="successor"></param>
private static void InsertLink(string predecessor, string successor)
{
if (!Chains.ContainsKey(predecessor)) Chains[predecessor] = new List<string>();
Chains[predecessor].Add(successor);
}
/// <summary>
/// Trains the Markov chain
/// </summary>
private static void Train()
{
Data.SelectMany(datum => datum.Tokenize().FindLinks()).Map(link => InsertLink(link.Item1, link.Item2));
}
/// <summary>
/// Maps an action onto a sequence
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="items"></param>
/// <param name="action"></param>
private static void Map<T>(this IEnumerable<T> items, Action<T> action)
{
foreach (var item in items)
{
action(item);
}
}
/// <summary>
/// Given a sequence of strings, return a new sequence with ordered pairs (including " " as start and end tokens)
/// </summary>
/// <param name="tokens"></param>
/// <returns></returns>
private static IEnumerable<Tuple<string, string>> FindLinks(this IEnumerable<string> tokens)
{
if (tokens == null) yield break;
var prev = " ";
foreach (var token in tokens)
{
yield return Tuple.Create(prev, token);
prev = token;
}
yield return Tuple.Create(prev, " ");
}
/// <summary>
/// Splits a string into words
/// </summary>
/// <param name="phrase"></param>
/// <returns></returns>
private static IEnumerable<string> Tokenize(this string phrase)
{
return phrase.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
}
// Define other methods and classes here
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment