Example #1
0
        /// <summary> Attempts to load a cost matrix.
		/// 
		/// </summary>
		/// <param name="costFileName">the filename of the cost matrix
		/// </param>
		/// <param name="numClasses">the number of classes that should be in the cost matrix
		/// (only used if the cost file is in old format).
		/// </param>
		/// <returns> a <code>CostMatrix</code> value, or null if costFileName is empty
		/// </returns>
		/// <throws>  Exception if an error occurs. </throws>
		protected internal static CostMatrix handleCostOption(System.String costFileName, int numClasses)
		{
			
			if ((costFileName != null) && (costFileName.Length != 0))
			{
				System.Console.Out.WriteLine("NOTE: The behaviour of the -m option has changed between WEKA 3.0" + " and WEKA 3.1. -m now carries out cost-sensitive *evaluation*" + " only. For cost-sensitive *prediction*, use one of the" + " cost-sensitive metaschemes such as" + " weka.classifiers.meta.CostSensitiveClassifier or" + " weka.classifiers.meta.MetaCost");
				
				//UPGRADE_ISSUE: Class hierarchy differences between 'java.io.Reader' and 'System.IO.StreamReader' may cause compilation errors. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1186'"
				System.IO.StreamReader costReader = null;
				try
				{
					//UPGRADE_TODO: The differences in the expected value  of parameters for constructor 'java.io.BufferedReader.BufferedReader'  may cause compilation errors.  "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'"
					//UPGRADE_WARNING: At least one expression was used more than once in the target code. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1181'"
					//UPGRADE_TODO: Constructor 'java.io.FileReader.FileReader' was converted to 'System.IO.StreamReader' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073'"
					costReader = new System.IO.StreamReader(new System.IO.StreamReader(costFileName, System.Text.Encoding.Default).BaseStream, new System.IO.StreamReader(costFileName, System.Text.Encoding.Default).CurrentEncoding);
				}
				catch (System.Exception e)
				{
					//UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.getMessage' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
					throw new System.Exception("Can't open file " + e.Message + '.');
				}
				try
				{
					// First try as a proper cost matrix format
					return new CostMatrix(costReader);
				}
				catch (System.Exception ex)
				{
					try
					{
						// Now try as the poxy old format :-)
						//System.err.println("Attempting to read old format cost file");
						try
						{
							costReader.Close(); // Close the old one
							//UPGRADE_TODO: The differences in the expected value  of parameters for constructor 'java.io.BufferedReader.BufferedReader'  may cause compilation errors.  "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'"
							//UPGRADE_WARNING: At least one expression was used more than once in the target code. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1181'"
							//UPGRADE_TODO: Constructor 'java.io.FileReader.FileReader' was converted to 'System.IO.StreamReader' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073'"
							costReader = new System.IO.StreamReader(new System.IO.StreamReader(costFileName, System.Text.Encoding.Default).BaseStream, new System.IO.StreamReader(costFileName, System.Text.Encoding.Default).CurrentEncoding);
						}
						catch (System.Exception e)
						{
							//UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.getMessage' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
							throw new System.Exception("Can't open file " + e.Message + '.');
						}
						CostMatrix costMatrix = new CostMatrix(numClasses);
						//System.err.println("Created default cost matrix");
						costMatrix.readOldFormat(costReader);
						return costMatrix;
						//System.err.println("Read old format");
					}
					catch (System.Exception e2)
					{
						// re-throw the original exception
						//System.err.println("Re-throwing original exception");
						throw ex;
					}
				}
			}
			else
			{
				return null;
			}
		}