Код на C#
public static string Lemmatization(string inputText)
{
Log.Logger = new LoggerConfiguration()
.WriteTo.File("log.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();
string filePath = "text_for_lemm.txt";
try
{
WriteTextToFile(inputText, filePath);
ExecutePythonScript();
string lemmatizedText = ReadTextFromFile(filePath);
return lemmatizedText;
}
catch (FileNotFoundException)
{
Log.Error($"Файл {filePath} не найден.");
return "";
}
catch (Exception ex)
{
Log.Error($"Произошла ошибка: {ex.Message}");
return "";
}
finally
{
Log.CloseAndFlush();
}
}
static void ExecutePythonScript()
{
var engine = Python.CreateEngine();
ICollection<string> paths = engine.GetSearchPaths();
paths.Add(@"C:\Users\Брест\AppData\Local\Programs\Python\Python39\Lib\site-packages");
engine.SetSearchPaths(paths);
var scope = engine.CreateScope();
engine.ExecuteFile("lemma_for_analyse.py", scope);
}
Код на python
import pymorphy2
import regex
file_path = "E:\\ToneAnalyseInterface\\ToneAnalyseInterface\\bin\\Debug\\net8.0-windows\\materials\\text_for_lemm.txt"
morph = pymorphy2.MorphAnalyzer()
with open(file_path, 'r', encoding='utf-8') as file:
words = file.read().split()
lemmas = [morph.parse(word)[0].normal_form for word in words]
with open(file_path, 'w', encoding='utf-8') as file:
for lemma in lemmas:
file.write(lemma + '\n')
regex я установила