Skip to content

Commit 37c79d7

Browse files
committed
Исправление падений, улучшение логики
1 parent e95c99a commit 37c79d7

7 files changed

Lines changed: 170 additions & 106 deletions

File tree

CaseCreate.cs

Lines changed: 85 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Cyriller;
22
using Cyriller.Model;
3+
using System.IO;
34
using System.Xml.Linq;
45

56
namespace RimLangKit
@@ -9,39 +10,70 @@ internal static class CaseCreate
910
private static readonly CyrNounCollection cyrNounCollection = new();
1011
private static readonly CyrAdjectiveCollection cyrAdjectiveCollection = new();
1112
private static readonly CyrPhrase cyrPhrase = new(cyrNounCollection, cyrAdjectiveCollection);
13+
private const string CasePath = "Languages\\Russian\\WordInfo";
1214

1315
private static string[] Declension(string word)
1416
{
15-
CyrNoun nouns = cyrNounCollection.Get(word, out string foundWord, out CasesEnum @case, out NumbersEnum number);
16-
string[] result = nouns.Decline().ToArray();
17-
result[0] = word;
18-
19-
string[] resultP = nouns.DeclinePlural().ToArray();
20-
string[] attributes = { resultP[0], nouns.Gender.ToString() };
21-
22-
return result;
17+
// try-catch - неэффективно, но слово может совсем не найтись. Вот бы просто возвращало что-то вроде -1
18+
try
19+
{
20+
CyrNoun nouns = cyrNounCollection.Get(word, out string foundWord, out CasesEnum @case, out NumbersEnum number);
21+
string[] result = nouns.Decline().ToArray();
22+
result[0] = word;
23+
return result;
24+
}
25+
catch
26+
{
27+
string[] result = { word, word, word, word, word, word };
28+
return result;
29+
}
2330
}
2431
private static string[] DeclensionComposite(string word)
2532
{
26-
CyrResult nouns = cyrPhrase.Decline(word, GetConditionsEnum.Similar);
27-
string[] result = nouns.ToArray();
28-
result[0] = word;
29-
return result;
33+
try
34+
{
35+
CyrResult nouns = cyrPhrase.Decline(word, GetConditionsEnum.Similar);
36+
string[] result = nouns.ToArray();
37+
result[0] = word;
38+
return result;
39+
}
40+
catch
41+
{
42+
string[] result = { word, word, word, word, word, word };
43+
return result;
44+
}
3045
}
3146

47+
// Возвращает форму множественного числа и пол под позициями 0 и 1 соотвественно
3248
private static string[] Attributes(string word)
3349
{
34-
CyrNoun nouns = cyrNounCollection.Get(word, out string foundWord, out CasesEnum @case, out NumbersEnum number);
35-
string[] result = nouns.DeclinePlural().ToArray();
36-
string[] attributes = { result[0], nouns.Gender.ToString() };
37-
return attributes;
50+
try
51+
{
52+
CyrNoun nouns = cyrNounCollection.Get(word, out string foundWord, out CasesEnum @case, out NumbersEnum number);
53+
string[] result = nouns.DeclinePlural().ToArray();
54+
string[] attributes = { result[0], nouns.Gender.ToString() };
55+
return attributes;
56+
}
57+
catch
58+
{
59+
string[] attributes = { word, "Undefined" };
60+
return attributes;
61+
}
3862
}
3963
private static string[] AttributesComposite(string word)
4064
{
41-
CyrResult nouns = cyrPhrase.DeclinePlural(word, GetConditionsEnum.Similar);
42-
string[] result = nouns.ToArray();
43-
string[] attributes = { result[0], "Composite" };
44-
return attributes;
65+
try
66+
{
67+
CyrResult nouns = cyrPhrase.DeclinePlural(word, GetConditionsEnum.Similar);
68+
string[] result = nouns.ToArray();
69+
string[] attributes = { result[0], "Composite" };
70+
return attributes;
71+
}
72+
catch
73+
{
74+
string[] attributes = { word, "Undefined" };
75+
return attributes;
76+
}
4577
}
4678

4779
private static List<string> WordsExtract(string file)
@@ -59,21 +91,24 @@ private static List<string> WordsExtract(string file)
5991
return words;
6092
}
6193

62-
internal static List<string> FindWordsProcessing(string CurrentFile, string DefType)
94+
internal static List<string> FindWordsProcessing(string currentFile, string defType)
6395
{
6496
List<string> words = new();
65-
if (CurrentFile.Contains(DefType, StringComparison.OrdinalIgnoreCase))
97+
// Если файл соответствует заданному defType, то запускатся в работу
98+
if (currentFile.Contains(defType, StringComparison.OrdinalIgnoreCase))
6699
{
67-
words.AddRange(WordsExtract(CurrentFile));
100+
words.AddRange(WordsExtract(currentFile));
68101
}
69102
return words;
70103
}
71104

72-
internal static void CreateCase(string DirectoryPath, List<string> words, string DefType)
105+
internal static void CreateCase(string directoryPath, List<string> words, string defType)
73106
{
74-
string pathCase = DirectoryPath + "\\Languages\\Russian\\WordInfo\\Case.txt";
75-
StreamWriter writerCase = new(pathCase, true, System.Text.Encoding.UTF8);
76-
writerCase.WriteLine(Environment.NewLine + "// " + DefType);
107+
string path = directoryPath + CasePath;
108+
Directory.CreateDirectory(path); // Созданиие директории, которая наверняка отсутствует
109+
path += "\\Case.txt";
110+
StreamWriter writerCase = new(path, true, System.Text.Encoding.UTF8);
111+
writerCase.WriteLine("// " + defType);
77112

78113
foreach (string word in words)
79114
{
@@ -95,27 +130,34 @@ internal static void CreateCase(string DirectoryPath, List<string> words, string
95130
}
96131
writerCase.WriteLine(tempStringCase[..^2]);
97132
}
133+
writerCase.WriteLine();
98134
writerCase.Close();
99135
}
100136

