aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Tests/ConsistencyTests/TextIndexing
diff options
context:
space:
mode:
authorLuke Pulverenti <luke.pulverenti@gmail.com>2016-07-26 13:28:47 -0400
committerLuke Pulverenti <luke.pulverenti@gmail.com>2016-07-26 13:28:47 -0400
commite4e87e24a1589af33d1a9e4258b1527b4d4902e5 (patch)
tree740a62eac05ac248b19154c1a339cf271ffeef26 /MediaBrowser.Tests/ConsistencyTests/TextIndexing
parentf539c9894c91e15189b2d9d676afa8ea08b3f179 (diff)
parentfd53bee140ca79a277a3cf4007069951d9cb5d65 (diff)
Merge branch 'dev' of https://github.com/MediaBrowser/Emby into dev
Diffstat (limited to 'MediaBrowser.Tests/ConsistencyTests/TextIndexing')
-rw-r--r--MediaBrowser.Tests/ConsistencyTests/TextIndexing/IndexBuilder.cs54
-rw-r--r--MediaBrowser.Tests/ConsistencyTests/TextIndexing/WordIndex.cs39
-rw-r--r--MediaBrowser.Tests/ConsistencyTests/TextIndexing/WordOccurrence.cs24
-rw-r--r--MediaBrowser.Tests/ConsistencyTests/TextIndexing/WordOccurrences.cs17
4 files changed, 134 insertions, 0 deletions
diff --git a/MediaBrowser.Tests/ConsistencyTests/TextIndexing/IndexBuilder.cs b/MediaBrowser.Tests/ConsistencyTests/TextIndexing/IndexBuilder.cs
new file mode 100644
index 0000000000..07c0df86c7
--- /dev/null
+++ b/MediaBrowser.Tests/ConsistencyTests/TextIndexing/IndexBuilder.cs
@@ -0,0 +1,54 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace MediaBrowser.Tests.ConsistencyTests.TextIndexing
+{
+ public class IndexBuilder
+ {
+ public const int MinumumWordLength = 4;
+
+ public static char[] WordChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToCharArray();
+
+ public static WordIndex BuildIndexFromFiles(IEnumerable<FileInfo> wordFiles, string rootFolderPath)
+ {
+ var index = new WordIndex();
+
+ var wordSeparators = Enumerable.Range(32, 127).Select(e => Convert.ToChar(e)).Where(c => !WordChars.Contains(c)).ToArray();
+ wordSeparators = wordSeparators.Concat(new[] { '\t' }).ToArray(); // add tab
+
+ foreach (var file in wordFiles)
+ {
+ var lineNumber = 1;
+ var displayFileName = file.FullName.Replace(rootFolderPath, string.Empty);
+ using (var reader = file.OpenText())
+ {
+ while (!reader.EndOfStream)
+ {
+ var words = reader
+ .ReadLine()
+ .Split(wordSeparators, StringSplitOptions.RemoveEmptyEntries);
+ ////.Select(f => f.Trim());
+
+ var wordIndex = 1;
+ foreach (var word in words)
+ {
+ if (word.Length >= MinumumWordLength)
+ {
+ index.AddWordOccurrence(word, displayFileName, file.FullName, lineNumber, wordIndex++);
+ }
+ }
+
+ lineNumber++;
+ }
+ }
+ }
+
+ return index;
+ }
+
+ }
+}
diff --git a/MediaBrowser.Tests/ConsistencyTests/TextIndexing/WordIndex.cs b/MediaBrowser.Tests/ConsistencyTests/TextIndexing/WordIndex.cs
new file mode 100644
index 0000000000..4ced812373
--- /dev/null
+++ b/MediaBrowser.Tests/ConsistencyTests/TextIndexing/WordIndex.cs
@@ -0,0 +1,39 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace MediaBrowser.Tests.ConsistencyTests.TextIndexing
+{
+ public class WordIndex : Dictionary<string, WordOccurrences>
+ {
+ public WordIndex() : base(StringComparer.InvariantCultureIgnoreCase)
+ {
+ }
+
+ public void AddWordOccurrence(string word, string fileName, string fullPath, int lineNumber, int wordIndex)
+ {
+ WordOccurrences current;
+ if (!this.TryGetValue(word, out current))
+ {
+ current = new WordOccurrences();
+ this[word] = current;
+ }
+
+ current.AddOccurrence(fileName, fullPath, lineNumber, wordIndex);
+ }
+
+ public WordOccurrences Find(string word)
+ {
+ WordOccurrences found;
+ if (this.TryGetValue(word, out found))
+ {
+ return found;
+ }
+
+ return null;
+ }
+
+ }
+}
diff --git a/MediaBrowser.Tests/ConsistencyTests/TextIndexing/WordOccurrence.cs b/MediaBrowser.Tests/ConsistencyTests/TextIndexing/WordOccurrence.cs
new file mode 100644
index 0000000000..40631f5825
--- /dev/null
+++ b/MediaBrowser.Tests/ConsistencyTests/TextIndexing/WordOccurrence.cs
@@ -0,0 +1,24 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace MediaBrowser.Tests.ConsistencyTests.TextIndexing
+{
+ public struct WordOccurrence
+ {
+ public readonly string FileName; // file containing the word.
+ public readonly string FullPath; // file containing the word.
+ public readonly int LineNumber; // line within the file.
+ public readonly int WordIndex; // index within the line.
+
+ public WordOccurrence(string fileName, string fullPath, int lineNumber, int wordIndex)
+ {
+ FileName = fileName;
+ FullPath = fullPath;
+ LineNumber = lineNumber;
+ WordIndex = wordIndex;
+ }
+ }
+}
diff --git a/MediaBrowser.Tests/ConsistencyTests/TextIndexing/WordOccurrences.cs b/MediaBrowser.Tests/ConsistencyTests/TextIndexing/WordOccurrences.cs
new file mode 100644
index 0000000000..3ba3b59167
--- /dev/null
+++ b/MediaBrowser.Tests/ConsistencyTests/TextIndexing/WordOccurrences.cs
@@ -0,0 +1,17 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace MediaBrowser.Tests.ConsistencyTests.TextIndexing
+{
+ public class WordOccurrences : List<WordOccurrence>
+ {
+ public void AddOccurrence(string fileName, string fullPath, int lineNumber, int wordIndex)
+ {
+ this.Add(new WordOccurrence(fileName, fullPath, lineNumber, wordIndex));
+ }
+
+ }
+}