private static Lib.Dawg BuildDawg() { var db = new Data.Database(); var dawg = new Lib.Dawg(); foreach (var w in db.GetWords()) { dawg.Insert(w.ToUpper()); } dawg.Finish(); return(dawg); }
public IEnumerable <Edge> Get(int numWords = 10, int batchSize = 2) { var dawg = new Lib.Dawg(); var db = new Data.Database(); foreach (var w in db.GetRandomWords(numWords, batchSize)) { dawg.Insert(w); } dawg.Finish(); return(Serialize(dawg.Root)); }
public async Task NewMessage(int numWords, int batchSize) { var dawg = new Lib.Dawg(); var db = new Data.Database(); foreach (var w in db.GetRandomWords(numWords, batchSize)) { dawg.Insert(w); await Clients.Caller.SendAsync("MessageReceived", Serialize(dawg.Root)); await Task.Delay(2000); } dawg.Finish(); await Clients.Caller.SendAsync("MessageReceived", Serialize(dawg.Root)); //return Serialize(dawg.Root); }
static void Main(string[] args) { var s = new Scrabble.Scrabble(new List <Square>()); s.PrintBoard(s.Board); Console.WriteLine(""); s.PrintBoard(s.Transpose(s.Board)); return; var dawg = new Lib.Dawg(); // var root = new Node(); // var uncheckedNodes = new List<(Node, char, Node)>(); // var minimizedNodes = new HashSet<Node>(); // var previousWord = ""; const string TargetFile = "enable.txt"; const string DestinationFile = "output.json"; const int WordCount = 100; const int BatchSize = 10; var rand = new Random(); //var words = new string[] { "cities", "city", "pities", "pity" }; //, pities, pity" }; //var words = new string[] { "blip", "cat", "catnip", "cats" }; //var words = new string[] { "cat", "catnip", "zcatnip" }; // foreach (var w in words) { // dawg.Insert(w); // } // dawg.Finish(); // printMap(dawg.Root); // //Console.WriteLine($"Nodes {dawg.Nodes.Count()}"); // foreach (var n in dawg.Nodes) // Console.WriteLine($"{n.Id}"); // Console.WriteLine($"Node count {dawg.NodeCount}"); // Console.WriteLine($"Edge count {dawg.EdgeCount}"); // return; using (var r = new StreamReader(TargetFile)) { var count = 0; var batch = BatchSize; while (r.Peek() >= 0 && count < WordCount) { if (rand.Next(1, 2001) > 1 && batch == 0) { r.ReadLine(); continue; } if (batch == 0) { batch = BatchSize; } var word = r.ReadLine(); dawg.Insert(word); count++; batch--; Console.WriteLine($"Adding {word} ({count}/{WordCount})"); } Console.WriteLine($"Wrote {count} of {WordCount} words. Node count: {dawg.NodeCount}"); } dawg.Finish(); Console.WriteLine($"Final Node count {dawg.NodeCount}"); Console.WriteLine($"Edge count {dawg.EdgeCount}"); // minimize(0); // foreach (var w in words) { // var position = root; // foreach (var c in w) { // if (position.Children.ContainsKey(c)) { // position = position.Children[c]; // continue; // } // //position = findStart(c, position) ?? position; // position = position.AddChild(c); // } // position.EndOfWord = true; // } //printJson(root); //printMap(root); //printMinimized(); // Console.WriteLine(serializeMap(root)); // string serialize(Node root) { // var settings = new JsonSerializerSettings(); // settings.Formatting = Formatting.Indented; // settings.ContractResolver = new DictionaryAsArrayResolver(); // return JsonConvert.SerializeObject(root, settings); // } // void insert(string word) { // var commonPrefix = 0; // foreach (var i in Enumerable.Range(0, Math.Min(word.Length, previousWord.Length))) { // if (word[i] != previousWord[i]) break; // commonPrefix++; // } // minimize(commonPrefix); // var node = (uncheckedNodes.Count() == 0) ? root : uncheckedNodes[^1].Item3; // foreach (var c in word.Substring(commonPrefix)) { // var next = new Node(); // node.Children[c] = next; // uncheckedNodes.Add((node, c, next)); // node = next; // } // node.EndOfWord = true; // previousWord = word; // } void printMap(Node root) { var stack = new Stack <Node>(); var done = new HashSet <int>(); stack.Push(root); while (stack.Count() > 0) { var content = new StringBuilder(); var node = stack.Pop(); if (done.Contains(node.Id)) { continue; } content.AppendLine($"{node.Id,-5} {node.ToString()}"); foreach (var(key, child) in node.Children.OrderByDescending(x => x.Key)) { content.AppendLine($"{key,5} goto {child.Id}"); stack.Push(child); done.Add(node.Id); } Console.WriteLine(content.ToString()); } } // string serializeMap(Node root) { // var stack = new Stack<Node>(); // var done = new HashSet<int>(); // var links = new List<Edge>(); // stack.Push(root); // while (stack.Count() > 0) { // var node = stack.Pop(); // if (done.Contains(node.Id)) continue; // if (node.Children.Count == 0) // links.Add(new Edge(new Vertex(node), null, null)); // foreach (var (key, child) in node.Children.OrderByDescending(x => x.Key)) { // links.Add(new Edge(new Vertex(node), new Vertex(child), key)); // stack.Push(child); // done.Add(node.Id); // } // } // using (StreamWriter writer = new StreamWriter(DestinationFile)) // using (JsonTextWriter jsonWriter = new JsonTextWriter(writer)) // { // JsonSerializer ser = new JsonSerializer(); // ser.Serialize(jsonWriter, links); // jsonWriter.Flush(); // } // return ""; // //return JsonConvert.SerializeObject(links, Formatting.Indented); // } // void minimize(int downTo) { // for (var i = uncheckedNodes.Count() - 1; i > downTo - 1; i--) { // var (parent, letter, child) = uncheckedNodes[i]; // if (minimizedNodes.Contains(child)) { // minimizedNodes.TryGetValue(child, out Node val); // parent.Children[letter] = val; // } // else // minimizedNodes.Add(child); // uncheckedNodes.Remove(uncheckedNodes[^1]); // } // } // void printJson(Node root) { // Console.WriteLine(serialize(root)); // } // void printList(Node root, int level = 1) { // Console.WriteLine($"{level}:{root.Key}"); // foreach (var c in root.Children) // printList(c.Value, level + 1); // } // Node findStart(char c, Node root) { // if (root == null) return null; // if (root.Key == c) return root; // foreach (var p in root.Children) { // if (p.Key == c) return p.Value; // } // return null; // } }
//public string Rack = "HACKER?"; public Scrabble(List <Square> tiles, string rack = "", Lib.Dawg wordList = null) { if (wordList == null) { BuildDawg(); } else { dawg = wordList; } // TODO: Fix this, need to elimnate the border internally because needing to change // offset means users have to know it's offset tiles.ForEach(t => t.Position.Offset(1, 1)); BuildBlankBoard(tiles); // Board[1, 4].Tile = 'Z'; // Board[2, 4].Tile = 'A'; // Board[2, 5].Tile = 'S'; // Board[1, 2].Tile = 'J'; // Board[2, 2].Tile = 'A'; // Board[3, 2].Tile = 'Y'; // Board[2, 3].Tile = 'S'; // Board[2, 4].Tile = 'S'; // Board[4, 2].Tile = 'S'; // Board[4, 3].Tile = 'A'; // Board[4, 4].Tile = 'D'; // Board[1, 1].Tile = 'B'; // Board[1, 2].Tile = 'R'; // Board[1, 3].Tile = 'I'; // Board[1, 4].Tile = 'C'; // Board[1, 5].Tile = 'K'; // Board[2, 1].Tile = 'A'; // Board[3, 1].Tile = 'D'; // Board[3, 2].Tile = 'I'; // Board[3, 3].Tile = 'E'; // Board[7, 3].Tile = 'T'; // Board[7, 4].Tile = 'U'; // Board[7, 5].Tile = 'B'; // Board[8, 4].Tile = 'S'; // Board[8, 5].Tile = 'A'; // Board[8, 6].Tile = 'I'; // Board[8, 7].Tile = 'N'; // Board[8, 8].Tile = 'T'; // Board[7, 8].Tile = 'E'; // Board[7, 9].Tile = 'N'; // Board[7, 10].Tile = 'A'; // Board[7, 11].Tile = 'M'; // Board[7, 12].Tile = 'E'; // Board[7, 13].Tile = 'L'; // Board[4, 14].Tile = 'F'; // Board[5, 14].Tile = 'O'; // Board[6, 14].Tile = 'G'; // Board[7, 14].Tile = 'S'; // Board[3, 15].Tile = 'M'; // Board[4, 15].Tile = 'I'; // Board[5, 15].Tile = 'X'; var transposedBoard = Transpose(Board); BuildAnchorList(Board); BuildAnchorList(transposedBoard); ApplyAnchorsToBoard(Board); ApplyAnchorsToBoard(transposedBoard, true); CalculateAllCrossChecks(Board); CalculateAllCrossChecks(transposedBoard); PrintBoard(transposedBoard); for (var r = 0; r < MAX_ROWS; r++) { ProcessRow(Board, r, new Rack(rack)); ProcessRow(transposedBoard, r, new Rack(rack)); } }