-4

上記のコードを機能させることができましたが、次のエラーが発生します。グーグルを試してみたところ、データ型の問題であることが少しわかりました。しかし、上記の 2 つの関数のデータ型を変更すると、同じエラーが発生します。私は何をすべきか?

*この場合、字句密度指数を計算しようとしています。

//For counting unique words
 private void UniqueWordCount(string fbStatus)
        {
            int count = 0;
            var countedWordList = new List<string>(100);
            var reg = new Regex(@"\w+");
            foreach (Match match in reg.Matches(fbStatus))
            {
                string word = match.Value.ToLower();
                if (!countedWordList.Contains(word))
                {
                    ++count;
                    countedWordList.Add(word);
                }
            }
            label_totaluniquewords.Text = count.ToString();
        }

//For counting total words

  private void SplitWords(string fbStatus)
        {
            int splitWords = fbStatus.Split(new char[] { ' ' },StringSplitOptions.RemoveEmptyEntries).Count();
            label_totalwordcount.Text = splitWords.ToString();
        }

//For counting lexical density (trying to make this work...)
   private void CalculateLexicalDensity(string fbStatus)
        {
            int ld = 0;
            ld = (UniqueWordCount(fbStatus) / SplitWords(fbStatus)) * 100;
            label_lexicaldensity.Text = ld.ToString();
        }
4

3 に答える 3

4

SplitWords計算した値を返しません。カウントを返す予定がある場合は、追加します

return splitWords;

関数の最後で、次のように宣言しますint

private int SplitWords(string fbStatus)
    {
        int splitWords = fbStatus.Split(new char[] { ' ' },StringSplitOptions.RemoveEmptyEntries).Count();
        label_totalwordcount.Text = splitWords.ToString();
        return splitWords;
    }

ただし、パーセンテージの計算は、整数除算のためにオフになる可能性があることに注意してください。除算を適用する前に、を返すか、decimalにキャストする必要があります。decimal

操作の順序を変更することもできます

ld = 100 * UniqueWordCount(fbStatus) / SplitWords(fbStatus);

最高のパーセンテージに切り捨てられた整数の結果を取得します。

于 2013-07-23T03:27:48.653 に答える
2

コードを次のように変更します。

//For counting unique words
 private int UniqueWordCount(string fbStatus)
        {
            int count = 0;
            var countedWordList = new List<string>(100);
            var reg = new Regex(@"\w+");
            foreach (Match match in reg.Matches(fbStatus))
            {
                string word = match.Value.ToLower();
                if (!countedWordList.Contains(word))
                {
                    ++count;
                    countedWordList.Add(word);
                }
            }
            label_totaluniquewords.Text = count.ToString();
            return count;
        }



private int SplitWords(string fbStatus)
        {
            int splitWords = fbStatus.Split(new char[] { ' ' },StringSplitOptions.RemoveEmptyEntries).Count();
            label_totalwordcount.Text = splitWords.ToString();
            return splitWords;
        }

//For counting lexical density (trying to make this work...)
   private void CalculateLexicalDensity(string fbStatus)
        {
            decimal ld = 0;
            ld = ((decimal)UniqueWordCount(fbStatus) / (decimal)SplitWords(fbStatus)) * 100;
            label_lexicaldensity.Text = ld.ToString();
        }
于 2013-07-23T03:27:36.013 に答える