Exemple #1
0
        private Triplets GetSubjectPredicateObjectTriplets(List <Chunk> chunks)
        {
            var triplets = new Triplets();
            var subject  = chunks.Find(c => c.tag == "NP");

            if (subject != null)
            {
                triplets.subject = new Subject()
                {
                    chunk = subject, confidence = 1
                };
            }

            var predicate = chunks.Find(c => c.tag == "VP");

            if (predicate != null)
            {
                triplets.predicate = new Predicate()
                {
                    chunk = predicate, confidence = 1
                };
            }

            var objectChunk = chunks.FindLast(c => c.tag == "NP");

            if (objectChunk != null)
            {
                triplets.objectTriplet = new ObjectTriplet()
                {
                    chunk = objectChunk, confidence = 1
                };
            }
            return(triplets);
        }
        private double getTripletsScore(Triplets targetTriplets, Triplets triplets)
        {
            var subjectScore = 0.0;

            foreach (var token in targetTriplets.subject.chunk.tokens)
            {
                subjectScore += tokenScoreService.getTokenScore(token, triplets.subject.chunk.tokens);
            }
            var maxSubjectScore = getMaxTripletScore(targetTriplets.subject.chunk.tokens);

            var predicateScore    = 0.0;
            var maxPredicateScore = 0.0;

            if (targetTriplets.predicate != null)
            {
                foreach (var token in targetTriplets.predicate.chunk.tokens)
                {
                    if (triplets.predicate != null)
                    {
                        predicateScore += tokenScoreService.getTokenScore(token, triplets.predicate.chunk.tokens);
                    }
                }
                maxPredicateScore = getMaxTripletScore(targetTriplets.predicate.chunk.tokens);
            }

            var objectScore = 0.0;

            foreach (var token in targetTriplets.objectTriplet.chunk.tokens)
            {
                objectScore += tokenScoreService.getTokenScore(token, triplets.objectTriplet.chunk.tokens);
            }
            var maxObjectScore = getMaxTripletScore(targetTriplets.objectTriplet.chunk.tokens);

            return(((subjectScore / maxSubjectScore) + (predicateScore / maxPredicateScore) + (objectScore / maxObjectScore)) / 3.0);
        }
Exemple #3
0
        public void CalculateFrequencyOfSpecifiedItemsWhen_IsSortedIsTrue()
        {
            var nestedList = new List <List <string> >
            {
                new List <string> {
                    "1", "2", "3"
                },
                new List <string> {
                    "1", "2", "3"
                }
            };
            var itemsToFind = new List <string> {
                "1", "2", "3"
            };
            var expected = new Triplets <string> {
                Item = "1", Item2 = "2", Item3 = "3", Frequency = 2
            };

            var actual = nestedList.CalculateTriplets(itemsToFind, true);

            var actualStr   = JsonConvert.SerializeObject(actual);
            var expectedStr = JsonConvert.SerializeObject(expected);

            Assert.Equal(expectedStr, actualStr);
        }
Exemple #4
0
        public void ReturnCountOf_SpecifiedValuesInCollection()
        {
            var nestedList = new List <List <string> >
            {
                new List <string> {
                    "1", "2", "3"
                },
                new List <string> {
                    "1", "2", "3"
                }
            };
            var itemsToFind = new List <string> {
                "1", "2", "3"
            };
            var expected = new Triplets <string> {
                Item = "1", Item2 = "2", Item3 = "3", Frequency = 2
            };

            var actual = nestedList.CalculateTriplets(itemsToFind);

            var actualStr   = JsonConvert.SerializeObject(actual);
            var expectedStr = JsonConvert.SerializeObject(expected);

            Assert.Equal(expectedStr, actualStr);
        }
Exemple #5
0
        private Triplets GetIsQuestionTriplets(Sentence sentence)
        {
            var questionStartIndex = sentence.tokens.FindIndex(t => _isQuestionStarters.Contains(t.Lexeme.ToLower()));
            var subjectStartIndex  = sentence.tokens.FindIndex(questionStartIndex + 1, t => _pronounsAndNouns.Contains(t.POSTag));
            var objectStartIndex   = sentence.tokens.FindIndex(subjectStartIndex + 1, t => _pronounsAndNouns.Contains(t.POSTag));

            var triplets  = new Triplets();
            var predicate = sentence.tokens[questionStartIndex];

            if (predicate != null)
            {
                var predicateChunk = new Chunk
                {
                    tokens = new List <Token> {
                        predicate
                    },
                    tag = predicate.POSTag
                };
                triplets.predicate = new Predicate()
                {
                    chunk = predicateChunk, confidence = 1
                };
            }

            var subject = sentence.tokens[subjectStartIndex];

            if (subject != null)
            {
                var subjectChunk = new Chunk
                {
                    tokens = new List <Token> {
                        subject
                    },
                    tag = subject.POSTag
                };
                triplets.subject = new Subject()
                {
                    chunk = subjectChunk, confidence = 1
                };
            }

            var objectToken = sentence.tokens[objectStartIndex];

            if (objectToken != null)
            {
                var objectChunk = new Chunk
                {
                    tokens = new List <Token> {
                        objectToken
                    },
                    tag = objectToken.POSTag
                };
                triplets.objectTriplet = new ObjectTriplet()
                {
                    chunk = objectChunk, confidence = 1
                };
            }
            return(triplets);
        }
