コード例 #1
0
        public void ExtractUrlsWithIndicesTest()
        {
            List <string> failures = new List <string>();

            foreach (dynamic test in LoadTestSection <dynamic>("urls_with_indices"))
            {
                try
                {
                    List <Extractor.Entity> actual = extractor.ExtractURLsWithIndices(test.text);
                    for (int i = 0; i < actual.Count; i++)
                    {
                        Extractor.Entity entity = actual[i];
                        Assert.AreEqual(test.expected[i].url, entity.Value);
                        Assert.AreEqual(test.expected[i].indices[0], entity.Start);
                        Assert.AreEqual(test.expected[i].indices[1], entity.End);
                    }
                }
                catch (Exception)
                {
                    failures.Add(test.description + ": " + test.text);
                }
            }
            if (failures.Any())
            {
                Assert.Fail(string.Join("\n", failures));
            }
        }
コード例 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="text">Target of string. Should be normalized 'NormalizationForm.FormC'</param>
        /// <returns></returns>
        public int GetTweetLength(String text)
        {
            try
            {
                //text = text.Normalize(NormalizationForm.FormC);
            }
            catch { }

            int length = new StringInfo(text).LengthInTextElements;

            foreach (Extractor.Entity urlEntity in __Extractor.ExtractURLsWithIndices(text))
            {
                // Subtract the length of the original URL
                length -= (urlEntity.End - urlEntity.Start);

                // Add `ShortUrlLengthHttps` characters for URL starting with https:// Otherwise add `ShortUrlLength` characters
                length += urlEntity.Value.ToLower().StartsWith("https://") ? ShortUrlLengthHttps : ShortUrlLength;
            }
            return(length);
        }