# Path: poetry/apps/corpus/models.py
# class Poem(Model):
#     text = TextField("Текст", blank=False)
#     name = CharField("Нвзвание стихотворения", max_length=50, blank=True)
#     author = CharField("Автор", max_length=50, blank=False)
#     date_from = IntegerField("Дата написания - первый год", blank=True, null=True)
#     date_to = IntegerField("Дата написания - второй год", blank=True, null=True)
#     themes = ManyToManyField(Theme, verbose_name="Темы", related_name="poems", blank=True)
#     is_restricted = BooleanField("Стихи с ограниченным доступом", default=False)
#     is_standard = BooleanField("Эталонные (проверенные) стихи", default=False)
# 
#     class Meta:
#         verbose_name = "Стихотворение"
#         verbose_name_plural = "Стихотворения"
#         permissions = (
#             ("can_view_restricted_poems", "Can view restricted poems"),
#         )
# 
#     def __str__(self):
#         return 'Стихотворение: ' + self.get_name() + ", " + str(self.author)
# 
#     def get_name(self):
#         name = self.name
#         if name == "":
#             name = self.text.strip().split("\n")[0]
#             i = len(name) - 1
#             while i > 0 and not name[i].isalpha():
#                 i -= 1
#             name = name[:i+1]
#         return name
# 
#     def get_name_short(self):
#         name = self.__clean_name(self.name)
#         if name == "":
#             name = self.__get_name_form_text()
#         return name[:64].replace(" ", "")
# 
#     def __get_name_form_text(self):
#         name = ""
#         line_number = 0
#         while name == "":
#             name = self.text.strip().split("\n")[line_number]
#             i = len(name) - 1
#             while i > 0 and not name[i].isalpha():
#                 i -= 1
#             name = name[:i+1]
#             name = self.__clean_name(name)
#             line_number += 1
#         return name
# 
#     def __clean_name(self, name):
#         new_name = ""
#         for ch in name:
#             if ch.isalpha() or ch.isalnum() or ch == " ":
#                 new_name += ch
#         return new_name.strip()
# 
#     def count_lines(self):
#         return len(self.text.rstrip().split("\n"))
# 
#     def count_automatic_errors(self):
#         for markup in self.markups.all():
#             if "Automatic" in markup.author:
#                 return markup.get_automatic_additional().get_metre_errors_count()
# 
#     def get_absolute_url(self):
#         if len(self.markups.all()) != 0:
#             return self.markups.all()[0].get_absolute_url()
#         return reverse("corpus:poems")
# 
#     def get_automatic_markup(self):
#         for markup in self.markups.all():
#             if "Automatic" in markup.author:
#                 return markup
#         return None
# 
#     def count_manual_markups(self):
#         return sum([int(markup.markup_version.name == "Manual") for markup in self.markups.all()])
# 
# Path: poetry/settings.py
# BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


import os
from django.core.management.base import BaseCommand
from poetry.apps.corpus.models import Poem
from poetry.settings import BASE_DIR



class Command(BaseCommand):
    def handle(self, *args, **options):
        poems = Poem.objects.all()
        directory = os.path.join(BASE_DIR, "datasets", "corpus", "all_raw")
        if os.path.exists(directory):
            for path in os.listdir(directory):
                os.remove(os.path.join(directory, path))
        else:
            os.mkdir(directory)
        for i, poem in enumerate(poems):
            print(i, poem.name)
            try:
                filename = poem.author.replace(" ","")+poem.get_name_short()+".txt"

# Path: poetry/apps/corpus/models.py
# class Poem(Model):
#     text = TextField("Текст", blank=False)
#     name = CharField("Нвзвание стихотворения", max_length=50, blank=True)
#     author = CharField("Автор", max_length=50, blank=False)
#     date_from = IntegerField("Дата написания - первый год", blank=True, null=True)
#     date_to = IntegerField("Дата написания - второй год", blank=True, null=True)
#     themes = ManyToManyField(Theme, verbose_name="Темы", related_name="poems", blank=True)
#     is_restricted = BooleanField("Стихи с ограниченным доступом", default=False)
#     is_standard = BooleanField("Эталонные (проверенные) стихи", default=False)
# 
#     class Meta:
#         verbose_name = "Стихотворение"
#         verbose_name_plural = "Стихотворения"
#         permissions = (
#             ("can_view_restricted_poems", "Can view restricted poems"),
#         )
# 
#     def __str__(self):
#         return 'Стихотворение: ' + self.get_name() + ", " + str(self.author)
# 
#     def get_name(self):
#         name = self.name
#         if name == "":
#             name = self.text.strip().split("\n")[0]
#             i = len(name) - 1
#             while i > 0 and not name[i].isalpha():
#                 i -= 1
#             name = name[:i+1]
#         return name
# 
#     def get_name_short(self):
#         name = self.__clean_name(self.name)
#         if name == "":
#             name = self.__get_name_form_text()
#         return name[:64].replace(" ", "")
# 
#     def __get_name_form_text(self):
#         name = ""
#         line_number = 0
#         while name == "":
#             name = self.text.strip().split("\n")[line_number]
#             i = len(name) - 1
#             while i > 0 and not name[i].isalpha():
#                 i -= 1
#             name = name[:i+1]
#             name = self.__clean_name(name)
#             line_number += 1
#         return name
# 
#     def __clean_name(self, name):
#         new_name = ""
#         for ch in name:
#             if ch.isalpha() or ch.isalnum() or ch == " ":
#                 new_name += ch
#         return new_name.strip()
# 
#     def count_lines(self):
#         return len(self.text.rstrip().split("\n"))
# 
#     def count_automatic_errors(self):
#         for markup in self.markups.all():
#             if "Automatic" in markup.author:
#                 return markup.get_automatic_additional().get_metre_errors_count()
# 
#     def get_absolute_url(self):
#         if len(self.markups.all()) != 0:
#             return self.markups.all()[0].get_absolute_url()
#         return reverse("corpus:poems")
# 
#     def get_automatic_markup(self):
#         for markup in self.markups.all():
#             if "Automatic" in markup.author:
#                 return markup
#         return None
# 
#     def count_manual_markups(self):
#         return sum([int(markup.markup_version.name == "Manual") for markup in self.markups.all()])
# 
# Path: poetry/settings.py
# BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Path: poetry/apps/corpus/management/commands/generate_raw.py
import os
from django.core.management.base import BaseCommand
from poetry.apps.corpus.models import Poem
from poetry.settings import BASE_DIR




class Command(BaseCommand):
    def handle(self, *args, **options):
        poems = Poem.objects.all()
        directory = os.path.join(BASE_DIR, "datasets", "corpus", "all_raw")
        if os.path.exists(directory):
            for path in os.listdir(directory):
                os.remove(os.path.join(directory, path))
        else:
            os.mkdir(directory)
        for i, poem in enumerate(poems):
            print(i, poem.name)
            try:
                filename = poem.author.replace(" ","")+poem.get_name_short()+".txt"

                with open(os.path.join(directory, filename), 'w', encoding="utf-8") as f: