|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using Microsoft.ML; |
| 4 | + |
| 5 | +namespace GitCommitAnalyser |
| 6 | +{ |
| 7 | + public class CommitInteractiveLabeler |
| 8 | + { |
| 9 | + private readonly MLContext _mlContext; |
| 10 | + private readonly ITransformer _model; |
| 11 | + private readonly Dictionary<uint, string> _clusterNames; |
| 12 | + private readonly PredictionEngine<CommitMLData, CommitPredictionWithData> _predictionEngine; |
| 13 | + |
| 14 | + public CommitInteractiveLabeler(MLContext mlContext, ITransformer model, Dictionary<uint, string> clusterNames) |
| 15 | + { |
| 16 | + _mlContext = mlContext; |
| 17 | + _model = model; |
| 18 | + _clusterNames = clusterNames; |
| 19 | + |
| 20 | + // Create a Prediction Engine for single-item inference |
| 21 | + _predictionEngine = _mlContext.Model.CreatePredictionEngine<CommitMLData, CommitPredictionWithData>(_model); |
| 22 | + } |
| 23 | + |
| 24 | + public void StartInteractiveLoop() |
| 25 | + { |
| 26 | + Console.WriteLine("\n========================================"); |
| 27 | + Console.WriteLine("--- Interactive Commit Formatting ---"); |
| 28 | + Console.WriteLine("Enter a raw commit message to see it mapped to its AI cluster label."); |
| 29 | + Console.WriteLine("Type 'exit' to quit."); |
| 30 | + Console.WriteLine("========================================\n"); |
| 31 | + |
| 32 | + while (true) |
| 33 | + { |
| 34 | + Console.Write("Enter commit message: "); |
| 35 | + var input = Console.ReadLine(); |
| 36 | + |
| 37 | + if (string.IsNullOrWhiteSpace(input)) continue; |
| 38 | + if (input.Equals("exit", StringComparison.OrdinalIgnoreCase)) |
| 39 | + { |
| 40 | + Console.WriteLine("Exiting interactive mode."); |
| 41 | + break; |
| 42 | + } |
| 43 | + |
| 44 | + var formattedCommit = FormatCommitMessage(input); |
| 45 | + Console.WriteLine($"Formatted => {formattedCommit}\n"); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + public string FormatCommitMessage(string commitName) |
| 50 | + { |
| 51 | + // Prepare the input data for the ML.NET model |
| 52 | + var inputData = new CommitMLData |
| 53 | + { |
| 54 | + CommitName = commitName, |
| 55 | + CommitDescription = string.Empty, |
| 56 | + Repository = "InteractiveInput" |
| 57 | + }; |
| 58 | + |
| 59 | + // Predict which cluster it belongs to |
| 60 | + var prediction = _predictionEngine.Predict(inputData); |
| 61 | + |
| 62 | + // Fetch the human-readable label from the Gemini cached Dictionary |
| 63 | + string label = _clusterNames != null && _clusterNames.TryGetValue(prediction.PredictedClusterId, out var name) |
| 64 | + ? name |
| 65 | + : $"Cluster_{prediction.PredictedClusterId}"; |
| 66 | + |
| 67 | + // Map spaces to hyphens/underscores if you want a strict prefix, |
| 68 | + // but keeping it as the Gemini-provided label string works directly. |
| 69 | + // Result: "Bug Fix / UI: fixed the button alignment" |
| 70 | + return $"{label}: {commitName}"; |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments