public static void PrepareLabelFileWithLineNumber(string readFilePath, string writeFilePath) { StringBuilder sb = new StringBuilder(); if (File.Exists(readFilePath)) { StreamReader sr1 = File.OpenText(readFilePath); string s = ""; int counter = 1; while ((s = sr1.ReadLine()) != null) { var lineOutput = counter++ + " " + s + "\n"; Console.WriteLine(lineOutput); sb.Append(lineOutput); } sr1.Close(); } else { Console.WriteLine("File not found"); Console.ReadLine(); } Console.WriteLine(); File_Helper.WriteToFile(sb, writeFilePath); }
public static void classifierOne(string classifierFileName, string predictionModel) { FileReader javaFileReader = new FileReader(classifierFileName); weka.core.Instances wekaInsts = new weka.core.Instances(javaFileReader); javaFileReader.close(); wekaInsts.setClassIndex(wekaInsts.numAttributes() - 1); Classifier cl = new SMO(); //Classifier cl = new NaiveBayes(); java.util.Random random = new java.util.Random(1); Evaluation evaluation = new Evaluation(wekaInsts); evaluation.crossValidateModel(cl, wekaInsts, 10, random); foreach (object o in evaluation.getMetricsToDisplay().toArray()) { } int count = 0; StringBuilder sb = new StringBuilder(); foreach (object o in evaluation.predictions().toArray()) { NominalPrediction prediction = o as NominalPrediction; if (prediction != null) { double[] distribution = prediction.distribution(); double predicted = prediction.predicted(); double actual = prediction.actual(); string revision = prediction.getRevision(); double weight = prediction.weight(); double margine = prediction.margin(); //bool equals = prediction.@equals(); string distributions = String.Empty; for (int i = 0; i < distribution.Length; i++) { //System.Console.WriteLine(distribution[i]); distributions += distribution[i]; } var predictionLine = String.Format("{0} - {1} - {2} - {3} - {4} - {5}\n", actual, predicted, revision, weight, margine, distributions); sb.Append(predictionLine); //System.Console.WriteLine(predicted); } count++; } File_Helper.WriteToFile(sb, predictionModel + "NbCl.txt"); System.Console.WriteLine(count); System.Console.ReadKey(); }
public static void BR(int evaluation, string BR_resultFilePath) { Dictionary <int, string> resultSet = new Dictionary <int, string>(); for (int i = 0; i < evaluation; i++) { resultSet[i] = BinaryRelAnalysis.binaryRelevenceData().ToString(); } StringBuilder sb = new StringBuilder(); foreach (var key in resultSet.Keys) { var data = key.ToString() + " time evaluation result \n " + resultSet[key] + "\n"; sb.Append(data); } File_Helper.WriteToFile(sb, BR_resultFilePath); Console.ReadKey(); }
public static void binaryRelevenceDataPreparation(Dictionary <string, string> MultiLevelClass, string binaryRelBaseFileName) { var binaryRelevenceClass = new Dictionary <int, string>(); int classes = 12; var fileName = ""; for (int oneClass = 1; oneClass <= classes; oneClass++) { var binaryFileEach = ""; StringBuilder sb = new StringBuilder(); foreach (var key in MultiLevelClass.Keys) { var multiLevelClass = MultiLevelClass[key].ToString(); string[] a = multiLevelClass.Split(' '); if (oneClass == Convert.ToInt32(a[0])) { binaryRelevenceClass[Convert.ToInt32(key)] = a[0]; } else if (Convert.ToInt32(a[1]) != 0 && oneClass == Convert.ToInt32(a[1])) { binaryRelevenceClass[Convert.ToInt32(key)] = a[1]; } else if (Convert.ToInt32(a[2]) != 0 && oneClass == Convert.ToInt32(a[2])) { binaryRelevenceClass[Convert.ToInt32(key)] = a[2]; } else { binaryRelevenceClass[Convert.ToInt32(key)] = "0"; } binaryFileEach = binaryRelevenceClass[Convert.ToInt32(key)].ToString() + "\n"; sb.Append(binaryFileEach); } fileName = String.Format("{0}_{1}.txt", binaryRelBaseFileName, oneClass); File_Helper.WriteToFile(sb, fileName); } Console.Write(""); }
public static void MergeTwoClassFileWithMultiClass(string[] files, string writeFilePath) { var hash = new Dictionary <int, string>(); foreach (var file in files) { string[] fileContents = File.ReadAllLines(file); foreach (string line in fileContents) { string[] a = line.Split(' '); if (!hash.Keys.Contains(Convert.ToInt32(a[0]))) { if (a.Length > 2) { //hash[Convert.ToInt32(a[0])] = a[1] + " " + a[2]; hash[Convert.ToInt32(a[0])] = a[1] + " " + a[2] + " " + a[3]; } else { hash[Convert.ToInt32(a[0])] = a[1] + " " + "0" + " " + "0"; } } } } var orderedWithLineNumber = hash.OrderBy(key => key.Key); var orderedWithLineNumberDictionary = orderedWithLineNumber.ToDictionary((keyItem) => keyItem.Key, (valueItem) => valueItem.Value); StringBuilder sb = new StringBuilder(); foreach (var key in orderedWithLineNumberDictionary.Keys) { var multiLevelClass = key.ToString() + ":" + orderedWithLineNumberDictionary[key].ToString() + "\n"; Console.WriteLine(key + ":" + orderedWithLineNumberDictionary[key]); sb.Append(multiLevelClass); } File_Helper.WriteToFile(sb, writeFilePath); }
public static void pssmFilePreparation() { string currentDirectory = Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())); currentDirectory = currentDirectory.TrimEnd('\\'); currentDirectory = currentDirectory.Remove(currentDirectory.LastIndexOf('\\') + 1); string BaseFilePath = Path.Combine(currentDirectory, "plant2_pssm\\"); string pssmConvertedFilePath = Path.Combine(currentDirectory, "pssmConvertedPlant2\\pssm"); int fileCount = (from file in Directory.EnumerateFiles(BaseFilePath, "pssm*.txt", SearchOption.AllDirectories) select file).Count(); for (int i = 1; i <= fileCount; i++) { string filePath = BaseFilePath + "pssm" + i + ".txt"; if (File.Exists(filePath)) { string[] fileContents = File.ReadAllLines(filePath); string[] slicedAllFileContents = fileContents.Skip(3).Take(fileContents.Length).ToArray(); //string[] slicedfileContents = slicedAllFileContents.Substring(10); string[] resultLines = new string[fileContents.Length]; int lineNumber = 0; foreach (string line in slicedAllFileContents) { if (!(string.IsNullOrEmpty(line)) && (line.Length > 9)) { string eachLine = line.Substring(9); string eachLineWithComma = Regex.Replace(eachLine, ".{3}", "$0,"); string eachLineWithoutWhiteSpace = Regex.Replace(eachLineWithComma, @"\s+", ""); string[] lineCharArray = eachLineWithoutWhiteSpace.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); string[] cleanedArray = lineCharArray.Where(x => !string.IsNullOrEmpty(x)).ToArray(); string[] slicedArray = cleanedArray.Take(20).ToArray(); string resultString = string.Join(",", slicedArray); int n; if (int.TryParse(slicedArray[0], out n)) { int test; if (int.TryParse(resultString.Split(',').Sum(x => int.Parse(x)).ToString(), out test)) { resultLines[lineNumber] = resultString; } } lineNumber++; } } string[] cleanedresultLines = resultLines.Where(x => !string.IsNullOrEmpty(x)).ToArray(); string allLinesOfSingleFile = string.Join("\n", cleanedresultLines); StringBuilder sb = new StringBuilder(); sb.Append(allLinesOfSingleFile); File_Helper.WriteToFile(sb, (pssmConvertedFilePath + i + ".txt")); } else { Console.WriteLine("File doesn't exist\n" + filePath); } } }
public static int MultiClassToSingleClassConverter(string multiLevelClassFileName, string ClassFileWritename, string classMappingFile, string multiLevelClassFileWithLineNumberWritename, string binaryRelFilePath) { var MultiLevelClass = new Dictionary <string, string>(); var NewMultiLevelClass = new Dictionary <int, string>(); var classMapping = new Dictionary <string, string>(); string[] fileContents = File.ReadAllLines(multiLevelClassFileName); foreach (string line in fileContents) { string[] a = line.Split(':'); if (!MultiLevelClass.Keys.Contains(a[0])) { MultiLevelClass[a[0]] = a[1]; NewMultiLevelClass[Convert.ToInt32(a[0])] = a[1]; } } var distinctList = MultiLevelClass.Values.Distinct().ToList(); int numberOfClass = Convert.ToInt32(distinctList.Count); int MakeNewClass = 0; foreach (var distinct in distinctList) { MakeNewClass++; foreach (KeyValuePair <string, string> eachDictionaryItem in MultiLevelClass) { if (eachDictionaryItem.Value == distinct) { NewMultiLevelClass[Convert.ToInt32(eachDictionaryItem.Key)] = MakeNewClass.ToString(); Console.WriteLine(""); } } classMapping["Class" + MakeNewClass.ToString()] = distinct.ToString(); } //classMapingToFile(NewMultiLevelClass, distinctList); StringBuilder sb = new StringBuilder(); StringBuilder sbLine = new StringBuilder(); foreach (var key in NewMultiLevelClass.Keys) { var multiLevelClass = NewMultiLevelClass[key].ToString() + "\n"; var multiLevelClassWithLineNumber = key + ":" + NewMultiLevelClass[key].ToString() + "\n"; Console.WriteLine(key + ":" + NewMultiLevelClass[key]); sb.Append(multiLevelClass); sbLine.Append(multiLevelClassWithLineNumber); } File_Helper.WriteToFile(sb, ClassFileWritename); File_Helper.WriteToFile(sbLine, multiLevelClassFileWithLineNumberWritename); StringBuilder sb1 = new StringBuilder(); foreach (var key in classMapping.Keys) { var multiLevelClass = key + ":" + classMapping[key].ToString() + "\n"; Console.WriteLine(key + ":" + classMapping[key]); sb1.Append(multiLevelClass); } File_Helper.WriteToFile(sb1, classMappingFile); binaryRelevenceDataPreparation(MultiLevelClass, binaryRelFilePath); return(numberOfClass); }