public void BigramParsing_ExampleInputRepeat1000_ReturnEqual() { string input = ""; for (int i = 0; i <= 1000; i++) { input = input + "The quick brown fox and the quick blue hare. "; } Bigram bg = new Bigram(); List <string> bigrams = bg.BigramParsing(input); string[] actualResults = bigrams.ToArray(); for (int i = 0; i <= 1000; i++) { GetExpectedsBigrams(); if (i < 1000) { expectedsBigrams.Add("hare the"); } } for (int i = 0; i < expectedsBigrams.Count; i++) { Assert.AreEqual(expectedsBigrams[i], actualResults[i]); } }
public void BigramHistogram_Repleat1000_ReturnEqual() { List <string> input = new List <string>(); for (int i = 0; i < 1000; i++) { input.Add("the quick"); input.Add("quick brown"); input.Add("brown fox"); input.Add("fox and"); input.Add("and the"); input.Add("the quick"); input.Add("quick blue"); input.Add("blue hare"); } Bigram bg = new Bigram(); Dictionary <string, int> bigramResults = bg.BigramHistogram(input); List <int> Results = new List <int>(bigramResults.Values); int[] ExpectedCount = { 2000, 1000, 1000, 1000, 1000, 1000, 1000 }; for (int i = 0; i < ExpectedCount.Length; i++) { Assert.AreEqual(ExpectedCount[i], Results[i]); } }
public void BigramInstanceTest() { var unigram = new Bigram("ab"); Assert.AreEqual(2, unigram.Length); Assert.AreEqual("ab", unigram.Value); }
/// <summary> /// Calculates the Dice Coefficient for two given strings. /// </summary> /// <remarks>This is a similarity metric based on bigrams that is calculated as follows: /// * D is Dice coefficient /// * SB is Shared Bigrams /// * TBg1 is total number of bigrams in Qgram1 /// * TBg2 is total number of bigrams in Qgram2 /// * D = (2SB)/(TBg1+TBg2) /// A good Dice Coefficient value would be a value greater than 0.33</remarks> /// <param name="pStr1">First string to compare</param> /// <param name="pStr2">Second string to compare</param> /// <returns>Dice Coefficient</returns> internal static double DiceCoefficient(String pStr1, String pStr2) { // faulty input parameters if (string.IsNullOrEmpty(pStr1) || string.IsNullOrEmpty(pStr1)) { return(0.0); } var bigram1 = Bigram.Parse(pStr1); var bigram2 = Bigram.Parse(pStr2); // calculate number of shared bigrams int sharedBigrams = 0; foreach (var s1 in bigram1) { foreach (var s2 in bigram2) { if (s1.Equals(s2)) { sharedBigrams++; } } } // calculate dice coefficient double dice = Convert.ToDouble(sharedBigrams * 2) / Convert.ToDouble(bigram1.Length + bigram2.Length); return(dice); }
public void BigramHistogram_Mirror_ReturnEqual() { List <string> input = new List <string>(); input.Add("the quick"); input.Add("quick brown"); input.Add("brown fox"); input.Add("fox and"); input.Add("and the"); input.Add("the quick"); input.Add("quick blue"); input.Add("blue hare"); #region Mirror example input.Add("blue hare"); input.Add("quick blue"); input.Add("the quick"); input.Add("and the"); input.Add("fox and"); input.Add("brown fox"); input.Add("quick brown"); input.Add("the quick"); #endregion Bigram bg = new Bigram(); Dictionary <string, int> bigramResults = bg.BigramHistogram(input); List <int> Results = new List <int>(bigramResults.Values); int[] ExpectedCount = { 4, 2, 2, 2, 2, 2, 2 }; for (int i = 0; i < ExpectedCount.Length; i++) { Assert.AreEqual(ExpectedCount[i], Results[i]); } }
public void CompareToTest() { var instance = new Bigram("xy"); var higherInstance = new Bigram("ab"); var equalInstance = new Bigram("xy"); HelpersTest.AssertCompareTo(instance, higherInstance, equalInstance); }
public Api(string unigramFile, string bigramFile) { if (!File.Exists(unigramFile)) { throw new Exception(string.Format("1-gram file '{0}' is missing", unigramFile)); } this.dict = Dict.Build(unigramFile); this.bigram = new Bigram(unigramFile, bigramFile); }
public void GetSequence_Returns_Sequence() { string[] words = bigramService.SplitWords(input); var bigrams = bigramService.GetSequence(words); Bigram bigram = bigrams[0]; Assert.AreEqual(7, bigrams.Count); Assert.AreEqual("the quick", bigram.Phrase); Assert.AreEqual(2, bigram.Count); }
public void BigramParsing_ExampleInput_ReturnEqual() { string input = "The quick brown fox and the quick blue hare."; Bigram bg = new Bigram(); List <string> bigrams = bg.BigramParsing(input); string[] actualResults = bigrams.ToArray(); GetExpectedsBigrams(); for (int i = 0; i < expectedsBigrams.Count; i++) { Assert.AreEqual(expectedsBigrams[i], actualResults[i]); } }
private void CompareEqual(string input) { Bigram bg = new Bigram(); List <string> bigrams = bg.BigramParsing(input); string[] actualResults = bigrams.ToArray(); GetExpectedsBigrams(); for (int i = 0; i < expectedsBigrams.Count; i++) { Assert.AreEqual(expectedsBigrams[i], actualResults[i]); } }
public static svm_node[] CreateNode(string x, IReadOnlyList<string> vocabulary) { var node = new List<svm_node>(vocabulary.Count); int sum = 0; List<string> allWords = new List<string>(); x = x.Replace(",", ""); Bigram b = new Bigram(); String[] stopwords = new String[] { "hon.", "gentleman", "member", "friend", "lady", "a", "about", "above", "above", "across", "after", "afterwards", "again", "against", "all", "almost", "alone", "along", "already", "also", "although", "always", "am", "among", "amongst", "amoungst", "amount", "an", "and", "another", "any", "anyhow", "anyone", "anything", "anyway", "anywhere", "are", "around", "as", "at", "back", "be", "became", "because", "become", "becomes", "becoming", "been", "before", "beforehand", "behind", "being", "below", "beside", "besides", "between", "beyond", "bill", "both", "bottom", "but", "by", "call", "can", "cannot", "cant", "co", "con", "could", "couldnt", "cry", "de", "describe", "detail", "do", "done", "down", "due", "during", "each", "eg", "eight", "either", "eleven", "else", "elsewhere", "empty", "enough", "etc", "even", "ever", "every", "everyone", "everything", "everywhere", "except", "few", "fifteen", "fify", "fill", "find", "fire", "first", "five", "for", "former", "formerly", "forty", "found", "four", "from", "front", "full", "further", "get", "give", "go", "had", "has", "hasnt", "have", "he", "hence", "her", "here", "hereafter", "hereby", "herein", "hereupon", "hers", "herself", "him", "himself", "his", "how", "however", "hundred", "i", "ie", "if", "in", "inc", "indeed", "interest", "into", "is", "it", "its", "itself", "keep", "last", "latter", "latterly", "least", "less", "ltd", "made", "many", "may", "me", "meanwhile", "might", "mill", "mine", "more", "moreover", "most", "mostly", "move", "much", "must", "my", "myself", "name", "namely", "neither", "never", "nevertheless", "next", "nine", "no", "nobody", "none", "noone", "nor", "not", "nothing", "now", "nowhere", "of", "off", "often", "on", "once", "one", "only", "onto", "or", "other", "others", "otherwise", "our", "ours", "ourselves", "out", "over", "own", "part", "per", "perhaps", "please", "put", "rather", "re", "same", "see", "seem", "seemed", "seeming", "seems", "serious", "several", "she", "should", "show", "side", "since", "sincere", "six", "sixty", "so", "some", "somehow", "someone", "something", "sometime", "sometimes", "somewhere", "still", "such", "system", "take", "ten", "than", "that", "the", "their", "them", "themselves", "then", "thence", "there", "thereafter", "thereby", "therefore", "therein", "thereupon", "these", "they", "thickv", "thin", "third", "this", "those", "though", "three", "through", "throughout", "thru", "thus", "to", "together", "too", "top", "toward", "towards", "twelve", "twenty", "two", "un", "under", "until", "up", "upon", "us", "very", "via", "was", "we", "well", "were", "what", "whatever", "when", "whence", "whenever", "where", "whereafter", "whereas", "whereby", "wherein", "whereupon", "wherever", "whether", "which", "while", "whither", "who", "whoever", "whole", "whom", "whose", "why", "will", "with", "within", "without", "would", "yet", "you", "your", "yours", "yourself", "yourselves", "the" }; List<String> stop = stopwords.ToList<String>(); x = x.Replace(",", ""); x = x.ToLower(); String[] sp = x.Split(' '); String sent = ""; for (int z = 0; z < sp.Length; z++) { String word = sp[z]; if (stop.Contains(word)) { Debug.WriteLine("Stop Word"); } else { sent += word; sent += " "; } } allWords = b.getNG(sent); string[] words = allWords.ToArray(); for (int i = 0; i < vocabulary.Count; i++) { int occurenceCount = words.Count(s => String.Equals(s, vocabulary[i], StringComparison.OrdinalIgnoreCase)); if (occurenceCount == 0) continue; node.Add(new svm_node { index = i + 1, value = occurenceCount }); } return node.ToArray(); }
/// <summary> /// Calculates the Dice Coefficient for two given strings. /// </summary> /// <remarks>This is a similarity metric based on bigrams that is calculated as follows: /// * D is Dice coefficient /// * SB is Shared Bigrams /// * TBg1 is total number of bigrams in Qgram1 /// * TBg2 is total number of bigrams in Qgram2 /// * D = (2SB)/(TBg1+TBg2) /// A good Dice Coefficient value would be a value greater than 0.33</remarks> /// <param name="pStr1">First string to compare</param> /// <param name="pStr2">Second string to compare</param> /// <returns>Dice Coefficient</returns> internal static double DiceCoefficient(string pStr1, string pStr2) { // faulty input parameters if (string.IsNullOrEmpty(pStr1) || string.IsNullOrEmpty(pStr1)) { return(0.0); } var bigram1 = Bigram.Parse(pStr1); var bigram2 = Bigram.Parse(pStr2); // calculate number of shared bigrams var sharedBigrams = bigram1.Sum(s1 => bigram2.Count(s1.Equals)); // calculate dice coefficient return(Convert.ToDouble(sharedBigrams * 2) / Convert.ToDouble(bigram1.Length + bigram2.Length)); }
public IList <Bigram> CreateBigramList(IList <string> wordList) { Bigrams = new List <Bigram>(); Bigram bigram = new Bigram(); bigram.FirstWord = wordList[0]; foreach (var w in wordList.ToList().GetRange(1, wordList.Count() - 1)) { bigram.SecondWord = w; Bigrams.Add(bigram); bigram = new Bigram(); bigram.FirstWord = Bigrams.Last().SecondWord; } return(Bigrams); }
public void Bigram_ShouldCreateAnInstanceOfThisType_WhenProperArgument() { // Arrange // Act Bigram actual1 = new Bigram( new TokenizationStrategy(), ObjectMother.LabeledExample_Text1_BigramValue1 ); Bigram actual2 = new Bigram( ObjectMother.LabeledExample_Text1_BigramValue1 ); // Assert Assert.IsInstanceOf <Bigram>(actual1); Assert.IsInstanceOf <Bigram>(actual2); }
public void BigramHistogram_Opposit_ReturnEqual() { List <string> input = new List <string>(); input.Add("the quick"); input.Add("quick the"); input.Add("the quick"); input.Add("quick the"); input.Add("quick the"); Bigram bg = new Bigram(); Dictionary <string, int> bigramResults = bg.BigramHistogram(input); List <int> Results = new List <int>(bigramResults.Values); int[] ExpectedCount = { 2, 3 }; for (int i = 0; i < ExpectedCount.Length; i++) { Assert.AreEqual(ExpectedCount[i], Results[i]); } }
Bigram CipherBigram(char[,] cipherTable, Bigram bigram) { int i1 = 0, i2 = 0, j1 = 0, j2 = 0; for (int i = 0; i < cipherTable.GetLength(1); i++) { for (int j = 0; j < cipherTable.GetLength(0); j++) { if (cipherTable[i, j] == bigram.character1) { i1 = i; j1 = j; } if (cipherTable[i, j] == bigram.character2) { i2 = i; j2 = j; } } } if (i1 == i2) { bigram.character1 = cipherTable[(i1 + 1) % (cipherTable.GetLength(1)), j1]; bigram.character2 = cipherTable[(i2 + 1) % (cipherTable.GetLength(1)), j2]; } else if (j1 == j2) { bigram.character1 = cipherTable[i1, (j1 + 1) % (cipherTable.GetLength(0))]; bigram.character2 = cipherTable[i2, (j2 + 1) % (cipherTable.GetLength(0))]; } else { bigram.character1 = cipherTable[i1, j2]; bigram.character2 = cipherTable[i2, j1]; } return(bigram); }
static void Main() { const string dataFilePath = @"C:\Users\Rory\Desktop\Han.csv"; List<String> negwords = new List<String>(); List<String> poswords = new List<String>(); sentC = new List<String>(); //String negFile = "C:/Users/Rory/Desktop/negative-words.txt"; //String posFile = "C:/Users/Rory/Desktop/positive-words.txt"; //GetNeg(negwords, negFile); //GetNeg(poswords, posFile); String[] stopwords = new String[]{"hon.","gentleman","member","friend","lady","a", "about", "above", "above", "across", "after", "afterwards", "again", "against", "all", "almost", "alone", "along", "already", "also","although","always","am","among", "amongst", "amoungst", "amount", "an", "and", "another", "any","anyhow","anyone","anything","anyway", "anywhere", "are", "around", "as", "at", "back","be","became", "because","become","becomes", "becoming", "been", "before", "beforehand", "behind", "being", "below", "beside", "besides", "between", "beyond", "bill", "both", "bottom","but", "by", "call", "can", "cannot", "cant", "co", "con", "could", "couldnt", "cry", "de", "describe", "detail", "do", "done", "down", "due", "during", "each", "eg", "eight", "either", "eleven","else", "elsewhere", "empty", "enough", "etc", "even", "ever", "every", "everyone", "everything", "everywhere", "except", "few", "fifteen", "fify", "fill", "find", "fire", "first", "five", "for", "former", "formerly", "forty", "found", "four", "from", "front", "full", "further", "get", "give", "go", "had", "has", "hasnt", "have", "he", "hence", "her", "here", "hereafter", "hereby", "herein", "hereupon", "hers", "herself", "him", "himself", "his", "how", "however", "hundred", "i","ie", "if", "in", "inc", "indeed", "interest", "into", "is", "it", "its", "itself", "keep", "last", "latter", "latterly", "least", "less", "ltd", "made", "many", "may", "me", "meanwhile", "might", "mill", "mine", "more", "moreover", "most", "mostly", "move", "much", "must", "my", "myself", "name", "namely", "neither", "never", "nevertheless", "next", "nine", "no", "nobody", "none", "noone", "nor", "not", "nothing", "now", "nowhere", "of", "off", "often", "on", "once", "one", "only", "onto", "or", "other", "others", "otherwise", "our", "ours", "ourselves", "out", "over", "own","part", "per", "perhaps", "please", "put", "rather", "re", "same", "see", "seem", "seemed", "seeming", "seems", "serious", "several", "she", "should", "show", "side", "since", "sincere", "six", "sixty", "so", "some", "somehow", "someone", "something", "sometime", "sometimes", "somewhere", "still", "such", "system", "take", "ten", "than", "that", "the", "their", "them", "themselves", "then", "thence", "there", "thereafter", "thereby", "therefore", "therein", "thereupon", "these", "they", "thickv", "thin", "third", "this", "those", "though", "three", "through", "throughout", "thru", "thus", "to", "together", "too", "top", "toward", "towards", "twelve", "twenty", "two", "un", "under", "until", "up", "upon", "us", "very", "via", "was", "we", "well", "were", "what", "whatever", "when", "whence", "whenever", "where", "whereafter", "whereas", "whereby", "wherein", "whereupon", "wherever", "whether", "which", "while", "whither", "who", "whoever", "whole", "whom", "whose", "why", "will", "with", "within", "without", "would", "yet", "you", "your", "yours", "yourself", "yourselves", "the"}; List<String> stop = stopwords.ToList<String>(); var dataTable = DataTable.New.ReadCsv(dataFilePath); List<string> x = dataTable.Rows.Select(row => row["Text"]).ToList(); double[] y = dataTable.Rows.Select(row => double.Parse(row["IsPos"])).ToArray(); //var vocab = x.SelectMany(GetWords).Distinct().OrderBy(word => word).ToList(); Bigram b = new Bigram(); List<String> v = new List<string>(); String sent = ""; for(int i = 0; i < x.Count; i++) { String c = x[i].ToString(); c = c.Replace(",", ""); c = c.ToLower(); String[] sp = c.Split(' '); for (int z = 0; z < sp.Length; z++) { String word = sp[z]; if(stop.Contains(word)) { Debug.WriteLine("Stop Word"); } else { sent += word; sent += " "; } } sent = sent.Trim(); v.AddRange(b.getNG(sent)); sent = ""; } // int bound = v.Count(); //v.AddRange(negwords); // int nBound = v.Count(); //v.AddRange(poswords); // v = v.Distinct().ToList(); var problemBuilder = new TextClassificationProblemBuilder(); var problem = problemBuilder.CreateProblem(x, y, v); ProblemHelper.WriteProblem(@"C:\Users\Rory\Desktop\hanData.problem", problem); problem = ProblemHelper.ReadProblem(@"C:\Users\Rory\Desktop\hanData.problem"); const int C = 1; var model = new C_SVC(problem, KernelHelper.LinearKernel(), C); var accuracy = model.GetCrossValidationAccuracy(10); Console.WriteLine("Accuracy of the model is {0:P}", accuracy); string userInput; _predictionDictionary = new Dictionary<int, string> { { -1, "Negative" }, { 1, "Positive" } }; getSent(); for (int i = 0; i < nText.Count; i++) { userInput = nText[i].ToString(); var newX = TextClassificationProblemBuilder.CreateNode(userInput, v); var predictedY = model.Predict(newX); Console.WriteLine("The prediction is {0}", _predictionDictionary[(int)predictedY]); String pred = _predictionDictionary[(int)predictedY]; Console.WriteLine(new string('=', 50)); sentC.Add(pred); } addToO(); getNumbers(); forPeople(); using(StreamWriter file = new StreamWriter(@"C:\Users\Rory\Desktop\Peop.csv")) { file.WriteLine("PersID,Name,Positive,Negative,Party"); for(int i = 0; i < pl.Count; i++) { String line = pl[i].getID().ToString() + "," + pl[i].getName().ToString() + "," + pl[i].getPos().ToString() + "," + pl[i].getNeg().ToString() + "," + pl[i].getParty().ToString(); //var json = JsonConvert.SerializeObject(pl[i]); file.WriteLine(line); } } using (StreamWriter file = new StreamWriter(@"C:\Users\Rory\Desktop\PositiveSent.csv")) { file.WriteLine("SpeakerID,TargetID,Text,Sentiment"); for (int i = 0; i < ps.Count; i++) { String line = ps[i].getSID().ToString() + "," + ps[i].getAID().ToString() + "," + ps[i].getText().ToString() + "," + ps[i].getSent().ToString(); file.WriteLine(line); } } using (StreamWriter file = new StreamWriter(@"C:\Users\Rory\Desktop\NegativeSent.csv")) { file.WriteLine("SpeakerID,TargetID,Text,Sentiment"); for (int i = 0; i < ns.Count; i++) { String line = ns[i].getSID().ToString() + "," + ns[i].getAID().ToString() + "," + ns[i].getText().ToString() + "," + ns[i].getSent().ToString(); file.WriteLine(line); } } ns.AddRange(ps); doSwap(); var json = "{\"nodes\":"; json += JsonConvert.SerializeObject(pl); json += ","; var edge = "\"edges\":"; edge += JsonConvert.SerializeObject(nss); edge += "}"; var fullJSON = json + edge; Debug.Write(fullJSON); using (StreamWriter file = new StreamWriter(@"C:\Users\Rory\Desktop\Nodes2.JSON")) { file.Write(fullJSON); } getRels(); List<Relationship> sl = rl.OrderBy(o=>o.source).ToList(); var edges = "\"links\":"; edges += JsonConvert.SerializeObject(sl); edges += "}"; var fullJ = json + edges; using (StreamWriter file = new StreamWriter(@"C:\Users\Rory\Desktop\Rels2.JSON")) { file.Write(fullJ); } }
public void BigramExceptionTest() { var unigram = new Bigram("a"); }
public void ToStringTest() { var bigram = new Bigram("ab"); Assert.AreEqual("ab", bigram.ToString()); }