for (int i = 0; i < sentenceN; i++)
{
for (int j = 0; j < array[i].Length; j++)
{
for (int x = 0; x < sentenceN; x++)
{
for (int y = 0; y < array.Length[x]; y++)
{
if (array[i][j] == array[x][y])
array[x][y] = containN++; //这里要如何存储最后的值,如果放进二维数组的话,就是覆盖,但后面遍历时还要用到。???
break;
}
}
}
}
array是个动态二维数组,里面放的是一个个的词语。这个二维数组的第一维是句子。
这段程序的目的是便利每一个句子,找到一个词在多少个句子中出现。
最后统计包含这个特定词语句子的个数。
===================================================================================
本来我想把统计后的数子放进原数组,但这样的话,下一次遍历的时候原先的数组就被覆盖了。
所以我想开辟一个新的动态数组,其大小跟array一样,专门用来存放containN++的值,也就是包含词语句子的个数。
请问我应该怎么做?如何定义这个数组
谢谢!
并输出出现频率为75%的单词
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Text.RegularExpressions;
using System.IO;
using System.Collections;namespace ImportExportTest
{
public class WordCounter : IComparer<int>
{
static Dictionary<String, String> badWordDict = new Dictionary<string, string>(); static WordCounter()
{
badWordDict = new Dictionary<string, string>();
#region WordFix
String dictStr = @"all aii
well weii
really reaiiy
i'll i'ii
love iove
would wouid
could couid
look iook
littie iittie
tell teii
should shouid
chandler chandier
will wiii
please piease
believe beiieve
rachel rachei
still stiii
people peopie
only oniy
we'll we'ii
call caii
help heip
wouldn't wouidn't
feel feei
left ieft
talking taiking
damnlt damnit
looking iooking
actually actuaiiy
place piace
you'll you'ii
life iife
while whiie
play piay
called caiied
always aiways
hello heiio
later iater
deal deai
whole whoie
leave ieave
also aiso
else eise
english engiish
couldn't couidn't
live iive
subtitles subtities
cooler cooi
until untii
plans pians
beautiful beautifui
problem probiem
already aiready
learn iearn
luck iuck
telling teiiing
living iiving
shouldn't shouidn't
lady iady
couple coupie
totally totaiiy
close ciose
myself myseif
hell heii
speclal speciai
real reai
girls giris
pull puii
looks iooks
lost iost
it'll it'ii
hold hoid
half haif
yelling yeiiing
kill kiii
exactly exactiy
little iittle
yourself yourseif
college coiiege
melissa meiissa
girlfriend girifriend
doodle doodie
flowers fiowers";
#endregion StringReader sr = new StringReader(dictStr);
while (true)
{
String s = sr.ReadLine();
if (s == null)
break;
String[] sArr = s.Split('\t');
if (sArr.Length == 2)
{
badWordDict.Add(sArr[1].Trim(), sArr[0].Trim());
}
} } double totalCount = 0;
public Dictionary<String, int> WordCount(String str)
{
Dictionary<String, int> wordCountDict = new Dictionary<string, int>(); if (String.IsNullOrEmpty(str))
return wordCountDict;
Regex re = new Regex(@"(?<word>[a-zA-Z\']*)");
foreach (Match m in re.Matches(str))
{
String word = m.Result("${word}");
word = word.Trim().ToLower();
if (word.Length <= 2)
continue;
if (badWordDict.ContainsKey(word))
{
word = badWordDict[word];
}
if (wordCountDict.ContainsKey(word))
{
wordCountDict[word] = wordCountDict[word] + 1;
}
else
{
wordCountDict[word] = 1;
}
totalCount = totalCount + 1;
}
return wordCountDict;
} public void Test()
{
totalCount = 0;
DirectoryInfo dir = new DirectoryInfo("F:\\Lan");
StringBuilder fileStr = new StringBuilder();
StreamWriter sw = new StreamWriter("F:\\Out.Txt");
foreach (FileInfo f in dir.GetFiles())
{
if (!f.Name.EndsWith(".lrc") || f.Name.EndsWith("_cn.lrc"))
{
continue;
}
StreamReader sr = f.OpenText();
String str = sr.ReadToEnd();
sr.Close();
fileStr.Append("\r\n");
fileStr.Append(str);
sw.WriteLine();
sw.Write(str);
sw.Flush();
}
sw.Close(); Dictionary<String, int> dict = WordCount(fileStr.ToString()); SortedList<int, List<string>> countsList = new SortedList<int, List<string>>(this);
foreach (String s in dict.Keys)
{
//Console.WriteLine(s + "\t" + dict[s]);
int n = dict[s];
if (countsList.ContainsKey(n))
{
countsList[n].Add(s);
}
else
{
countsList[n] = new List<string>();
countsList[n].Add(s);
}
} double currentTotal = 0;
//超过百分之多少才输出
double outPercentage = 75; foreach (int key in countsList.Keys)
{
foreach (String s in countsList[key])
{
Console.WriteLine(s + "\t" + key);
}
currentTotal += key;
double p = (currentTotal / totalCount) * 100;
if (p >= outPercentage) //超过百分比
{
Console.WriteLine(p);
break;
}
}
} #region IComparer<int> Members public int Compare(int x, int y)
{
return y.CompareTo(x);
} #endregion
}
}