Example #1
0
        private static SearchSegmentResult SetMatchPercentage(SearchSegmentResult searchSegmentResult, SearchSettings settings, ScoreType matchScoringType)
        {
            var matchAnalysis = new MatchAnalysis.MatchAnalysis();

            foreach (var segment in searchSegmentResult.Segments)
            {
                segment.MatchPercentage = matchScoringType == ScoreType.Lookup ? matchAnalysis.GetPercentageLookUp(settings.SearchSections, segment.SourceSections, settings) : matchAnalysis.GetPercentageConcordance(settings.SearchSections, segment.SourceSections, settings);
            }

            return(searchSegmentResult);
        }
Example #2
0
        public SearchSegmentResult SearchSegment(SearchSettings settings, ScoreType matchScoringType)
        {
            var searchSegmentResult = new SearchSegmentResult();


            var result = GetSearchSegmentResult(settings);


            if (result.Trim() != string.Empty)
            {
                var resultSegmentParser = new ResultSegmentParser();
                searchSegmentResult = resultSegmentParser.ReadResult(result);

                //apply the analysis percentage
                searchSegmentResult = SetMatchPercentage(searchSegmentResult, settings, matchScoringType);

                //sort on match %
                searchSegmentResult.Segments.Sort((c1, c2) => c2.MatchPercentage.CompareTo(c1.MatchPercentage));

                //respect the limit
                var limitIndex        = settings.Limit - 1;
                var totalSegmentIndex = searchSegmentResult.Segments.Count - 1;

                if (settings.Limit > 0 && settings.Limit < searchSegmentResult.Segments.Count)
                {
                    searchSegmentResult.Segments.RemoveRange(limitIndex, totalSegmentIndex - limitIndex);
                }
            }
            else
            {
                searchSegmentResult.Status = "timed out";
                searchSegmentResult.Reason = "timed out";
            }

            return(searchSegmentResult);
        }
        internal SearchSegmentResult ReadResult(string xmlFragment)
        {
            var searchSegmentResult = new SearchSegmentResult();


            var textReader = new XmlTextReader(xmlFragment, XmlNodeType.Element, null);

            try
            {
                while (textReader.Read())
                {
                    switch (textReader.NodeType)
                    {
                    case XmlNodeType.Element:
                        if (string.Compare(textReader.Name, "result", StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            var readSubtree = textReader.ReadSubtree();
                            searchSegmentResult = ReadResult(readSubtree);
                            readSubtree.Close();
                        }
                        break;

                    case XmlNodeType.ProcessingInstruction:
                        //not required for this implementation
                        break;

                    case XmlNodeType.Comment:
                        //not required for this implementation
                        break;

                    case XmlNodeType.XmlDeclaration:
                        //not required for this implementation
                        break;

                    case XmlNodeType.Document:
                        //not required for this implementation
                        break;

                    case XmlNodeType.DocumentType:
                        //not required for this implementation
                        break;

                    case XmlNodeType.EndElement:
                        //not required for this implementation
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (textReader.ReadState != ReadState.Closed)
                {
                    textReader.Close();
                }
            }
            return(searchSegmentResult);
        }
        private static SearchSegmentResult ReadResult(XmlReader xmlReader)
        {
            var searchSegmentResult = new SearchSegmentResult {
                Segments = new List <Segment.Segment>()
            };

            while (xmlReader.Read())
            {
                switch (xmlReader.NodeType)
                {
                case XmlNodeType.Element:
                    if (string.Compare(xmlReader.Name, "status", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        var readSubtree = xmlReader.ReadSubtree();
                        searchSegmentResult.Status = ReadText(readSubtree);
                        readSubtree.Close();
                    }
                    else if (string.Compare(xmlReader.Name, "auth_key", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        var readSubtree = xmlReader.ReadSubtree();
                        searchSegmentResult.AuthKey = ReadAuthKey(readSubtree);
                        readSubtree.Close();
                    }

                    else if (string.Compare(xmlReader.Name, "reason", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        var readSubtree = xmlReader.ReadSubtree();
                        searchSegmentResult.Reason = ReadText(readSubtree);
                        readSubtree.Close();
                    }
                    else if (string.Compare(xmlReader.Name, "segment", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        var readSubtree = xmlReader.ReadSubtree();
                        var segment     = ReadSegment(readSubtree);
                        searchSegmentResult.Segments.Add(segment);
                        readSubtree.Close();
                    }
                    break;

                case XmlNodeType.Whitespace:
                    //not required for this implementation
                    break;

                case XmlNodeType.Text:
                    //not required for this implementation
                    break;

                case XmlNodeType.CDATA:
                    //not required for this implementation
                    break;

                case XmlNodeType.EntityReference:
                    //not required for this implementation
                    break;

                case XmlNodeType.EndElement:
                    //not required for this implementation
                    break;
                }
            }
            return(searchSegmentResult);
        }