Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用idf和count找到保存一些单词的最佳方法_C#_.net - Fatal编程技术网

C# 用idf和count找到保存一些单词的最佳方法

C# 用idf和count找到保存一些单词的最佳方法,c#,.net,C#,.net,我有一个文本和很多句子。所有的句子都用一个特定的id存储在数据库中。 我想读这些句子,并保存句子中的每个单词,这样我最终可以找到整个文本中单词的重复次数 例如: 文本=克尔多尔姆修道院选举争端于1308年发生在英国约克郡。约克大主教威廉·格林菲尔德(William Greenfield)在院长一系列辞职后,任命了一名修女领导这所房子。他的候选人艾玛·德埃博(Emma de Ebor)被许多修女认为是不可接受的,三个月后她辞职了。大主教随后任命了附近罗斯代尔修道院的琼·德·皮克林,但修女们也拒绝了

我有一个文本和很多句子。所有的句子都用一个特定的id存储在数据库中。 我想读这些句子,并保存句子中的每个单词,这样我最终可以找到整个文本中单词的重复次数

例如:

文本=克尔多尔姆修道院选举争端于1308年发生在英国约克郡。约克大主教威廉·格林菲尔德(William Greenfield)在院长一系列辞职后,任命了一名修女领导这所房子。他的候选人艾玛·德埃博(Emma de Ebor)被许多修女认为是不可接受的,三个月后她辞职了。大主教随后任命了附近罗斯代尔修道院的琼·德·皮克林,但修女们也拒绝了她。大主教试图平息修女们的反叛情绪,将一些修女驱逐到周围的修道院,并威胁将其他修女逐出教会。修道院并没有被阻止,最终格林菲尔德允许修女们再次选出一名修女。他们首先再次选举了艾玛·德·斯泰普顿,她曾于1301年担任院长,但她也变得不受欢迎,并辞职了。他们最终再次选举艾玛·德埃博。选举争端烟消云散,隐修会在1536年解散之前很少有人听说过它”

句子={“1308年,英格兰约克郡发生了凯尔德霍尔姆修道院选举争端。约克大主教威廉·格林菲尔德在院长一系列辞职后任命了一名修女领导众议院。”,“他的候选人艾玛·德埃博被许多修女认为是不可接受的,她在三个月后辞职。”,…}

{单词,计数,idf}={“The”,101000},{“Keldholme”,19000}

单词=所有句子中的所有单词

计数=所有句子中重复的单词数

idf=log(计数所有单词/计数此单词)

我估计我们有大约14000个非重复单词

我的测试解决方案:

    public class TermModel
{
    public int id { get; set; }
    public string text { get; set; }
    public double idf { get; set; }
    public int count { get; set; }

}

    List<TermModel> termlist = new List<TermModel>();
    foreach (var item in sentences)
    {
      var cleanitem = cleanSen(item);//Output lists all sentence words
       foreach (var items in cleanitem)
       {
          var term = termlist.FirstOrDefault(p => p.text == items);
             if (term != null)
             {
                term.count++;
             }
             else
             {
              TermModel termModel = new TermModel();
              termModel.idf = 0;
              termModel.text = items;
              termModel.count = 1;
              termlist.Add(termModel);
              }
            }
       }

       int countALL = termlist.Count();
       foreach (var item in termlist)
       {
         item.idf = Math.Log(countALL / item.count);
       }
       foreach(var item in termlist)
       {
          _db.termModels.Add(item);
       }
          _db.SaveChanges();
