示例#1
0
        private static int GetMiddleFroQuickSort(WORDSFRE[] array, int left, int right)
        {
            try
            {
                WORDSFRE key = array[left];
                while (left < right)
                {
                    while (left < right && key.ED.CompareTo(array[right].ED) > 0)
                    {
                        right--;
                    }
                    if (left < right)
                    {
                        WORDSFRE temp = array[left];
                        array[left] = array[right];
                        left++;
                    }

                    while (left < right && key.ED.CompareTo(array[left].ED) < 0)
                    {
                        left++;
                    }
                    if (left < right)
                    {
                        WORDSFRE temp = array[right];
                        array[right] = array[left];
                        right--;
                    }
                    array[left] = key;
                }
                return(left);
            }
            catch (System.Exception ex)
            {
                return(-1);
            }
        }
示例#2
0
        public static WORDSFRE[] StatisticsWords(string TheDoc)
        {
            try
            {
                WORDSFRE[] WordsFre;
                string[]   words;
                Hashtable  ha = new Hashtable();

                //获取全部词
                words = TheDoc.Split(' ');

                //统计词频
                foreach (string wd in words)
                {
                    if (ha.Contains(wd))
                    {
                        ha[wd] = (int)ha[wd] + 1;
                    }
                    else
                    {
                        ha.Add(wd, 1);
                    }
                }

                //统计位置信息
                WordsFre = new WORDSFRE[ha.Count];
                int  WordsFreID = 0;
                int  ExistID    = 0;
                bool isExist    = false;
                for (int i = 0; i < words.Length; i++)
                {
                    for (int j = 0; j < WordsFreID; j++)
                    {
                        if (words[i] == WordsFre[j].Word)
                        {
                            isExist = true;
                            ExistID = j;
                            break;
                        }
                    }
                    if (!isExist)
                    {
                        WordsFre[WordsFreID]             = new WORDSFRE();
                        WordsFre[WordsFreID].Position    = new int[(int)ha[words[i]]];
                        WordsFre[WordsFreID].Distance    = new int[(int)ha[words[i]]];
                        WordsFre[WordsFreID].Word        = words[i];
                        WordsFre[WordsFreID].Frequency   = 1;
                        WordsFre[WordsFreID].Position[0] = i;
                        WordsFreID++;
                    }
                    else
                    {
                        WordsFre[ExistID].Position[WordsFre[ExistID].Frequency] = i;
                        WordsFre[ExistID].Frequency++;
                        isExist = false;
                    }
                }

                for (int i = 0; i < WordsFre.Length; i++)
                {
                    for (int j = 0; j < WordsFre[i].Position.Length; j++)
                    {
                        if (j == 0)
                        {
                            WordsFre[i].Distance[j] = WordsFre[i].Position[j] + words.Length - WordsFre[i].Position[WordsFre[i].Position.Length - 1];
                        }
                        else
                        {
                            WordsFre[i].Distance[j] = WordsFre[i].Position[j] - WordsFre[i].Position[j - 1];
                        }
                    }
                }

                return(WordsFre);
            }
            catch (System.Exception ex)
            {
                return(null);
            }
        }