Exemple #6
0
        public Triplets GetQuestionTriplets(Sentence sentence)
        {
            var triplets = new Triplets();

            if (IsQuestionPattern(sentence)) //Is he a teacher?
            {
                triplets = GetIsQuestionTriplets(sentence);
            }

            //Were dinosaurs big?

            return(triplets);
        }
Exemple #7
0
        public Triplets GetReplyTriplets(Sentence sentence)
        {
            var triplets = new Triplets();

            //TODO: other patterns
            if (IsSubjectPredicateObjectPattern(sentence.chunks))
            {
                triplets = GetSubjectPredicateObjectTriplets(sentence.chunks);
            }
            else if (IsSubjectObjectPattern(sentence.chunks))
            {
                triplets = GetSubjectObjectTriplets(sentence.chunks);
            }
            return(triplets);
        }
        public Triplets GetSentenceTriplets(Sentence sentence)
        {
            var triplets = new Triplets();

            if (!sentence.interrogative)
            {
                triplets = _replyTripletService.GetReplyTriplets(sentence);
            }
            else
            {
                triplets = _questionTripletService.GetQuestionTriplets(sentence);
            }

            return(triplets);
        }
        public double getBestTripletScore(Triplets targetTriplets, List <Sentence> sentences)
        {
            var bestScore = 0.0;

            foreach (var sentence in sentences)
            {
                if (sentence.triplets.subject != null)
                {
                    var tripletScore = getTripletsScore(targetTriplets, sentence.triplets);

                    if (tripletScore > bestScore)
                    {
                        bestScore = tripletScore;
                    }
                }
            }
            return(bestScore);
        }
Exemple #10
0
            public override bool Equals(Object o)
            {
                Triplets t = (Triplets)o;

                return(t.a == a && t.b == b && t.c == c);
            }
Exemple #11
0
        public static IList <IList <int> > ThreeSum(int[] nums)
        {
            HashSet <Triplets>   myHashSet = new HashSet <Triplets>();
            IList <IList <int> > ans       = new List <IList <int> >();
            Dictionary <int, List <Couples> > myDictionary = new Dictionary <int, List <Couples> >();

            //bruteforce
            //         for(int i=0;i<nums.Length-2;i++){
            //             for(int j=i+1;j<nums.Length-1;j++){
            //                 for(int k=j+1; k<nums.Length;k++){
            //                     if((nums[i] + nums [j] + nums[k])==0){
            //                         List<int> tempList= new List<int>();

            //                         Triplets obj= new Triplets(nums[i],nums[j],nums[k]);

            //                         if(!myHashSet.Contains(obj)){
            //                             myHashSet.Add(obj);
            //                         }

            //                     }
            //                 }
            //             }
            //         }


            for (int i = 0; i < nums.Length - 1; i++)
            {
                for (int j = i + 1; j < nums.Length; j++)
                {
                    int sum = nums[i] + nums[j];

                    if (!myDictionary.ContainsKey(sum))
                    {
                        List <Couples> l = new List <Couples>();
                        l.Add(new Couples(i, j));
                        myDictionary.Add(sum, l);
                    }
                    else
                    {
                        List <Couples> l = myDictionary[sum];
                        l.Add(new Couples(i, j));
                    }
                }
            }

            foreach (int key in myDictionary.Keys)
            {
                int targetNumber = 0 - key;
                if (!nums.Contains(targetNumber))
                {
                    continue;
                }
                int index_targetNumber = Array.IndexOf(nums, targetNumber);

                List <Couples> CurrCouple = myDictionary[key];

                foreach (Couples c in CurrCouple)
                {
                    if (index_targetNumber == c.a || index_targetNumber == c.b)
                    {
                        continue;
                    }
                    Triplets obj = new Triplets(nums[c.a], nums[c.b], targetNumber);

                    if (!myHashSet.Contains(obj))
                    {
                        myHashSet.Add(obj);
                    }
                }
            }



            foreach (Triplets t in myHashSet)
            {
                IList <int> tempList = new List <int>();
                tempList.Add(t.a);
                tempList.Add(t.b);
                tempList.Add(t.c);
                ans.Add(tempList);
            }

            return(ans);
        }