/* * Function: ReadFile * Description:TRIStrategy算法读取函数 * Parameters: * string sPath 文件路径 * StyleSet pStyle 绘制样式集 * Return Value:cNet */ cNet IfIOStrategy.ReadFile(string sPath) { StreamReader Reader; cNet NewNet; string sLine = ""; int i, intLines; intLines = CountNodes(sPath); NewNet = new cNet(intLines); for (i = 0; i < intLines; i++) { NewNet.Network.Add(new cNode(i)); } Reader = new StreamReader(sPath, Encoding.GetEncoding(strEncoding));//设定编码,因为存在中文,采用国标2312编码 if (Reader == null) { return null; } do { sLine = Reader.ReadLine().Trim(); if (sLine != "" && sLine.StartsWith(strComment) == false) //文本不为空,也不是注释 { PhraseTriad(ref NewNet, sLine, strSeparator); //对读入文本进行解释 } } while (Reader.EndOfStream == false); //文件终止 Reader.Close(); //关闭文件 Reader.Dispose(); if (NewNet.intNumber == 0) { return null; } return NewNet; }
int intNumOffset = 0; //如果等于0则代表新版本,编号从0开始,如果为1,代表老版本,编号从1开始。 /* * Function: ReadFile * Description:SSTStrategy算法读取函数 * Parameters: * string sPath 文件路径 * StyleSet pStyle 绘制样式集 * Return Value:cNet */ cNet IfIOStrategy.ReadFile(string sPath) { StreamReader Reader; cNet NewNet; List<string> store; string sLine = ""; int intLines = 0; int intTemp; Reader = new StreamReader(sPath, Encoding.GetEncoding(strEncoding)); if(Reader == null) { return null; } store = new List<string>(); do { //读出一行 sLine = Reader.ReadLine().Trim(); //检查是否空行或注释 if (sLine != "" && sLine.StartsWith(strCommet) == false) { //将生成的字符串保存入列表 store.Add(sLine); if (intLines == 0) { intTemp = sLine.IndexOf('-'); if (sLine.Substring(0, intTemp) == "1") { intNumOffset = 1; } } intLines++; } } while (Reader.EndOfStream == false); Reader.Close(); Reader.Dispose(); //初始化网络 NewNet = new cNet(intLines); intLines = 0; //遍历节点字符串列表,加入网络 foreach(string strLine in store) { //将解析后生成的节点加入网络 NewNet.Network.Add(SstInit(strLine, intLines)); intLines++; } if (NewNet.intNumber == 0) { return null; } return NewNet; }
/* * Function: ReadFile * Description:XMLStrategy算法读取函数 * Parameters: * string sPath 文件路径 * StyleSet pStyle 绘制样式集 * Return Value:cNet */ cNet IfIOStrategy.ReadFile(string sPath) { FileStream stream = null; XmlDocument doc = new XmlDocument(); XmlNodeList Nodelist; XmlNode xmlroot, xmltmp; cNet NewNet; int iNum; try { stream = new FileStream(sPath, FileMode.Open); doc.Load(stream); //从流文件读入xml文档 stream.Close(); } catch (Exception ex) { if (stream != null) { ex.ToString(); stream.Dispose(); } return null; } stream.Dispose(); xmlroot = doc.ChildNodes.Item(0); Nodelist = xmlroot.ChildNodes; //获取节点列表 xmltmp = Nodelist[0].ChildNodes[3]; //创建网络 NewNet = new cNet(Nodelist.Count); for (iNum = 0; iNum < Nodelist.Count; iNum++) { switch (xmltmp.Name) {//区分XML数据类型 case "Word"://如果包含Word标签,就是关键词网络文件,生成KNode NewNet.Network.Add(new kNode(iNum)); break; default://默认都是cNode NewNet.Network.Add(new cNode(iNum)); break; } } NewNet.XMLtoNet(doc); if (NewNet.intNumber == 0) { return null; } return NewNet; }
//窗体控件重置 public void FormReset() { ComplexNet = null; PicCam.Image= null; //控件重置重置 TabMain.TabPages.Clear(); TabMain.TabPages.Add(TabStructure); //列表框重置 NetNum.Text = ""; NetDeg.Text = ""; EdgeNum.Text = ""; MaxDegree.Text = ""; MaxNode.Text = ""; MinDegree.Text = ""; NodeList.Items.Clear(); }
/* * Function: PhraseTriad * Description:TRIStrategy算法读取函数 * Parameters: * ref cNet cNetwork 待处理网络 * string sLine 待分析字符串 * string Separator 分隔符 * Return Value: */ private static void PhraseTriad(ref cNet cNetwork, string sLine, string Separator) { int intSour, intTar; double dubValue; string[] strSeg; dubValue = 1.0; strSeg = sLine.Split(new string[] { Separator }, StringSplitOptions.None);//首先使用Separator进行信息区块划分 intSour = Convert.ToInt32(strSeg[intSourceOffset]); intTar = Convert.ToInt32(strSeg[intTargetOffset]); if (strSeg.Length == 3) { dubValue = Convert.ToDouble(strSeg[intValueOffset]); } cNetwork.AddEdge(intSour, intTar, dubValue); //根据读出的信息加边 }
//OK按钮响应函数 private void OK_Button_Click(object sender, EventArgs e) { CreateParameter cParam; IfCreateStrategy Creator; int iNumber, iPara1, iPara2; bool bOption = true; if (TypeCombo.Text == "") { MessageBox.Show("请先选择一种网络类型!", "警告", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation); } Cursor = Cursors.WaitCursor; this.Refresh(); //根据用户选择项,装载不同参数 switch (TypeCombo.Text) //选择网络类型 { case "BA无标度网络": iNumber = Convert.ToInt32(BANum.Value); iPara1 = Convert.ToInt32(BAInit.Value); iPara2 = Convert.ToInt32(BALimit.Value); if (FullNet.Checked == true) { bOption = true; } else { bOption = false; } Creator = new BAStrategy(); break; case "ER随机图": iNumber = Convert.ToInt32(ERNum.Value); iPara1 = Convert.ToInt32(EREdge.Value); iPara2 = Convert.ToInt32(ERPro.Value); if (BaseEdge.Checked == true) { bOption = true; } else { bOption = false; } Creator = new ERStrategy(); break; case "小世界网络": iNumber = Convert.ToInt32(SWNum.Value); iPara1 = Convert.ToInt32(SWnei.Value); iPara2 = Convert.ToInt32(SWPro.Value); if (WS_SW.Checked == true) { bOption = true; } else if (NW_SW.Checked == true) { bOption = false; } Creator = new SWstrategy(); break; default: return; } cParam = new CreateParameter(iNumber, iPara1, iPara2, bOption); //调用接口Create创建函数 cNetwork = Creator.Create(cParam); Cursor = Cursors.Arrow; if (cNetwork == null) { this.DialogResult = System.Windows.Forms.DialogResult.Abort; } else { this.DialogResult = System.Windows.Forms.DialogResult.OK; } this.Close(); }
/* * Function: BuileNetwork * Description:MATStrategy算法读取函数 * Parameters: * Byte[,] bytMatrix 从邻接矩阵生成cNet网络 * StyleSet pStyle 绘制样式集 * Return Value:cNet */ cNet BuileNetwork(Byte[,] bytMatrix) { cNet NewNet; IfPlatform NewNode; int intRow, intCol, i, j; intRow = bytMatrix.GetLength(0); intCol = bytMatrix.GetLength(1); NewNet = new cNet(intRow); for(i = 0; i < intRow; i++) { NewNode = new cNode(i); for(j = 0; j<intCol; j++) { if(bytMatrix[i, j] != 0) { NewNode.AddEdge(j, (double)bytMatrix[i, j]); } } NewNet.Network.Add(NewNode); } if (NewNet.intNumber == 0) { return null; } return NewNet; }
/* * Function: FindNodeinNet * Description:查找Net中拥有目标索引的词语的节点编号 * Parameters: * int iLine 句子编号 * int iPos 位置索引 * cNet newNet 待搜索网络 * Return Value:int */ int FindNodeinNet(int iLine, int iPos, cNet newNet) {//遍历网络中的节点,找到对应索引(句号,句中位置)则返回节点编号 foreach (IfKeyWord curNode in newNet.Network) { if ((curNode.Line == iLine) && (curNode.Position == iPos)) { return curNode.Number; } } return -1; }
/* * Function: FindNode * Description:查找Net中拥有目标索引的词语的节点编号 * Parameters: * int iLine 句子编号 * int iPos 位置索引 * List<WordResult> WordList 词语列表 * cNet newNet 待搜索网络 * Return Value:int */ int FindNode(int iLine, int iPos, List<WordResult> WordList, cNet newNet) {//核心算法D, 查找cNet中对应索引(句子号,句中位置)对应的节点编号 int intOwnerLine, intOwnerPos; foreach (WordResult word in WordList) {//首先在WordList中找到该节点,WordList中信息最完整 if ((word.Line != iLine) || (word.Position != iPos)) {//两个索引不匹配,换下一个词语 continue; } if (word.IsIgnore == false) {//当前词语未被筛选掉或被兼并,在Net中查找节点编号。 return FindNodeinNet(iLine, iPos, newNet); } if (word.IsMerged == true) {//当前节点被兼并,返回其从属节点的句号,位置 intOwnerLine = word.Owner.Key; intOwnerPos = word.Owner.Value; //返回当前节点的宗主节点的编号 return FindNodeinNet(intOwnerLine, intOwnerPos, newNet); } } return -1; }
/* * Function: BuildKeyWordNet * Description:构建关键词网络 * Parameters: * List<kNode> NodeList 待构建节点 * List<WordResult> WordList 词语列表 * StyleSet PaintStyle 绘制样式集 * Return Value:cNet */ cNet BuildKeyWordNet(List<kNode> NodeList, List<WordResult> WordList) {//核心算法C, 构造网络,主要任务是找到符合条件的两个节点,在他们之间加边。 cNet NewNet = null; int intSource, intTarget, intSourLine, intSourPos; //1 构造网络并加入节点 NewNet = new cNet(NodeList.Count); foreach (IfPlatform curNode in NodeList) { NewNet.Network.Add(curNode); } //遍历词语列表 foreach (WordResult word in WordList) { if(word.IsIgnore == true) {//被忽略节点则直接跳到下一个。 continue; } intSourLine = word.Line;//获取词语的句号,位置,作为词语的唯一全局索引(每个词语都不同) intSourPos = word.Position; //查找对应索引在cNet中的节点的编号,查到返回,查不到返回-1 intSource = FindNode(intSourLine, intSourPos, WordList, NewNet); for (int i = 1; i <= intFollowUp; i++) {//对当前节点的后面intFollowUp个邻居节点进行遍历 //返回对应索引的邻居节点在cNet中的节点的编号 intTarget = FindNode(intSourLine, intSourPos + i, WordList, NewNet); if (intTarget < 0) {//没有邻居则跳出 break; } //执行加边 NewNet.AddEdge(intSource, intTarget, 1); } } if (NewNet.intNumber == 0) { return null; } return NewNet; }
//文件读取函数,输入文件路径 void Read(string sPath) { Error eRet = null; FormReset(); //重置当前系统 ComplexNet = cNet.Read(sPath, ref eRet); //调用下层函数,文件解析 if (eRet.intError != 0) { Cursor = Cursors.Arrow; TSSL1.Text = "就绪"; MessageBox.Show(eRet.ToString(), "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } Initialized(); }
//新建菜单项响应函数 private void NewMI_Click(object sender, EventArgs e) { DialogResult choice; if (ComplexNet != null) //确保网络为空 { choice =MessageBox.Show("是否保存当前网络?", "警告", MessageBoxButtons.YesNoCancel , MessageBoxIcon.Exclamation); if( choice ==DialogResult .Yes ) { if(SaveNetwork() ==false ) //保存网络 { return; } } else if( choice ==DialogResult .No ) { } else if(choice ==DialogResult.Cancel ) { return ; } } DiaCreate diaNew = new DiaCreate(this); //新建网络对话框,新建网络 if (diaNew.ShowDialog(this) == DialogResult.OK) { FormReset(); //重置当前系统 ComplexNet = diaNew.cNetwork; Initialized(); } else if (diaNew.ShowDialog(this) == DialogResult.Abort) { MessageBox.Show("网络创建失败", "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); TSSL1.Text = "就绪"; } diaNew.Dispose(); }