公共类术语模型
{
公共int id{get;set;}
公共字符串文本{get;set;}
公共双idf{get;set;}
公共整数计数{get;set;}
}
List termlist=新列表();
foreach(句子中的变量项)
{
var cleanitem=cleanSen(item);//输出列出所有句子单词
foreach(cleanitem中的var项)
{
var term=termlist.FirstOrDefault(p=>p.text==items);
如果(术语!=null)
{
term.count++;
}
其他的
{
TermModel TermModel=新的TermModel();
termModel.idf=0;
termModel.text=项目;
termModel.count=1;
termlist.Add(术语模型);
}
}
}
int countALL=termlist.Count();
foreach(术语表中的变量项)
{
item.idf=Math.Log(countALL/item.count);
}
foreach(术语表中的变量项)
{
_db.termModels.Add(项目);
}
_db.SaveChanges();
此方法需要30分钟以上才能完成

我需要一个更好的方法

  • 不需要在数据库中保存
  • 我使用sqlite
  • 保存是必需的

我想我一直遵循你想要的,直到日志业务。字典非常快,可以修改为容纳20亿项。我想它们下面使用哈希表。因此这可能会加快计数速度

    private void CountWords()
    {
        string text = "The Keldholme Priory election dispute occurred in Yorkshire, England, in 1308. The Archbishop of York, William Greenfield, appointed one of the nuns to lead the house after a series of resignations by its prioresses. His candidate, Emma de Ebor', was deemed unacceptable by many nuns, and she resigned three months later. The Archbishop next appointed Joan de Pykering from nearby Rosedale Priory, but the nuns resisted her as well. The Archbishop attempted to quash the nuns' rebelliousness, exiling some to surrounding priories and threatening others with excommunication. The convent was not deterred, and eventually Greenfield allowed the nuns to elect one of their number again. They first re-elected Emma de Stapleton, who had been prioress in 1301, but she also became unpopular, and resigned. They eventually re-elected Emma de Ebor'. The election dispute evaporated, and little more was heard of the priory until its dissolution in 1536";
        char[] sp = new[] { ' ' };
        string[] words = text.Split(sp);
        Dictionary<string, int> WordCount = new Dictionary<string, int>();
        char[] punctuation = new[] { ',', '.' }; //probably need to more punctuation characters
        foreach (string word in words)
        {
            string CleanWord =word.Trim(punctuation);
            CleanWord = CleanWord.ToLower();
            if (WordCount.ContainsKey(CleanWord))
            {
                WordCount[CleanWord]++;
            }
            else
            {
                WordCount.Add(CleanWord, 1);
            }
        }
        foreach (KeyValuePair<string, int> kv in WordCount)
        {
            Debug.Print($"{kv.Key} {kv.Value}");
        }
    }
private void CountWords()
{
字符串文本=”1308年,英格兰约克郡发生了凯尔多尔姆修道院选举纠纷。约克大主教威廉·格林菲尔德(William Greenfield)在一系列院长辞职后任命了一名修女领导该院。他的候选人艾玛·德埃博(Emma de Ebor)被许多修女认为是不可接受的,她在三个月后辞职。大主教随后任命了他大主教试图平息修女们的叛逆情绪,将一些修女驱逐到周围的修道院,并威胁将其他修女逐出教会。修道院并没有受到阻止,最终格林菲尔德允许修女们再次选出其中一名修女。他们首先-选举艾玛·德·斯泰普顿(Emma de Stapleton),她曾于1301年担任院长,但她也变得不受欢迎,并辞职。他们最终再次选举艾玛·德·埃博。选举争议消失了,在1536年解散之前,几乎没有听到更多关于修道院的消息”;
char[]sp=new[]{''};
string[]words=text.Split(sp);
字典字数=新字典();
char[]标点符号=新的[]{',','.'.};//可能需要更多标点符号
foreach(单词中的字符串)
{
string CleanWord=word.Trim(标点符号);
CleanWord=CleanWord.ToLower();
if(WordCount.ContainsKey(CleanWord))
{
字数[CleanWord]++;
}
其他的
{
WordCount.Add(CleanWord,1);
}
}
foreach(字计数中的KeyValuePair kv)
{
打印($“{kv.Key}{kv.Value}”);
}
}

如何保存字典以便下次使用