🐈
ローカルLLMに一つのファイル内の関数の関係を列挙させてみた。
DeepSeek-r1 14bで検証 ollama GUI環境
以下が固定文
Please enumerate the functions and the functions called within them from the program shown below. Please represent this by structuring it as folders and files. The purpose is to clarify the relationship between the defined functions. However, please be careful not to make it too verbose, as it would lose its meaning as a map. Please respond in Japanese.
渡したコード
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Text.Json; // Added for JSON serialization/deserialization
using System.Linq; // この行を追加してください
using System.Text.RegularExpressions;
class Program
{
private static readonly Random random = new Random();
static void Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine("使用方法: html_create.exe [プロジェクトフォルダパス]");
return;
}
string projectFolderPath = args[0];
if (!Directory.Exists(projectFolderPath))
{
Console.WriteLine($"エラー: 指定されたプロジェクトフォルダ '{projectFolderPath}' が見つかりません。");
return;
}
string mainTemplatePath = Path.Combine(projectFolderPath, "main_template.txt");
if (!File.Exists(mainTemplatePath))
{
Console.WriteLine($"エラー: プロジェクトフォルダ内にテンプレートファイル 'main_template.txt' が見つかりません。");
return;
}
// JSONファイルからマッピング情報を読み込む
string mappingFilePath = Path.Combine(projectFolderPath, "component_mapping.json");
if (!File.Exists(mappingFilePath))
{
Console.WriteLine($"エラー: マッピングファイル 'component_mapping.json' がプロジェクトフォルダ内に見つかりません。");
return;
}
try
{
// JSONファイルの内容を読み込み、ディクショナリにデシリアライズする
string jsonContent = File.ReadAllText(mappingFilePath);
var componentMapping = JsonSerializer.Deserialize<Dictionary<string, string>>(jsonContent);
ProcessDocument(projectFolderPath, componentMapping, mainTemplatePath);
}
catch (JsonException ex)
{
Console.WriteLine($"エラー: 'component_mapping.json' の解析中にエラーが発生しました。ファイル形式を確認してください。");
Console.WriteLine($"詳細: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"エラーが発生しました: {ex.Message}");
}
Console.WriteLine("終了するには何かキーを押してください...");
Console.ReadLine();
}
/// <summary>
/// メインのドキュメント処理を実行する
/// </summary>
static void ProcessDocument(string projectFolderPath, Dictionary<string, string> componentMapping, string mainTemplatePath)
{
string componentBaseDir = "component"; // 実行ファイルと同じ階層にあるcomponentフォルダ
// テンプレートファイルを読み込み
string mainTemplateContent = File.ReadAllText(mainTemplatePath, Encoding.UTF8);
// 正規表現で置換する文字列をすべて抽出
string pattern = @"\[\[\[(.*?)\]\]\]";
var matches = Regex.Matches(mainTemplateContent, pattern);
var uniqueMatches = matches.Cast<Match>()
.Select(m => new { Full = m.Value, Captured = m.Groups[1].Value })
.Distinct()
.ToList();
var replacementContents = new Dictionary<string, string>();
Console.WriteLine("--- コンポーネントの置換を開始 ---");
foreach (var match in uniqueMatches)
{
string instanceName = match.Captured;
// JSONマッピングからコンポーネント名を取得
if (!componentMapping.TryGetValue(instanceName, out string componentName))
{
Console.WriteLine($"警告: マッピングファイルにインスタンス名 '{instanceName}' の定義がありません。スキップします。");
replacementContents[match.Full] = string.Empty;
continue;
}
string csvFilePath = Path.Combine(projectFolderPath, $"{instanceName}.csv");
string componentFilePath = Path.Combine(componentBaseDir, $"{componentName}.txt");
string componentStartFilePath = Path.Combine(componentBaseDir, $"{componentName}_start.txt");
string componentEndFilePath = Path.Combine(componentBaseDir, $"{componentName}_end.txt");
if (!File.Exists(componentFilePath))
{
Console.WriteLine($"警告: コンポーネントファイル '{componentName}.txt' が見つかりません。スキップします。");
replacementContents[match.Full] = string.Empty;
continue;
}
var dataList = ReadCsv(csvFilePath);
string componentTemplate = File.ReadAllText(componentFilePath, Encoding.UTF8);
string componentStart = File.Exists(componentStartFilePath) ? File.ReadAllText(componentStartFilePath, Encoding.UTF8) : string.Empty;
string componentEnd = File.Exists(componentEndFilePath) ? File.ReadAllText(componentEndFilePath, Encoding.UTF8) : string.Empty;
var currentComponentParts = new List<string>();
foreach (var data in dataList)
{
string replacedPart = ReplacePlaceholders(componentTemplate, data);
currentComponentParts.Add(replacedPart);
}
// start_csvとend_csvからデータを読み込み、置換処理を行う
string startCsvFilePath = Path.Combine(projectFolderPath, $"{instanceName}_start.csv");
string endCsvFilePath = Path.Combine(projectFolderPath, $"{instanceName}_end.csv");
// start.txt の置換
if (!string.IsNullOrEmpty(componentStart))
{
var startDataList = ReadCsv(startCsvFilePath);
if (startDataList.Count > 0)
{
componentStart = ReplacePlaceholders(componentStart, startDataList[0]);
}
}
// end.txt の置換
if (!string.IsNullOrEmpty(componentEnd))
{
var endDataList = ReadCsv(endCsvFilePath);
if (endDataList.Count > 0)
{
componentEnd = ReplacePlaceholders(componentEnd, endDataList[0]);
}
}
string finalComponentContent = string.Join(Environment.NewLine, currentComponentParts);
replacementContents[match.Full] = componentStart + finalComponentContent + componentEnd;
}
// 元のテンプレートの置換処理
string finalDocument = mainTemplateContent;
foreach (var pair in replacementContents)
{
finalDocument = finalDocument.Replace(pair.Key, pair.Value);
}
// 出力ファイルをプロジェクトフォルダに作成
string outputFileName = Path.Combine(projectFolderPath, "01build_document.html");
File.WriteAllText(outputFileName, finalDocument, Encoding.UTF8);
Console.WriteLine($"\n[ OK ] ドキュメントが正常に作成されました: {outputFileName}");
}
/// <summary>
/// CSVファイルを読み込み、ヘッダーと行データをDictionaryのリストとして返す
/// ファイルが存在しない、またはヘッダーしかない場合は空のリストを返す
/// </summary>
static List<Dictionary<string, string>> ReadCsv(string filePath)
{
var result = new List<Dictionary<string, string>>();
if (!File.Exists(filePath))
{
Console.WriteLine($"警告: データファイル '{filePath}' が見つかりません。処理をスキップします。");
return result;
}
string[] lines = File.ReadAllLines(filePath, Encoding.UTF8);
if (lines.Length <= 1)
{
Console.WriteLine($"情報: データファイル '{filePath}' にはヘッダーのみ、またはデータがありません。");
return result;
}
var headers = lines[0].Split(',').Select(h => h.Trim()).ToArray();
foreach (var line in lines.Skip(1))
{
var values = line.Split(',').Select(v => v.Trim()).ToArray();
if (values.Length != headers.Length)
{
Console.WriteLine($"警告: '{filePath}' の行データがヘッダーと一致しません。スキップします。");
continue;
}
var data = new Dictionary<string, string>();
for (int i = 0; i < headers.Length; i++)
{
data[headers[i]] = values[i];
}
result.Add(data);
}
return result;
}
/// <summary>
/// テンプレート内のプレースホルダーをDictionaryのデータで置換する
/// </summary>
static string ReplacePlaceholders(string template, Dictionary<string, string> data)
{
string result = template;
foreach (var pair in data)
{
result = result.Replace($"[replace]{pair.Key}", pair.Value);
}
return result;
}
}
応答
以下は、プログラム内の関数とその相互呼び出し関係をフォルダーとファイルに似た形式で整理したものです。分かりやすくするために必要な階層のみを残しました。
Program.cs
├── Main()
│ ├─ 検証: 関数の存在確認 (args.Length == 0)
│ ├─ 検証: プロジェクトフォルダの存在確認 (Directory.Exists)
│ ├─ 検証: main_template.txtの存在確認 (File.Exists)
│ ├─ 検証: component_mapping.jsonの存在確認 (File.Exists)
│ └─ ProcessDocument()呼び出し
├── ProcessDocument()
│ ├── ReadCsv()呼び出し
│ └─ ReplacePlaceholders()呼び出し
├── ReadCsv()
│ └─ なし
└── ReplacePlaceholders()
└─ なし
説明:
Main(): プログラムの起点となる関数です。引数の検証、ファイルの存在確認、およびProcessDocument()への呼び出しを行います。
ProcessDocument(): 主な処理を行う関数で、テンプレート読み込み、置換処理、ファイル出力などを行います。この中にて、ReadCsv()とReplacePlaceholders()を呼び出します。
ReadCsv(): CSVファイルの読み込みとデータ加工を行う関数です。
ReplacePlaceholders(): テンプレート内のPlaceholder置換を行う関数です。
以上が主な関係構造になります。
Discussion