protected void GetRules(string prefixText) { Dawg <string> Dawg; var PrefixText = DictionaryHelper.RemoveStressMarks(prefixText).ToLowerInvariant().Reverse(); var fileBasedDictionary = new FileBasedDictionary(Context.Server); try { using (Stream ReverseDict = fileBasedDictionary.OpenReverseIndex()) { Dawg = Dawg <string> .Load(ReverseDict, Func => { string s = Func.ReadString(); return(s == String.Empty ? null : s); }); } int PrefixLen = Dawg.GetLongestCommonPrefixLength(PrefixText); WriteJSONToResponse(Dawg.MatchPrefix(PrefixText.Take(PrefixLen)) .GroupBy(kvp => kvp.Value, kvp => kvp) .SelectMany(g => g.Take(1)) .Select(kvp => kvp.Value + DictionaryHelper.RuleLineDelimiter + new string(kvp.Key.Reverse().ToArray())) .Take(10) .ToArray()); } catch (Exception e) { WriteJSONToResponse(new [] { "Доступ к словарю в данный момент отсутствует. Возможно происходит построение индексов." }); Email.SendAdminEmail("GetRules", e.ToString()); } }
public void PersistenceTest() { var dawgBuilder = new DawgBuilder <int> (); dawgBuilder.Insert("cone", 10); dawgBuilder.Insert("bone", 10); dawgBuilder.Insert("gone", 9); dawgBuilder.Insert("go", 5); var dawg = dawgBuilder.BuildDawg(); var memoryStream = new MemoryStream(); dawg.SaveTo(memoryStream, (w, p) => w.Write(p)); var buffer = memoryStream.GetBuffer(); var rehydrated = Dawg <int> .Load(new MemoryStream (buffer), r => r.ReadInt32()); Assert.AreEqual(10, rehydrated ["cone"]); Assert.AreEqual(10, rehydrated ["bone"]); Assert.AreEqual(0, rehydrated ["cones"]); Assert.AreEqual(9, rehydrated ["gone"]); Assert.AreEqual(5, rehydrated ["go"]); Assert.AreEqual(0, rehydrated ["god"]); }
public void BoingDawg_Generate() { const string textFile = "boing_crosschecks.txt"; //const string textFile = "englishWords.txt"; const string binFile = "boingDAWG.bin"; //const string binFile = "englishDawg.bin"; string fileContents = File.ReadAllText(textFile); List <string> boingWords = Regex.Matches(fileContents, "\\w+").Select(m => m.Value).ToList(); DawgBuilder <bool> dawgBuilder = new(); foreach (string word in boingWords) { dawgBuilder.Insert(word, true); } Dawg <bool> dawg = dawgBuilder.BuildDawg(); // Computer is working. Please wait ... using (FileStream file = File.Create(binFile)) dawg.SaveTo(file); //Now read the file back in and check if a particular word is in the dictionary: Dawg <bool> dawg2 = Dawg <bool> .Load(File.Open(binFile, FileMode.Open)); }
//public static void MakeEnglishDictionary() //{ // var dawgBuilder = new DawgBuilder<bool>(); // <bool> is the value type. // // Key type is always string. // string[] lines = File.ReadLines(@"C:\Users\Simeon\Desktop\Scrabble\Scrabble\Helpers\englishWords.txt").ToArray(); // foreach (string key in new[] { "Aaron", "abacus", "abashed" }) // { // dawgBuilder.Insert(key, true); // } // var dawg = dawgBuilder.BuildDawg(); // Computer is working. Please wait ... // dawg.SaveTo(File.Create(@"C:\Users\Simeon\Desktop\Scrabble\Scrabble\Helpers\englishDawg.bin")); //} /// <summary> /// Loads a DAWG (a dictionary of words as a compact trie). /// </summary> /// <param name="language">Language of dictionary to load</param> /// <returns></returns> public static Dawg <bool> LoadDawg(GameLanguage language) { Stream fs = File.Open(language.Language + "Dawg.bin", FileMode.Open, FileAccess.Read); var dawg = Dawg <bool> .Load(fs); return(dawg); }
public static DawgSearchableIndex Deserialize(Stream stream) { using var gzipStream = new GZipStream(stream, CompressionMode.Decompress, leaveOpen: true); var allDocuments = Serializer.DeserializeWithLengthPrefix <RangePostingsList>(gzipStream, PrefixStyle.Base128); var dawg = Dawg <RangePostingsList> .Load(gzipStream, readPayload : DeserializePayload); return(new DawgSearchableIndex(dawg, allDocuments)); }
private static Dawg <bool> SaveToFileAndLoadBack(Dawg <bool> dawg) { string binFilePath = Path.GetTempFileName(); using (var file = File.OpenWrite(binFilePath)) dawg.SaveTo(file); var rehydrated = Dawg <bool> .Load(File.OpenRead(binFilePath)); return(rehydrated); }
/// <summary> /// Finds the word in the dictionary /// </summary> /// <param name="word"></param> /// <returns>TRUE if found</returns> public static bool WordExists(String word, Stream fs) { Func <BinaryReader, bool> readPayload = null; var dawgMap = Dawg <bool> .Load(fs, readPayload); if (dawgMap[word]) { return(true); } return(false); }
protected override Dawg <TPayload> GetDawg <TPayload> (DawgBuilder <TPayload> dawgBuilder) { var dawg = dawgBuilder.BuildDawg(); var memoryStream = new MemoryStream(); #pragma warning disable 612,618 dawg.SaveAsMatrixDawg(memoryStream); #pragma warning restore 612,618 var buffer = memoryStream.GetBuffer(); var rehydrated = Dawg <TPayload> .Load(new MemoryStream (buffer)); return(rehydrated); }
public void LoadDAWG() { string path = Path.Combine(_workDir, "forms.dawg"); if (File.Exists(path)) { DateTime start = DateTime.Now; _dawg = Dawg <FormInterpretations> .Load(File.Open(path, FileMode.Open), ReadPayload); Console.WriteLine("DAWG load time: {0}", DateTime.Now - start); Console.WriteLine("DAWG nodes: {0}", _dawg.GetNodeCount()); Console.WriteLine("DAWG count {0}", _dawg.Count()); } else { _rebuildDAWG(path); } }
/// <summary> /// Loads the list of valid words from the input file. /// These words are from the Collin's dictionary of valid scrabble words. /// </summary> private void LoadWords() { ValidWords = new List <string>(); string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"Resources\ods4.txt"); foreach (var w in File.ReadAllLines(path)) { ValidWords.Add(w); } var assembly = Assembly.GetExecutingAssembly(); string resourceName = assembly.GetManifestResourceNames().Single(str => str.EndsWith("ODS4_DAWG.bin")); using (Stream stream = assembly.GetManifestResourceStream(resourceName)) using (StreamReader reader = new StreamReader(stream)) { Dawg = Dawg <bool> .Load(reader.BaseStream); } }
public static Dawg <bool> LoadDawgFile(string path) { return(Dawg <bool> .Load(File.Open(path, FileMode.Open))); }
IDawg IDawgFactory.Load(Stream stream) { return(new NewDawg(Dawg <ushort> .Load(stream, r => r.ReadUInt16()))); }
IDawg IDawgFactory.Load(Stream stream) { return(new NewDawg(Dawg <ushort> .Load(stream))); }
/// <summary> /// Загружает модель из заданого потока. /// </summary> /// <param name="fs">Поток.</param> public void Load(System.IO.Stream fs) { builder = new DawgBuilder <ulong>(); dawg = Dawg <ulong> .Load(fs); }
void LoadDictionary(Stream stream) { m_dictionary = Dawg <bool> .Load(stream); }
/// <summary> /// Turn the last word typed into camel case. /// Uses "Directed Acyclic Word Graph" or "DAWG" for short. /// </summary> internal static void CamelCaseLastWord() { //MessageBox.Show("Camel Case Last Word"); Dawg <bool> dawg; try { Stream fs = File.Open("./plugins/doc/FirstUpper/FirstUpperDAWG.bin", FileMode.Open, FileAccess.Read); dawg = Dawg <bool> .Load(fs); IntPtr currentScint = PluginBase.GetCurrentScintilla(); ScintillaGateway scintillaGateway = new ScintillaGateway(currentScint); string word = getLastWord().ToLower(); var wordCharArray = word.ToCharArray(); var index = 0; var wordCount = 0; string wordSubstr; #region "Repeat steps until no more words." // See if the text is a word. // If it is not a word, we remove the last letter // until we find a word. Then we capitalize this word. // We repeat this process for the entire variable. while (true) { //MessageBox.Show("Index: " + index + "\nWord End: " + (word.Length - 1)); if (index >= word.Length - 1) { break; } // Get the substring word based on the start position. wordSubstr = word.Substring(index); while (true) { // Is the substring word an actual word? if (dawg[wordSubstr]) { // Increment the word count. ++wordCount; // Do not capitalize the first word of the variable. if (wordCount > 1) { // Capitalize word. wordCharArray[index] = Convert.ToChar(wordCharArray[index].ToString().ToUpper()); } //MessageBox.Show("Word Length: “" + word.Length + "”\nSubstr Length: “" + wordSubstr.Length + "”"); index += wordSubstr.Length; //MessageBox.Show("Substr After Caps: " + word.Substring(index)); break; } else { //MessageBox.Show("Substr before removal: “" + wordSubstr + "”\nLength: " + wordSubstr.Length); if (wordSubstr.Length > 0) { wordSubstr = wordSubstr.Remove(wordSubstr.Length - 1); //MessageBox.Show("Substr after removal: “" + wordSubstr + "”\nLength: " + wordSubstr.Length); } else { index = word.Length; break; } } } } #endregion "Repeat steps until no more words." // Replace selected word with new word. word = new string(wordCharArray); scintillaGateway.ReplaceSel(word); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }