Example #1
0
        private static List <DictionaryValue> translations(string verb)
        {
            string store = "translations/"; // Path to store translations.

            if (!Directory.Exists(store))
            {
                Directory.CreateDirectory(store);
            }

            foreach (string file in Directory.GetFiles(store, "*.dict"))
            {
                List <DictionaryValue> entry = ObjectSerializer.FromFile
                                               <List <DictionaryValue> >(file);
                if (entry.Count > 0 && entry[0].EnglishWord.Equals(verb,
                                                                   StringComparison.InvariantCultureIgnoreCase))
                {
                    return(entry);
                }
            }

            List <DictionaryValue> translations =
                new WordReference(client).GetENFR_Translations(verb);

            ObjectSerializer.ToFile(translations, store + verb + ".dict");

            return(translations);
        }
Example #2
0
        public Analysis_VerseExtract(WordReference wordReference)
        {
            this.WordReference = wordReference;

            var verseReference =
                SharedData.Document.CorpusDocument[WordReference.ChapterIndex, WordReference.VerseIndex];
            int wordIndex = wordReference.WordIndex;

            this.PrecedingWords =
                verseReference
                .Words
                .Where(x =>
                       x.Index >= wordIndex - NumberOfSurroundingWordsToDisplay &&
                       x.Index < wordIndex)
                .ToArray();
            this.FollowingWords =
                verseReference
                .Words
                .Where(x =>
                       x.Index > wordIndex &&
                       x.Index <= wordIndex + NumberOfSurroundingWordsToDisplay)
                .ToArray();

            BuckwalterWord = wordReference.BuckwalterText;
        }
Example #3
0
        private async Task AddWordToReferenceTableAsync(Guid instanceId, string wordName, IEnumerable <WordInformation> wordInfo)
        {
            var wordRef = new WordReference(wordName, wordInfo);

            wordRef.Serialize();
            var tableEntity = new TableEntityAdapter <WordReference>()
            {
                PartitionKey   = instanceId.ToString(),
                RowKey         = wordName,
                OriginalEntity = wordRef,
            };

            await this.wordReferencesTable.ExecuteAsync(TableOperation.Insert(tableEntity));
        }
Example #4
0
        public IEnumerable <SearchResult> Search(SearchQuery query)
        {
            if (query == null)
            {
                throw new ArgumentNullException("query");
            }

            IDictionary <string, SearchResult> results = new Dictionary <string, SearchResult>(StringComparer.OrdinalIgnoreCase);

            if (!string.IsNullOrWhiteSpace(query.SearchTerms))
            {
                Regex           wordPattern = new Regex(@"[a-z1-9]{3,}", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                MatchCollection wordMatches = wordPattern.Matches(query.SearchTerms);

                foreach (Match match in wordMatches)
                {
                    if (s_Words.ContainsKey(match.Value))
                    {
                        WordReference wordRef = s_Words[match.Value];

                        foreach (KeyValuePair <string, int> pair in wordRef.Files)
                        {
                            FileReference fileRef = s_Files[pair.Key];
                            SearchResult  result;

                            if (results.ContainsKey(fileRef.Path))
                            {
                                result = results[fileRef.Path];
                            }
                            else
                            {
                                result         = new SearchResult();
                                result.Url     = "/Files/" + Path.GetFileName(fileRef.Path);
                                result.Title   = fileRef.Title;
                                result.Preview = fileRef.Preview;

                                results.Add(fileRef.Path, result);
                            }

                            result.Rank += pair.Value;
                        }
                    }
                }
            }

            return(results.Values.OrderByDescending(result => result.Rank));
        }
        private static void AddTheWordsToTheIndex(IEnumerable <Word> words, T key, TextIndex <T> index)
        {
            foreach (var word in words)
            {
                var wordReference = new WordReference <T> {
                    Word = word.WordText, Key = key, PhraseIndex = word.Index
                };

                if (ThisWordIsNotInTheIndex(word, index))
                {
                    AddTheNewWordToTheIndex(word, index, wordReference);
                }
                else
                {
                    AddTheNewUseOfThisWordToTheIndex(word, index, wordReference);
                }
            }
        }
Example #6
0
        private static void LoadIndex(IFilePathResolver resolver)
        {
            using (Stream indexStream = new FileStream(resolver.MapPath("~/App_Data/Index.xml"), FileMode.Open, FileAccess.Read, FileShare.None))
            {
                const string ns = "http://schemas.magurany.com/search";

                XmlSchemaSet schemas = new XmlSchemaSet();
                schemas.Add(ns, resolver.MapPath("~/App_Data/Index.xsd"));

                XmlReaderSettings settings = new XmlReaderSettings();
                settings.IgnoreComments   = true;
                settings.IgnoreWhitespace = true;
                settings.Schemas          = schemas;

                XmlReader reader = XmlReader.Create(indexStream, settings);

                reader.ReadStartElement("Index", ns);

                reader.ReadStartElement("Words", ns);

                while (reader.IsStartElement("Word", ns))
                {
                    WordReference wordRef = new WordReference();

                    reader.MoveToAttribute("Word");
                    wordRef.Word = reader.ReadContentAsString();

                    reader.MoveToAttribute("Count");
                    wordRef.Count = reader.ReadContentAsInt();

                    reader.ReadStartElement("Word", ns);

                    while (reader.IsStartElement("File", ns))
                    {
                        reader.MoveToAttribute("Path");
                        string path = reader.ReadContentAsString();

                        reader.MoveToAttribute("Count");
                        int count = reader.ReadContentAsInt();

                        wordRef.Files.Add(path, count);

                        reader.ReadStartElement("File", ns);
                    }

                    s_Words.Add(wordRef.Word, wordRef);

                    reader.ReadEndElement();
                }

                reader.ReadEndElement();

                reader.ReadStartElement("Files", ns);

                while (reader.IsStartElement("File", ns))
                {
                    FileReference fileRef = new FileReference();

                    reader.MoveToAttribute("Path");
                    fileRef.Path = reader.ReadContentAsString();

                    reader.MoveToAttribute("Title");
                    fileRef.Title = reader.ReadContentAsString();

                    reader.MoveToAttribute("Preview");
                    fileRef.Preview = reader.ReadContentAsString();

                    s_Files.Add(fileRef.Path, fileRef);

                    reader.ReadStartElement("File", ns);
                }

                reader.ReadEndElement();

                reader.ReadEndElement();
            }
        }
        private static void AddTheNewWordToTheIndex(Word word, TextIndex <T> index, WordReference <T> wordReference)
        {
            var wordReferences = new List <WordReference <T> >(new[] { wordReference });

            index.Add(word.WordText, wordReferences);
        }
 private static void AddTheNewUseOfThisWordToTheIndex(Word word, TextIndex <T> index, WordReference <T> wordReference)
 {
     index[word.WordText].Add(wordReference);
 }
Example #9
0
 public Analysis_WordUsageWithVerseExtract(WordReference wordReference)
 {
     this.Extract   = new Analysis_VerseExtract(wordReference);
     this.Reference = wordReference;
 }
Example #10
0
        static void Main(string[] args)
        {
            if (args.Length != 2)
            {
                Console.WriteLine("usage:");
                Console.WriteLine("mIndexer DirectoryToSearch Index.xml");

                return;
            }

            DirectoryInfo sourceDir = new DirectoryInfo(args[0]);
            FileInfo      indexFile = new FileInfo(args[1]);

            if (!sourceDir.Exists)
            {
                Console.WriteLine("The directory, '{0},' was not found.", sourceDir);

                return;
            }

            FileInfo[] sourceFiles = sourceDir.GetFiles("*.txt");

            if (sourceFiles.Length == 0)
            {
                Console.WriteLine("The directory, '{0},' does not contain any text files.", sourceDir);

                return;
            }

            bool proceed = false;

            while (!proceed && indexFile.Exists)
            {
                Console.Write("The file, '{0},' exists. Overwrite? [y/n] ", indexFile);

                ConsoleKeyInfo yesNo = Console.ReadKey(false);

                if (ConsoleKey.N == yesNo.Key)
                {
                    return;
                }

                proceed = ConsoleKey.Y == yesNo.Key;

                Console.WriteLine();
            }

            if (!indexFile.Directory.Exists)
            {
                indexFile.Directory.Create();
            }

            IDictionary <string, WordReference> words = new Dictionary <string, WordReference>(StringComparer.OrdinalIgnoreCase);
            IList <FileReference> files = new List <FileReference>(sourceFiles.Length);
            Regex wordPattern           = new Regex(@"[a-z1-9]{3,}", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            Regex previewPattern        = new Regex(@"[^ ]+", RegexOptions.Compiled | RegexOptions.IgnoreCase);

            foreach (FileInfo sourceFile in sourceFiles)
            {
                FileReference fileRef = new FileReference();
                fileRef.Path    = sourceFile.FullName;
                fileRef.Preview = string.Empty;
                fileRef.Title   = sourceFile.Name;

                using (Stream sourceStream = sourceFile.OpenRead())
                {
                    TextReader reader = new StreamReader(sourceStream);
                    string     line;

                    while ((line = reader.ReadLine()) != null)
                    {
                        if (!string.IsNullOrWhiteSpace(line))
                        {
                            if (fileRef.Preview.Length < 250)
                            {
                                MatchCollection previewMatches = previewPattern.Matches(line);

                                foreach (Match previewMatch in previewMatches)
                                {
                                    fileRef.Preview += previewMatch.Value + " ";
                                }
                            }

                            MatchCollection wordMatches = wordPattern.Matches(line);

                            foreach (Match match in wordMatches)
                            {
                                string        word = match.Value;
                                WordReference wordRef;

                                if (words.ContainsKey(word))
                                {
                                    wordRef = words[word];
                                }
                                else
                                {
                                    wordRef      = new WordReference();
                                    wordRef.Word = word;

                                    words.Add(word, wordRef);
                                }

                                wordRef.Count++;

                                if (wordRef.Files.ContainsKey(sourceFile.FullName))
                                {
                                    wordRef.Files[sourceFile.FullName]++;
                                }
                                else
                                {
                                    wordRef.Files.Add(sourceFile.FullName, 1);
                                }
                            }
                        }
                    }

                    files.Add(fileRef);
                }
            }

            using (Stream indexStream = indexFile.Open(FileMode.Create, FileAccess.Write, FileShare.None))
            {
                const string ns = "http://schemas.magurany.com/search";

                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;

                XmlWriter writer = XmlWriter.Create(indexStream, settings);
                writer.WriteStartDocument();
                writer.WriteStartElement("Index", ns);

                writer.WriteStartElement("Words", ns);

                foreach (WordReference wordRef in words.Values)
                {
                    writer.WriteStartElement("Word", ns);
                    writer.WriteAttributeString("Word", wordRef.Word);
                    writer.WriteAttributeString("Count", wordRef.Count.ToString());

                    foreach (KeyValuePair <string, int> pair in wordRef.Files)
                    {
                        writer.WriteStartElement("File", ns);
                        writer.WriteAttributeString("Path", pair.Key);
                        writer.WriteAttributeString("Count", pair.Value.ToString());
                        writer.WriteEndElement();
                    }

                    writer.WriteEndElement();
                }

                writer.WriteEndElement();

                writer.WriteStartElement("Files", ns);

                foreach (FileReference wordRef in files)
                {
                    writer.WriteStartElement("File", ns);
                    writer.WriteAttributeString("Path", wordRef.Path);
                    writer.WriteAttributeString("Title", wordRef.Title);
                    writer.WriteAttributeString("Preview", wordRef.Preview);
                    writer.WriteEndElement();
                }

                writer.WriteEndElement();

                writer.WriteEndElement();

                writer.Flush();
            }
        }