簡體   English   中英

C#中的字數統計算法

[英]Word Count Algorithm in C#

我正在尋找一個好的單詞計數類或功能。 當我從互聯網上復制並粘貼一些內容並將其與我的自定義字數統計算法和MS Word進行比較時,它總是偏離10%多一點。 我覺得這太過分了。 那么你們在c#中知道一個准確的字數統計算法嗎?

正如@astander建議的那樣,你可以按如下方式執行String.Split:

string[] a = s.Split(
    new char[] { ' ', ',', ';', '.', '!', '"', '(', ')', '?' },
    StringSplitOptions.RemoveEmptyEntries);

通過傳入一個字符數組,您可以分割多個單詞分隔符。 刪除空條目將使您無法計算非單詞。

String.Split由預定義的字符組成。 使用標點符號,空格(刪除多個空格)以及您確定為“單詞拆分”的任何其他字符

你有什么嘗試?

我確實看到前一個用戶被釘上了鏈接,但這里有一些使用正則表達式或字符匹配的例子。 希望它有所幫助,沒有人受傷X-)

String.Split方法(Char [])

C#中的字計數器

C#字數

使用正則表達式查找單詞(例如[\\ w] +)並計算匹配項

public static Regex regex = new Regex(
  "[\\w]+",
RegexOptions.Multiline
| RegexOptions.CultureInvariant
| RegexOptions.Compiled
);

regex.Match(_someString).Count之間

我在ClipFlair中遇到了同樣的問題,我需要為電影字幕計算WPM(每分鍾字數),所以我想出了以下一個:

您可以在靜態類中定義此靜態擴展方法,然后在需要使用此擴展方法的任何類中將using子句添加到該靜態類的名稱空間。 使用s.WordCount()調用擴展方法,其中s是一個字符串(標識符[variable / constant]或literal)

public static int WordCount(this string s)
{
  int last = s.Length-1;

  int count = 0;
  for (int i = 0; i <= last; i++)
  {
    if ( char.IsLetterOrDigit(s[i]) &&
         ((i==last) || char.IsWhiteSpace(s[i+1]) || char.IsPunctuation(s[i+1])) )
      count++;
  }
  return count;
}

這是我用於計算單詞,亞洲單詞,字符等的c#代碼類的精簡版本。這與Microsoft Word幾乎相同。 我開發了用於計算Microsoft Word文檔單詞的原始代碼。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    namespace BL {
    public class WordCount 
    {

    public int NonAsianWordCount { get; set; }
    public int AsianWordCount { get; set; }
    public int TextLineCount { get; set; }
    public int TotalWordCount { get; set; }
    public int CharacterCount { get; set; }
    public int CharacterCountWithSpaces { get; set; }


    //public string Text { get; set; }

    public WordCount(){}

    ~WordCount() {}


    public void GetCountWords(string s)
    {
        #region Regular Expression Collection
        string asianExpression = @"[\u3001-\uFFFF]";
        string englishExpression = @"[\S]+";
        string LineCountExpression = @"[\r]+";
        #endregion


        #region Asian Character
        MatchCollection asiancollection = Regex.Matches(s, asianExpression);

        AsianWordCount = asiancollection.Count; //Asian Character Count

        s = Regex.Replace(s, asianExpression, " ");

        #endregion 


        #region English Characters Count
        MatchCollection collection = Regex.Matches(s, englishExpression);
        NonAsianWordCount = collection.Count;
        #endregion

        #region Text Lines Count
        MatchCollection Lines = Regex.Matches(s, LineCountExpression);
        TextLineCount = Lines.Count;
        #endregion

        #region Total Character Count

        CharacterCount = AsianWordCount;
        CharacterCountWithSpaces = CharacterCount;

        foreach (Match word in collection)
        {
            CharacterCount += word.Value.Length ;
            CharacterCountWithSpaces += word.Value.Length + 1;
        }

        #endregion

        #region Total Character Count
        TotalWordCount = AsianWordCount + NonAsianWordCount;
        #endregion
    }
}
}

您還需要檢查newlinestabsnon-breaking spaces 我發現最好將源文本復制到StringBuilder ,並用空格替換所有換行符,制表符和句子結​​束符。 然后根據空格拆分字符串。

public static class WordCount
{
    public static int Count(string text)
    {
        int wordCount = 0;
        text = text.Trim();// trim white spaces

        if (text == ""){return 0;} // end if empty text

        foreach (string word in text.Split(' ')) // or use any other char(instead of empty space ' ') that you consider a word splitter 
        wordCount++;
        return wordCount;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM