void runDisplaySetKind() { //读取设备词典 if (WordSetKind.Words.Count == 0) { using (FileStream fs = new FileStream("datas/WordsSetKind.dat", FileMode.Open)) { BinaryFormatter binFormatter = new BinaryFormatter(); WordSetKind.Words = (Dictionary <string, SetAttribute>)binFormatter.Deserialize(fs); fs.Close(); } } BlogReader breader; //if (Datas.sinaJSONList.Count > 0) // Datas.sinaJSONList.Clear(); if (Datas.sinaJSONList.Count == 0) { breader = new BlogReader(@"temp5.dat");//G:\11\SerializedData\temp3.dat");//("temp5.dat");// Datas.sinaJSONList = breader.ReadFromFile(); } Datas.ProgressBarState = "读取完成,正在分析..."; Datas.ProgresBarStyle = System.Windows.Forms.ProgressBarStyle.Marquee; List <string> key = new List <string>(); List <int> value = new List <int>(); List <SetKind> setKind = new List <SetKind>(); foreach (SinaJSON item in Datas.sinaJSONList) { string set = Regex.Match(item.Source, @">[^<]+").Value; set = set.Substring(1, set.Length - 1); int index = key.IndexOf(set); //存在该条目 if (index != -1) { value[index] = value[index] + 1; } else { if (WordSetKind.Words.ContainsKey(set)) { setKind.Add(WordSetKind.Words[set].setKind); } else { setKind.Add(SetKind.未知); } key.Add(set); value.Add(1); } } this.Invoke(new updateDataGridViewSetKind(doUpdateDataGridViewSetKind), new object[] { key, value, setKind }); }
void runDisplaySetKind() { //读取设备词典 if (WordSetKind.Words.Count == 0) { using (FileStream fs = new FileStream("datas/WordsSetKind.dat", FileMode.Open)) { BinaryFormatter binFormatter = new BinaryFormatter(); WordSetKind.Words = (Dictionary<string, SetAttribute>)binFormatter.Deserialize(fs); fs.Close(); } } BlogReader breader; //if (Datas.sinaJSONList.Count > 0) // Datas.sinaJSONList.Clear(); if (Datas.sinaJSONList.Count == 0) { breader = new BlogReader(@"temp5.dat");//G:\11\SerializedData\temp3.dat");//("temp5.dat");// Datas.sinaJSONList = breader.ReadFromFile(); } Datas.ProgressBarState = "读取完成,正在分析..."; Datas.ProgresBarStyle = System.Windows.Forms.ProgressBarStyle.Marquee; List<string> key = new List<string>(); List<int> value = new List<int>(); List<SetKind> setKind = new List<SetKind>(); foreach (SinaJSON item in Datas.sinaJSONList) { string set = Regex.Match(item.Source, @">[^<]+").Value; set = set.Substring(1, set.Length - 1); int index = key.IndexOf(set); //存在该条目 if (index != -1) { value[index] = value[index] + 1; } else { if (WordSetKind.Words.ContainsKey(set)) { setKind.Add(WordSetKind.Words[set].setKind); } else { setKind.Add(SetKind.未知); } key.Add(set); value.Add(1); } } this.Invoke(new updateDataGridViewSetKind(doUpdateDataGridViewSetKind), new object[] { key, value, setKind}); }
//读取数据 显示到 datagridviewEmo void runDisplayEmotion() { BlogReader breader; //if (Datas.sinaJSONList.Count > 0) // Datas.sinaJSONList.Clear(); if (Datas.sinaJSONList.Count == 0) { breader = new BlogReader("temp5.dat"); Datas.sinaJSONList = breader.ReadFromFile(); } Datas.ProgressBarState = "读取完成,正在分析..."; Datas.ProgresBarStyle = System.Windows.Forms.ProgressBarStyle.Marquee; string emo = ""; //表情键 List<string> emoKey = new List<string>(); List<EmoValue> emoValue = new List<EmoValue>(); using (FileStream fs = new FileStream("datas/emo.txt", FileMode.Open)) { StreamReader sr = new StreamReader(fs); while (!sr.EndOfStream) { string line = sr.ReadLine(); if (line.Length > 0) { emoKey.Add(line); emoValue.Add(new EmoValue()); } } sr.Close(); fs.Close(); } MatchCollection mCollection; foreach (SinaJSON item in Datas.sinaJSONList) { mCollection = Regex.Matches(item.Text, @"\[\w+\]"); for (int i = 0; i < mCollection.Count; i++) { emo = mCollection[i].Value; //emo = emo.Substring(1, emo.Length - 2); int index = emoKey.IndexOf(emo); int male = 0, female = 0; if (item.SinaUser.Gender.Equals("m")) male++; else female++; if (index != -1) emoValue[index] = new EmoValue(emoValue[index].male + male, emoValue[index].female + female); } } app = new Excel.Application(); workbooks = app.Workbooks as Excel.Workbooks; workbook = workbooks.Add(Type.Missing); app.DisplayAlerts = false; //删除 多余Sheet foreach (Worksheet ws in workbook.Worksheets) if (ws != app.ActiveSheet) { ws.Delete(); } foreach (Chart cht in workbook.Charts) cht.Delete(); //创建一个Sheet,存数据 //worksheet = (Worksheet)workbook. // Worksheets.Add(Type.Missing, workbook.ActiveSheet, // Type.Missing, Type.Missing); worksheet = workbook.Worksheets[1]; worksheet.Name = "数据"; int worksheetIndex = 0; for (int i = 0; i < emoKey.Count; i++) { if (emoValue[i].male > 0 || emoValue[i].female > 0) { worksheet.Cells[i + 1, 1] = emoKey[i]; worksheet.Cells[i + 1, 2] = emoValue[i].male; worksheet.Cells[i + 1, 3] = emoValue[i].female; worksheetIndex++; } } // TODO: 生成一个统计图对象: Chart xlChart = (Chart)workbook.Charts. Add(Type.Missing, worksheet, Type.Missing, Type.Missing); // TODO: 设定数据来源 Range cellRange = (Range)worksheet.Cells[1, 1]; // TODO: 通过向导生成Chart xlChart.ChartWizard(cellRange.CurrentRegion, XlChartType.xl3DColumn, Type.Missing, XlRowCol.xlColumns, 1, 0, true, "表情比较", "表情", "数量"); // TODO: 设置统计图Sheet的名称 xlChart.Name = "统计"; // TODO: 让12个Bar都显示不同的颜色 ChartGroup grp = (ChartGroup)xlChart.ChartGroups(1); grp.GapWidth = 20; grp.VaryByCategories = true; // TODO: 让Chart的条目的显示形状变成圆柱形,并给它们显示加上数据标签 Series s1 = (Series)grp.SeriesCollection(1); s1.Name = "男"; s1.BarShape = XlBarShape.xlCylinder; s1.HasDataLabels = true; Series s = (Series)grp.SeriesCollection(2); s.BarShape = XlBarShape.xlCylinder; s.HasDataLabels = true; s.Name = "女"; // TODO: 设置统计图的标题和图例的显示 xlChart.Legend.Position = XlLegendPosition.xlLegendPositionTop; xlChart.ChartTitle.Font.Size = 24; xlChart.ChartTitle.Shadow = false; xlChart.ChartTitle.Border.LineStyle = XlLineStyle.xlContinuous; // TODO: 设置两个轴的属性,Excel.XlAxisType.xlValue对应的是Y轴,Excel.XlAxisType.xlCategory对应的是X轴 Axis valueAxis = (Axis)xlChart.Axes(XlAxisType.xlValue, XlAxisGroup.xlPrimary); valueAxis.AxisTitle.Orientation = -90; Axis categoryAxis = (Axis)xlChart.Axes(XlAxisType.xlCategory, XlAxisGroup.xlPrimary); categoryAxis.AxisTitle.Font.Name = "宋体"; //-------------------------------------------------- //workbook.SaveAs(sPath, Type.Missing, Type.Missing, // Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange, // Type.Missing, Type.Missing, Type.Missing, Type.Missing, // Type.Missing); xlChart.CopyPicture(XlPictureAppearance.xlScreen, XlCopyPictureFormat.xlPicture); IntPtr hwnd = (IntPtr)app.Hwnd; Bitmap b = null; try { if (OpenClipboard(hwnd)) { IntPtr data = GetClipboardData(14); // CF_ENHMETAFILE 14 if (data != IntPtr.Zero) { using (Metafile mf = new Metafile(data, true)) { b = new Bitmap(mf); } } } } finally { CloseClipboard(); //clear workbook.Close(Type.Missing, Type.Missing, Type.Missing); app.Workbooks.Close(); app.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook); System.Runtime.InteropServices.Marshal.ReleaseComObject(app); workbook = null; app = null; xlChart = null; GC.Collect(); } this.Invoke(new updateDataGridViewEmo(doUpdateDataGridViewEmo), new object[] { emoKey, emoValue, b }); }
void doAutoAna() { int anaCount = 0; if (WordSetKind.Words.Count != 0) { WordSetKind.Words.Clear(); } if (Datas.sinaJSONList.Count == 0) { BlogReader br = new BlogReader(@"temp5.dat"); Datas.sinaJSONList = br.ReadFromFile(); } if (listBrands == null) { listBrands = new List <SingleBrand>(); } if (listBrands.Count == 0) { using (FileStream fs = new FileStream("datas/mobileDatas.dat", FileMode.Open)) { BinaryFormatter bf = new BinaryFormatter(); listBrands = bf.Deserialize(fs) as List <SingleBrand>; fs.Close(); } } foreach (SinaJSON item in Datas.sinaJSONList) { anaCount++; Datas.ProgressBarState = "正在分析第" + anaCount + "个"; string source = Regex.Match(item.Source, @">[^<]+").Value; source = source.Substring(1, source.Length - 1); SetAttribute attr = new SetAttribute(); short position = 0; //位置变量 foreach (SingleBrand singleBrand in listBrands) { string[] name = singleBrand.BrandName.Replace(")", "").Split('('); if (name.Length == 2) { if (source.IndexOf(name[0]) != -1 || source.IndexOf(name[1]) != -1) { attr.BrandName = singleBrand.BrandName; break; } } else { if (source.IndexOf(name[0]) != -1) { attr.BrandName = singleBrand.BrandName; break; } } position++; } //品牌匹配成功 if (attr.BrandName != null) { foreach (Model model in listBrands[position].ListModels) { //存在型号 if (model.ModelName != null && model.ModelName.IndexOf(source) != -1) { if (model.ModelName.IndexOf("android", StringComparison.CurrentCultureIgnoreCase) != -1) { attr.OS = OperatingSystem.Android; } else if (model.ModelName.IndexOf("iOS", StringComparison.CurrentCultureIgnoreCase) != -1) { attr.OS = OperatingSystem.iOS; } if (attr.OS != OperatingSystem.未知) { attr.setKind = SetKind.手机; break; } } } if (attr.OS == OperatingSystem.未知) { attr.OS = OperatingSystem.Android; } if (attr.setKind == SetKind.未知) { attr.setKind = SetKind.手机; } } //品牌匹配失败 else { //大系统确定 if (source.IndexOf("android", StringComparison.CurrentCultureIgnoreCase) != -1) { attr.OS = OperatingSystem.Android; attr.setKind = SetKind.未知; } else if (source.IndexOf("ios", StringComparison.CurrentCultureIgnoreCase) != -1) { attr.OS = OperatingSystem.iOS; attr.setKind = SetKind.未知; } else if (source.IndexOf("win8", StringComparison.CurrentCultureIgnoreCase) != -1) { attr.OS = OperatingSystem.Windows; attr.setKind = SetKind.PC; } else { attr.OS = OperatingSystem.其它; attr.setKind = SetKind.未知; } attr.BrandName = "其它"; } if (source.IndexOf("window", StringComparison.CurrentCultureIgnoreCase) != -1) { attr.OS = OperatingSystem.WindowsPhone; } else if (source.IndexOf("s60", StringComparison.CurrentCultureIgnoreCase) != -1 || source.IndexOf("s40", StringComparison.CurrentCultureIgnoreCase) != -1 || source.IndexOf("s30", StringComparison.CurrentCultureIgnoreCase) != -1) { attr.OS = OperatingSystem.塞班; } if (source.IndexOf("phone", StringComparison.CurrentCultureIgnoreCase) != -1) { attr.setKind = SetKind.手机; } if (source.IndexOf("iphone", StringComparison.CurrentCultureIgnoreCase) != -1) { attr.OS = OperatingSystem.iOS; attr.setKind = SetKind.手机; } else if (source.IndexOf("ipad", StringComparison.CurrentCultureIgnoreCase) != -1) { attr.OS = OperatingSystem.iOS; attr.setKind = SetKind.平板; } if (source.IndexOf("手机", StringComparison.CurrentCultureIgnoreCase) != -1 || source.IndexOf("note", StringComparison.CurrentCultureIgnoreCase) != -1) { attr.setKind = SetKind.手机; } else if (source.IndexOf("平板", StringComparison.CurrentCultureIgnoreCase) != -1 || source.IndexOf("tab", StringComparison.CurrentCultureIgnoreCase) != -1) { attr.setKind = SetKind.平板; } if (attr.OS == OperatingSystem.其它 && source.IndexOf("智能") != -1) { attr.OS = OperatingSystem.Android; } WordSetKind.AddWord(source, attr); } Datas.ProgressBarCompelet = true; }
//读取数据 显示到 datagridviewEmo void runDisplayEmotion() { BlogReader breader; //if (Datas.sinaJSONList.Count > 0) // Datas.sinaJSONList.Clear(); if (Datas.sinaJSONList.Count == 0) { breader = new BlogReader("temp5.dat"); Datas.sinaJSONList = breader.ReadFromFile(); } Datas.ProgressBarState = "读取完成,正在分析..."; Datas.ProgresBarStyle = System.Windows.Forms.ProgressBarStyle.Marquee; string emo = ""; //表情键 List <string> emoKey = new List <string>(); List <EmoValue> emoValue = new List <EmoValue>(); using (FileStream fs = new FileStream("datas/emo.txt", FileMode.Open)) { StreamReader sr = new StreamReader(fs); while (!sr.EndOfStream) { string line = sr.ReadLine(); if (line.Length > 0) { emoKey.Add(line); emoValue.Add(new EmoValue()); } } sr.Close(); fs.Close(); } MatchCollection mCollection; foreach (SinaJSON item in Datas.sinaJSONList) { mCollection = Regex.Matches(item.Text, @"\[\w+\]"); for (int i = 0; i < mCollection.Count; i++) { emo = mCollection[i].Value; //emo = emo.Substring(1, emo.Length - 2); int index = emoKey.IndexOf(emo); int male = 0, female = 0; if (item.SinaUser.Gender.Equals("m")) { male++; } else { female++; } if (index != -1) { emoValue[index] = new EmoValue(emoValue[index].male + male, emoValue[index].female + female); } } } app = new Excel.Application(); workbooks = app.Workbooks as Excel.Workbooks; workbook = workbooks.Add(Type.Missing); app.DisplayAlerts = false; //删除 多余Sheet foreach (Worksheet ws in workbook.Worksheets) { if (ws != app.ActiveSheet) { ws.Delete(); } } foreach (Chart cht in workbook.Charts) { cht.Delete(); } //创建一个Sheet,存数据 //worksheet = (Worksheet)workbook. // Worksheets.Add(Type.Missing, workbook.ActiveSheet, // Type.Missing, Type.Missing); worksheet = workbook.Worksheets[1]; worksheet.Name = "数据"; int worksheetIndex = 0; for (int i = 0; i < emoKey.Count; i++) { if (emoValue[i].male > 0 || emoValue[i].female > 0) { worksheet.Cells[i + 1, 1] = emoKey[i]; worksheet.Cells[i + 1, 2] = emoValue[i].male; worksheet.Cells[i + 1, 3] = emoValue[i].female; worksheetIndex++; } } // TODO: 生成一个统计图对象: Chart xlChart = (Chart)workbook.Charts. Add(Type.Missing, worksheet, Type.Missing, Type.Missing); // TODO: 设定数据来源 Range cellRange = (Range)worksheet.Cells[1, 1]; // TODO: 通过向导生成Chart xlChart.ChartWizard(cellRange.CurrentRegion, XlChartType.xl3DColumn, Type.Missing, XlRowCol.xlColumns, 1, 0, true, "表情比较", "表情", "数量"); // TODO: 设置统计图Sheet的名称 xlChart.Name = "统计"; // TODO: 让12个Bar都显示不同的颜色 ChartGroup grp = (ChartGroup)xlChart.ChartGroups(1); grp.GapWidth = 20; grp.VaryByCategories = true; // TODO: 让Chart的条目的显示形状变成圆柱形,并给它们显示加上数据标签 Series s1 = (Series)grp.SeriesCollection(1); s1.Name = "男"; s1.BarShape = XlBarShape.xlCylinder; s1.HasDataLabels = true; Series s = (Series)grp.SeriesCollection(2); s.BarShape = XlBarShape.xlCylinder; s.HasDataLabels = true; s.Name = "女"; // TODO: 设置统计图的标题和图例的显示 xlChart.Legend.Position = XlLegendPosition.xlLegendPositionTop; xlChart.ChartTitle.Font.Size = 24; xlChart.ChartTitle.Shadow = false; xlChart.ChartTitle.Border.LineStyle = XlLineStyle.xlContinuous; // TODO: 设置两个轴的属性,Excel.XlAxisType.xlValue对应的是Y轴,Excel.XlAxisType.xlCategory对应的是X轴 Axis valueAxis = (Axis)xlChart.Axes(XlAxisType.xlValue, XlAxisGroup.xlPrimary); valueAxis.AxisTitle.Orientation = -90; Axis categoryAxis = (Axis)xlChart.Axes(XlAxisType.xlCategory, XlAxisGroup.xlPrimary); categoryAxis.AxisTitle.Font.Name = "宋体"; //-------------------------------------------------- //workbook.SaveAs(sPath, Type.Missing, Type.Missing, // Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange, // Type.Missing, Type.Missing, Type.Missing, Type.Missing, // Type.Missing); xlChart.CopyPicture(XlPictureAppearance.xlScreen, XlCopyPictureFormat.xlPicture); IntPtr hwnd = (IntPtr)app.Hwnd; Bitmap b = null; try { if (OpenClipboard(hwnd)) { IntPtr data = GetClipboardData(14); // CF_ENHMETAFILE 14 if (data != IntPtr.Zero) { using (Metafile mf = new Metafile(data, true)) { b = new Bitmap(mf); } } } } finally { CloseClipboard(); //clear workbook.Close(Type.Missing, Type.Missing, Type.Missing); app.Workbooks.Close(); app.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook); System.Runtime.InteropServices.Marshal.ReleaseComObject(app); workbook = null; app = null; xlChart = null; GC.Collect(); } this.Invoke(new updateDataGridViewEmo(doUpdateDataGridViewEmo), new object[] { emoKey, emoValue, b }); }
void doAutoAna() { int anaCount = 0; if (WordSetKind.Words.Count != 0) { WordSetKind.Words.Clear(); } if (Datas.sinaJSONList.Count == 0) { BlogReader br = new BlogReader(@"temp5.dat"); Datas.sinaJSONList = br.ReadFromFile(); } if (listBrands == null) listBrands = new List<SingleBrand>(); if (listBrands.Count == 0) { using (FileStream fs = new FileStream("datas/mobileDatas.dat", FileMode.Open)) { BinaryFormatter bf = new BinaryFormatter(); listBrands = bf.Deserialize(fs) as List<SingleBrand>; fs.Close(); } } foreach (SinaJSON item in Datas.sinaJSONList) { anaCount++; Datas.ProgressBarState = "正在分析第" + anaCount + "个"; string source = Regex.Match(item.Source, @">[^<]+").Value; source = source.Substring(1, source.Length - 1); SetAttribute attr = new SetAttribute(); short position = 0; //位置变量 foreach (SingleBrand singleBrand in listBrands) { string[] name = singleBrand.BrandName.Replace(")", "").Split('('); if (name.Length == 2) { if (source.IndexOf(name[0]) != -1 || source.IndexOf(name[1]) != -1) { attr.BrandName = singleBrand.BrandName; break; } } else { if (source.IndexOf(name[0]) != -1) { attr.BrandName = singleBrand.BrandName; break; } } position++; } //品牌匹配成功 if (attr.BrandName != null) { foreach (Model model in listBrands[position].ListModels) { //存在型号 if (model.ModelName != null && model.ModelName.IndexOf(source) != -1) { if (model.ModelName.IndexOf("android", StringComparison.CurrentCultureIgnoreCase) != -1) { attr.OS = OperatingSystem.Android; } else if (model.ModelName.IndexOf("iOS", StringComparison.CurrentCultureIgnoreCase) != -1) attr.OS = OperatingSystem.iOS; if (attr.OS != OperatingSystem.未知) { attr.setKind = SetKind.手机; break; } } } if (attr.OS == OperatingSystem.未知) { attr.OS = OperatingSystem.Android; } if (attr.setKind == SetKind.未知) attr.setKind = SetKind.手机; } //品牌匹配失败 else { //大系统确定 if (source.IndexOf("android", StringComparison.CurrentCultureIgnoreCase) != -1) { attr.OS = OperatingSystem.Android; attr.setKind = SetKind.未知; } else if (source.IndexOf("ios", StringComparison.CurrentCultureIgnoreCase) != -1) { attr.OS = OperatingSystem.iOS; attr.setKind = SetKind.未知; } else if (source.IndexOf("win8", StringComparison.CurrentCultureIgnoreCase) != -1) { attr.OS = OperatingSystem.Windows; attr.setKind = SetKind.PC; } else { attr.OS = OperatingSystem.其它; attr.setKind = SetKind.未知; } attr.BrandName = "其它"; } if (source.IndexOf("window", StringComparison.CurrentCultureIgnoreCase) != -1) attr.OS = OperatingSystem.WindowsPhone; else if (source.IndexOf("s60", StringComparison.CurrentCultureIgnoreCase) != -1 || source.IndexOf("s40", StringComparison.CurrentCultureIgnoreCase) != -1 || source.IndexOf("s30", StringComparison.CurrentCultureIgnoreCase) != -1) attr.OS = OperatingSystem.塞班; if (source.IndexOf("phone", StringComparison.CurrentCultureIgnoreCase) != -1) { attr.setKind = SetKind.手机; } if (source.IndexOf("iphone", StringComparison.CurrentCultureIgnoreCase) != -1) { attr.OS = OperatingSystem.iOS; attr.setKind = SetKind.手机; } else if (source.IndexOf("ipad", StringComparison.CurrentCultureIgnoreCase) != -1) { attr.OS = OperatingSystem.iOS; attr.setKind = SetKind.平板; } if (source.IndexOf("手机", StringComparison.CurrentCultureIgnoreCase) != -1 || source.IndexOf("note", StringComparison.CurrentCultureIgnoreCase) != -1) { attr.setKind = SetKind.手机; } else if (source.IndexOf("平板", StringComparison.CurrentCultureIgnoreCase) != -1 || source.IndexOf("tab", StringComparison.CurrentCultureIgnoreCase) != -1) { attr.setKind = SetKind.平板; } if (attr.OS == OperatingSystem.其它 && source.IndexOf("智能") != -1) { attr.OS = OperatingSystem.Android; } WordSetKind.AddWord(source, attr); } Datas.ProgressBarCompelet = true; }