101-
internal static void CreateGender(string DirectoryPath, List<string> words, string DefType)
137+
internal static void CreateGender(string directoryPath, List<string> words, string defType)
102138
{
103-
string pathPlural = DirectoryPath + "\\Languages\\Russian\\WordInfo\\Plural.txt";
139+
string pathPlural = directoryPath + CasePath + "\\Plural.txt";
104140
StreamWriter writerPlural = new(pathPlural, true, System.Text.Encoding.UTF8);
105-
writerPlural.WriteLine(Environment.NewLine + "// " + DefType);
141+
writerPlural.WriteLine("// " + defType);
142+
143+
string pathGender = directoryPath + CasePath + "\\Gender";
144+
Directory.CreateDirectory(pathGender);
106145

107-
string pathGenderFemale = DirectoryPath + "\\Languages\\Russian\\WordInfo\\Gender\\Female.txt";
146+
string pathGenderFemale = pathGender + "\\Female.txt";
108147
StreamWriter writerFemale = new(pathGenderFemale, true, System.Text.Encoding.UTF8);
109-
writerFemale.WriteLine(Environment.NewLine + "// " + DefType);
110-
string pathGenderMale = DirectoryPath + "\\Languages\\Russian\\WordInfo\\Gender\\Male.txt";
148+
writerFemale.WriteLine("// " + defType);
149+
150+
string pathGenderMale = pathGender + "\\Male.txt";
111151
StreamWriter writerMale = new(pathGenderMale, true, System.Text.Encoding.UTF8);
112-
writerMale.WriteLine(Environment.NewLine + "// " + DefType);
113-
string pathGenderNeuter = DirectoryPath + "\\Languages\\Russian\\WordInfo\\Gender\\Neuter.txt";
152+
writerMale.WriteLine("// " + defType);
153+
154+
string pathGenderNeuter = pathGender + "\\Neuter.txt";
114155
StreamWriter writerNeuter = new(pathGenderNeuter, true, System.Text.Encoding.UTF8);
115-
writerNeuter.WriteLine(Environment.NewLine + "// " + DefType);
116-
string pathGenderUndefined = DirectoryPath + "\\Languages\\Russian\\WordInfo\\Gender\\Undefined.txt";
156+
writerNeuter.WriteLine("// " + defType);
157+
158+
string pathGenderUndefined = pathGender + "\\Undefined.txt";
117159
StreamWriter writerUndefined = new(pathGenderUndefined, true, System.Text.Encoding.UTF8);
118-
writerUndefined.WriteLine(Environment.NewLine + "// " + DefType);
160+
writerUndefined.WriteLine("// " + defType);
119161

120162
foreach (string word in words)
121163
{
@@ -152,10 +194,15 @@ internal static void CreateGender(string DirectoryPath, List<string> words, stri
152194
break;
153195
}
154196
}
197+
writerPlural.WriteLine();
155198
writerPlural.Close();
199+
writerFemale.WriteLine();
156200
writerFemale.Close();
201+
writerMale.WriteLine();
157202
writerMale.Close();
203+
writerNeuter.WriteLine();
158204
writerNeuter.Close();
205+
writerUndefined.WriteLine();
159206
writerUndefined.Close();
160207
}
161208
}

CommentInsert.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,32 @@ namespace RimLangKit
44
{
55
internal static class CommentInsert
66
{
7-
internal static (bool, string) CommentsInsertProcessing(string CurrentFile)
7+
internal static (bool, string) CommentsInsertProcessing(string currentFile)
88
{
99
string error = string.Empty;
1010
//Позволяет избежать падения при загрузке сломанного .xml файла
1111
try
1212
{
13-
XDocument.Load(CurrentFile, LoadOptions.PreserveWhitespace);
13+
XDocument.Load(currentFile, LoadOptions.PreserveWhitespace);
1414
}
1515
catch
1616
{
17-
error = "Не удалось загрузить файл " + CurrentFile;
17+
error = "Не удалось загрузить файл " + currentFile;
1818
return (false, error);
1919
}
2020

21-
XDocument xDoc = XDocument.Load(CurrentFile, LoadOptions.PreserveWhitespace);
21+
XDocument xDoc = XDocument.Load(currentFile, LoadOptions.PreserveWhitespace);
2222
//Позволяет избежать обработки пустого или не содержащего нужных тегов файла
2323
if (xDoc.Element("LanguageData") is null)
2424
{
25-
error = "Не удалось найти LanguageData " + CurrentFile;
25+
error = "Не удалось найти LanguageData " + currentFile;
2626
return (false, error);
2727
}
2828
//Перевод контекста в содержимое тега LanguageData
2929
XElement? root = xDoc.Element("LanguageData");
3030
if (root?.Elements() is null)
3131
{
32-
error = "Тег LanguageData пуст " + CurrentFile;
32+
error = "Тег LanguageData пуст " + currentFile;
3333
return (false, error);
3434
}
3535

@@ -46,7 +46,7 @@ internal static (bool, string) CommentsInsertProcessing(string CurrentFile)
4646
root.LastNode?.AddAfterSelf("\n");
4747

4848
//Сохранение файла
49-
xDoc.Save(CurrentFile);
49+
xDoc.Save(currentFile);
5050
return (true, error);
5151
}
5252
}

0 commit comments

Comments
 (0)