Beispiel #1
0
        public List <EntityInfo> ExtractUrls(string text)
        {
            var result = new List <EntityInfo>();

            if (!string.IsNullOrEmpty(text))
            {
                UrlExtractor.Extract(text, this.ExtractsUrlWithoutProtocol, this._tldDictionary, this._longestTldLength, this._shortestTldLength, result);
            }
            return(result);
        }
Beispiel #2
0
        public List <EntityInfo> ExtractEntities(string text)
        {
            var result = new List <EntityInfo>();

            if (!string.IsNullOrEmpty(text))
            {
                UrlExtractor.Extract(text, this.ExtractsUrlWithoutProtocol, this._tldDictionary, this._longestTldLength, this._shortestTldLength, result);
                HashtagExtractor.Extract(text, result);
                MentionExtractor.Extract(text, true, result);
                CashtagExtractor.Extract(text, result);
                RemoveOverlappingEntities(result);
            }
            return(result);
        }
Beispiel #3
0
        public List <EntityInfo> ExtractHashtags(string text, bool checkUrlOverlap)
        {
            var result = new List <EntityInfo>();

            if (!string.IsNullOrEmpty(text))
            {
                HashtagExtractor.Extract(text, result);

                if (checkUrlOverlap && result.Count > 0)
                {
                    UrlExtractor.Extract(text, this.ExtractsUrlWithoutProtocol, this._tldDictionary, this._longestTldLength, this._shortestTldLength, result);
                    RemoveOverlappingEntities(result);
                    result.RemoveAll(x => x.Type != EntityType.Hashtag);
                }
            }
            return(result);
        }
        private void AppendText(DateTime dateTime, string sender, string senderInfo, string text, Color color)
        {
            // Create a textrange at the very end of the chat text box, extend the range with the new text
            var timestampTextRange = new TextRange(chatTextBox.Document.ContentEnd, chatTextBox.Document.ContentEnd)
            {
                Text = string.Format("[{0}] ", dateTime)
            };

            // Colorize the timestamp
            timestampTextRange.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(color));
            timestampTextRange.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Normal);

            var senderTextRange = new TextRange(chatTextBox.Document.ContentEnd, chatTextBox.Document.ContentEnd)
            {
                Text = string.Format("{0}", sender)
            };

            // Colorize the sender and make it bold
            senderTextRange.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(color));
            senderTextRange.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);

            var senderInfoTextRange = new TextRange(chatTextBox.Document.ContentEnd, chatTextBox.Document.ContentEnd)
            {
                Text = string.IsNullOrWhiteSpace(senderInfo) ? ": " : string.Format(" {0}: ", senderInfo)
            };

            // Colorize the sender info
            senderInfoTextRange.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(color));
            senderInfoTextRange.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Normal);

            // Try to extract URLs and make them clickable
            foreach (var partialText in UrlExtractor.Extract(text))
            {
                if (partialText.Value != null)
                {
                    var linkTextRange = new TextRange(chatTextBox.Document.ContentEnd, chatTextBox.Document.ContentEnd)
                    {
                        Text = partialText.Key
                    };
                    var link = new Hyperlink(linkTextRange.Start, linkTextRange.End);
                    link.NavigateUri      = partialText.Value;
                    link.RequestNavigate += LinkRequestNavigate;
                }
                else
                {
                    var regularTextRange = new TextRange(chatTextBox.Document.ContentEnd, chatTextBox.Document.ContentEnd)
                    {
                        Text = partialText.Key
                    };
                    // Colorize the text
                    regularTextRange.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(color));
                    regularTextRange.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Normal);
                }
            }

            // Add a new line and reset styles
            var resetTextRange = new TextRange(chatTextBox.Document.ContentEnd, chatTextBox.Document.ContentEnd)
            {
                Text = Environment.NewLine
            };

            resetTextRange.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Normal);

            // Scroll to end of chat text if ScrollLock isn't on
            var scrollLock = (((ushort)GetKeyState(0x91)) & 0xffff) != 0;

            if (!scrollLock)
            {
                chatTextBox.ScrollToEnd();
            }
        }