/// <summary> /// Helper method to read the conversation from <paramref name="inputFilePath"/>. /// </summary> /// <param name="inputFilePath"> /// The input file path. /// </param> /// <returns> /// A <see cref="Conversation"/> model representing the conversation. /// </returns> /// <exception cref="ArgumentException"> /// Thrown when the input file could not be found. /// </exception> /// <exception cref="Exception"> /// Thrown when something else went wrong. /// </exception> public async Task <Conversation> ReadConversation(string inputFilePath, string outputFilePath, string user, string keyword = "", string blacklist = "") { StreamReader reader = null; Conversation conversation; reader = HandleFileAsync(inputFilePath); Task <Conversation> task = getLines(reader, user, keyword, blacklist); conversation = await task; //find out active users messageActiveUsers mActiveUsers = this.findActive(conversation); this.WriteConversation(conversation, mActiveUsers, outputFilePath); Console.WriteLine("Conversation exported from '{0}' to '{1}'", inputFilePath, outputFilePath); return(conversation); }
private messageActiveUsers findActive(Conversation conversation) { messageActiveUsers mActive = new messageActiveUsers(); if (logUsers.Count > 0) { List <string> uniqueUsers = logUsers.Distinct().ToList(); foreach (string us in uniqueUsers) { int count = logUsers.Where(s => s == us).Count(); ActiveUsers acUser = new ActiveUsers() { name = us, times = count }; activeUsers.Add(acUser); } mActive.mostActiveUser = activeUsers[0].name; mActive.activeUsers = activeUsers; } return(mActive); }
/// <summary> /// Helper method to write the <paramref name="conversation"/> as JSON to <paramref name="outputFilePath"/>. /// </summary> /// <param name="conversation"> /// The conversation. /// </param> /// <param name="outputFilePath"> /// The output file path. /// </param> /// <exception cref="ArgumentException"> /// Thrown when there is a problem with the <paramref name="outputFilePath"/>. /// </exception> /// <exception cref="Exception"> /// Thrown when something else bad happens. /// </exception> public void WriteConversation(Conversation conversation, messageActiveUsers mActive, string outputFilePath) { StreamWriter writer = null; try { writer = new StreamWriter(new FileStream(outputFilePath, FileMode.Create, FileAccess.ReadWrite)); } catch (SecurityException) { throw new ArgumentException("No permission to file."); } catch (DirectoryNotFoundException) { throw new ArgumentException("Path invalid."); } try { Output output = new Output() { conversation = conversation, mActiveUsers = mActive }; var serialized = JsonConvert.SerializeObject(output, Formatting.Indented); writer.Write(serialized); writer.Flush(); writer.Close(); } catch (IOException) { throw new Exception("Something went wrong in the IO."); } }