コード例 #1
0
        public static IDictionary <string, int> TopNWords(string document, int n, HashSet <string> stopwords, CultureInfo culture = null)
        {
            if (document == null)
            {
                throw new ArgumentNullException("document");
            }

            culture = CultureUtils.GetCurrentCultureIfNull(culture);

            bool dontUseStopwords = stopwords == null;

            IDictionary <string, int> result = new Dictionary <string, int>(dontUseStopwords ? StringComparer.Create(culture, true) : stopwords.Comparer);

            string[] words = document.Split(s_WordSeparators, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < words.Length; i++)
            {
                string word = words[i];
                if (dontUseStopwords || !stopwords.Contains(word))
                {
                    int count;
                    if (result.TryGetValue(word, out count))
                    {
                        result[word] = count + 1;
                    }
                    else
                    {
                        result[word] = 1;
                    }
                }
            }

            return(result.OrderBy(x => x.Value).Take(n).ToDictionary(x => x.Key, x => x.Value));
        }
コード例 #2
0
        /// <summary>
        /// Converts the first character of each word to Uppercase. Example: "the lazy dog" returns "The Lazy Dog"ç
        /// </summary>
        /// <param name="words">The words.</param>
        /// <param name="culture">The culture.</param>
        /// <returns>title cased string.</returns>
        /// <exception cref="System.ArgumentNullException">words</exception>
        public static string ToTitleCase(string[] words, CultureInfo culture = null)
        {
            if (words == null)
            {
                throw new ArgumentNullException("words");
            }

            culture = CultureUtils.GetCurrentCultureIfNull(culture);

            string[] titleCasedWords = new string[words.Length];
            for (int i = 0; i < words.Length; i++)
            {
                string word = words[i];
                if (string.IsNullOrEmpty(word))
                {
                    titleCasedWords[i] = word;
                }
                else
                {
                    titleCasedWords[i] = char.ToUpper(word[0], culture) + word.Substring(1);
                }
            }

            return(string.Join(SPACE_STRING, titleCasedWords));
        }
コード例 #3
0
        public static DataTable ToDataTable <TItem>(IList <TItem> list, CultureInfo culture = null)
            where TItem : IDictionary
        {
            if (list == null)
            {
                throw new ArgumentNullException("list");
            }

            culture = CultureUtils.GetCurrentCultureIfNull(culture);

            DataTable result   = null;
            bool      succeded = false;

            try
            {
                result = new DataTable
                {
                    Locale = culture
                };

                if (list.Count == 0)
                {
                    succeded = true;
                    return(result);
                }

                // Select column names
                HashSet <string> columns = new HashSet <string>();
                for (int i = 0; i < list.Count; i++)
                {
                    TItem item = list[i];
                    foreach (DictionaryEntry dictionaryEntry in item)
                    {
                        columns.Add(ConvertUtils.ChangeType <string>(dictionaryEntry.Key, culture));
                    }
                }

                result.Columns.AddRange(columns.Select(c => new DataColumn(c, typeof(object))).ToArray());

                for (int i = 0; i < list.Count; i++)
                {
                    TItem item = list[i];
                    AddRow(result, item, culture);
                }

                succeded = true;
            }
            finally
            {
                if (!succeded && result != null)
                {
                    result.Dispose();
                    result = null;
                }
            }

            return(result);
        }
コード例 #4
0
        /// <summary>
        /// Gets the node attribute value.
        /// </summary>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <param name="node">The node.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="culture">The culture.</param>
        /// <returns>The node attribute value.</returns>
        public static TValue GetNodeAttributeValue <TValue>(XmlNode node, string propertyName, CultureInfo culture = null)
        {
            if (node == null)
            {
                throw new XmlUtilsException(Strings.XmlUtils_GetNodeProperty_Error);
            }

            culture = CultureUtils.GetCurrentCultureIfNull(culture);

            return(ConvertUtils.ChangeType <TValue>(GetNodeAttributeValue(node, propertyName, null), culture));
        }
コード例 #5
0
        /// <summary>
        /// Adds the row.
        /// </summary>
        /// <param name="table">The table.</param>
        /// <param name="item">The item.</param>
        /// <param name="culture">The culture.</param>
        private static void AddRow(DataTable table, IDictionary item, CultureInfo culture = null)
        {
            DataRow row = table.NewRow();

            foreach (var key in item.Keys)
            {
                string columnName = ConvertUtils.ChangeType <string>(key, CultureUtils.GetCurrentCultureIfNull(culture));
                row[columnName] = item[key];
            }

            table.Rows.Add(row);
        }
コード例 #6
0
        /// <summary>
        /// Gets the month names of the specified culture.
        /// </summary>
        /// <param name="culture">The culture.</param>
        /// <returns>Month names list.</returns>
        public static IList <MonthName> GetMonthNames(CultureInfo culture = null)
        {
            culture = CultureUtils.GetCurrentCultureIfNull(culture);

            return(culture.DateTimeFormat.MonthNames
                   .TakeWhile(m => !m.IsNullOrEmpty())
                   .Select((m, i) => new MonthName
            {
                Number = i + 1,
                Name = m
            }).ToList());
        }
コード例 #7
0
        /// <summary>
        /// Gets the day names.
        /// </summary>
        /// <param name="culture">The culture.</param>
        /// <returns>Day names list.</returns>
        public static IList <DayName> GetDayNames(CultureInfo culture = null)
        {
            culture = CultureUtils.GetCurrentCultureIfNull(culture);

            return(culture.DateTimeFormat.DayNames
                   .TakeWhile(m => !m.IsNullOrEmpty())
                   .Select((m, i) => new DayName
            {
                Number = ((i + (7 - (int)culture.DateTimeFormat.FirstDayOfWeek)) % 7) + 1,
                Name = m
            }).ToList());
        }
コード例 #8
0
        /// <summary>
        /// Gets the node attribute value.
        /// </summary>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <param name="node">The node.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="defaultValue">The default value.</param>
        /// <param name="culture">The culture.</param>
        /// <returns>The node attribute value.</returns>
        public static TValue GetNodeAttributeValue <TValue>(XmlNode node, string propertyName, TValue defaultValue, CultureInfo culture = null)
        {
            culture = CultureUtils.GetCurrentCultureIfNull(culture);

            string value;

            if (TryGetNodeAttributeValue(node, propertyName, out value))
            {
                return(ConvertUtils.ChangeType <TValue>(value, culture));
            }

            return(defaultValue);
        }
コード例 #9
0
        /// <summary>
        /// Capitalizes the specified string.
        /// </summary>
        /// <param name="string">The string.</param>
        /// <param name="culture">The culture.</param>
        /// <returns>capitalized string.</returns>
        public static string Capitalize(string @string, CultureInfo culture = null)
        {
            if (string.IsNullOrEmpty(@string))
            {
                return(string.Empty);
            }

            culture = CultureUtils.GetCurrentCultureIfNull(culture);

            if (@string.Length == 1)
            {
                return(@string.ToUpper(culture));
            }

            return(@string.Substring(0, 1).ToUpper(culture) + @string.Substring(1));
        }
コード例 #10
0
        public static DataTable ToDataTable(ICollection <DynamicDictionary> list, CultureInfo culture = null)
        {
            if (list == null)
            {
                throw new ArgumentNullException("list");
            }

            culture = CultureUtils.GetCurrentCultureIfNull(culture);

            DataTable result   = null;
            bool      succeded = false;

            try
            {
                result = new DataTable {
                    Locale = culture
                };

                if (list.Count == 0)
                {
                    succeded = true;
                    return(result);
                }

                IEnumerable <string> columnNames = list.SelectMany(dict => dict.Keys).Distinct();
                result.Columns.AddRange(columnNames.Select(c => new DataColumn(c, typeof(object))).ToArray());

                foreach (DynamicDictionary item in list)
                {
                    AddRow(result, item, culture);
                }

                succeded = true;
            }
            finally
            {
                if (!succeded && result != null)
                {
                    result.Dispose();
                    result = null;
                }
            }

            return(result);
        }
コード例 #11
0
        /// <summary>
        /// To the name value collection.
        /// </summary>
        /// <param name="dictionary">The dictionary.</param>
        /// <param name="culture">The culture for string conversion.</param>
        /// <returns>NameValueCollection.</returns>
        /// <exception cref="System.ArgumentNullException">dictionary</exception>
        public static NameValueCollection ToNameValueCollection(IDictionary dictionary, CultureInfo culture = null)
        {
            if (dictionary == null)
            {
                throw new ArgumentNullException("dictionary");
            }

            culture = CultureUtils.GetCurrentCultureIfNull(culture);

            NameValueCollection result = new NameValueCollection(dictionary.Count);

            foreach (DictionaryEntry dictionaryEntry in dictionary)
            {
                result.Add(ConvertUtils.ChangeType <string>(dictionaryEntry.Key, culture), ConvertUtils.ChangeType <string>(dictionaryEntry.Value, culture));
            }

            return(result);
        }
コード例 #12
0
        public static IDictionary <string, int> TopNWords(string document, int n, bool stopwordEnabled = false, CultureInfo culture = null)
        {
            if (document == null)
            {
                throw new ArgumentNullException("document");
            }

            culture = CultureUtils.GetCurrentCultureIfNull(culture);

            HashSet <string> stopwords = null;

            if (stopwordEnabled)
            {
                s_StopWords.TryGetValue(culture, out stopwords);
            }

            return(TopNWords(document, n, stopwords, culture));
        }
コード例 #13
0
        /// <summary>
        /// Method to convert a custom Object to XML string
        /// </summary>
        /// <param name="value">Object that is to be serialized to XML</param>
        /// <param name="culture">Culture.</param>
        /// <exception cref="ArgumentNullException">value</exception>
        /// <returns>XML string</returns>
        public static string XmlSerializeObject(object value, CultureInfo culture = null)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            culture = CultureUtils.GetCurrentCultureIfNull(culture);

            Type          objectType    = value.GetType();
            XmlSerializer xmlSerializer = new XmlSerializer(objectType);

            using (StringWriter stringWriter = new StringWriter(culture))
            {
                XmlWriter xmlWriter = new XmlTextWriter(stringWriter);
                xmlSerializer.Serialize(xmlWriter, value);
                return(stringWriter.ToString());
            }
        }
コード例 #14
0
        /// <summary>
        /// Determines whether [contains] [the specified target].
        /// </summary>
        /// <param name="target">The target.</param>
        /// <param name="value">The value.</param>
        /// <param name="startIndex">The start index.</param>
        /// <param name="culture">The culture.</param>
        /// <returns>
        ///   <c>true</c> if [contains] [the specified target]; otherwise, <c>false</c>.
        /// </returns>
        public static bool Contains(string target, string value, int startIndex, CultureInfo culture = null)
        {
            culture = CultureUtils.GetCurrentCultureIfNull(culture);

            return(target != null && value != null && culture.CompareInfo.IndexOf(target, value, startIndex) >= 0);
        }