126 lines
No EOL
4.3 KiB
C#
126 lines
No EOL
4.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text.RegularExpressions;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
|
|
public class DictionaryProcessorWindow : EditorWindow
|
|
{
|
|
private int _filteredWordCount;
|
|
private int _maxWordLength = 6;
|
|
private int _minWordLength = 3;
|
|
private int _originalWordCount;
|
|
private string _outputFileName = "FilteredDictionary";
|
|
private string _previewText = "";
|
|
private Vector2 _scrollPosition;
|
|
private TextAsset _sourceFile;
|
|
|
|
private void OnGUI()
|
|
{
|
|
GUILayout.Label("Dictionary Processing Tool", EditorStyles.boldLabel);
|
|
|
|
EditorGUILayout.Space();
|
|
|
|
_sourceFile = (TextAsset)EditorGUILayout.ObjectField("Source Dictionary",
|
|
_sourceFile, typeof(TextAsset), false);
|
|
|
|
EditorGUILayout.Space();
|
|
|
|
GUILayout.Label("Filtering Options", EditorStyles.boldLabel);
|
|
_minWordLength = EditorGUILayout.IntSlider("Min Word Length", _minWordLength, 1, 10);
|
|
_maxWordLength = EditorGUILayout.IntSlider("Max Word Length", _maxWordLength, 1, 15);
|
|
|
|
EditorGUILayout.Space();
|
|
|
|
GUILayout.Label("Output Settings", EditorStyles.boldLabel);
|
|
_outputFileName = EditorGUILayout.TextField("Output File Name", _outputFileName);
|
|
|
|
EditorGUILayout.Space();
|
|
|
|
EditorGUILayout.BeginHorizontal();
|
|
if (GUILayout.Button("Preview")) PreviewFiltering();
|
|
|
|
GUI.enabled = _sourceFile;
|
|
if (GUILayout.Button("Process & Save")) ProcessAndSave();
|
|
GUI.enabled = true;
|
|
EditorGUILayout.EndHorizontal();
|
|
|
|
if (_originalWordCount > 0)
|
|
{
|
|
EditorGUILayout.Space();
|
|
EditorGUILayout.LabelField($"Original: {_originalWordCount:N0} words");
|
|
EditorGUILayout.LabelField(
|
|
$"Filtered: {_filteredWordCount:N0} words ({(float)_filteredWordCount / _originalWordCount * 100:F1}% kept)");
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(_previewText))
|
|
{
|
|
EditorGUILayout.Space();
|
|
GUILayout.Label("Preview (first 50 words):", EditorStyles.boldLabel);
|
|
_scrollPosition = EditorGUILayout.BeginScrollView(_scrollPosition, GUILayout.Height(200));
|
|
EditorGUILayout.TextArea(_previewText, GUILayout.ExpandHeight(true));
|
|
EditorGUILayout.EndScrollView();
|
|
}
|
|
}
|
|
|
|
[MenuItem("Tools/Dictionary Processor")]
|
|
public static void ShowWindow()
|
|
{
|
|
GetWindow<DictionaryProcessorWindow>("Dictionary Processor");
|
|
}
|
|
|
|
private void PreviewFiltering()
|
|
{
|
|
if (!_sourceFile) return;
|
|
|
|
var filteredWords = ProcessWords(_sourceFile.text);
|
|
_originalWordCount = _sourceFile.text.Split('\n').Length;
|
|
_filteredWordCount = filteredWords.Count;
|
|
|
|
_previewText = string.Join("\n", filteredWords.Take(50));
|
|
if (filteredWords.Count > 50)
|
|
_previewText += $"\n... and {filteredWords.Count - 50} more words";
|
|
}
|
|
|
|
private void ProcessAndSave()
|
|
{
|
|
if (!_sourceFile) return;
|
|
|
|
var filteredWords = ProcessWords(_sourceFile.text);
|
|
|
|
var outputPath = Path.Combine(Application.dataPath, "StreamingAssets", $"{_outputFileName}.txt");
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(outputPath) ?? throw new NullReferenceException("Invalid path"));
|
|
|
|
File.WriteAllText(outputPath, string.Join("\n", filteredWords));
|
|
|
|
AssetDatabase.Refresh();
|
|
|
|
Debug.Log($"Dictionary processed! {filteredWords.Count} words saved to: {outputPath}");
|
|
|
|
_originalWordCount = _sourceFile.text.Split('\n').Length;
|
|
_filteredWordCount = filteredWords.Count;
|
|
}
|
|
|
|
private List<string> ProcessWords(string text)
|
|
{
|
|
var words = text.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
|
|
var filteredWords = new List<string>();
|
|
|
|
foreach (var rawWord in words)
|
|
{
|
|
var word = rawWord.Trim().ToLowerInvariant();
|
|
|
|
if (word.Length < _minWordLength || word.Length > _maxWordLength)
|
|
continue;
|
|
|
|
if (!Regex.IsMatch(word, @"^[a-z]+$"))
|
|
continue;
|
|
|
|
filteredWords.Add(word);
|
|
}
|
|
return filteredWords.Distinct().OrderBy(w => w).ToList();
|
|
}
|
|
} |