Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 22 additions & 23 deletions CountVectorizer.py → CountVectorizer_hw.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from typing import List


class CountVectorizer:
"""
Класс CountVectorizer используется для разбивки текста на слова.
Expand All @@ -14,47 +11,49 @@ class CountVectorizer:
----------
uniquewords : list
список из уникальных слов в предложенном массиве
wordcount : list
wordcount : list[list]
массив из списков с количеством появлений каждого слова

Methods
-------
fit_transform(array_of_strings)
Выводит список уникальных слов из предложенного массива
Выводит массив из документов с количеством появлений каждого слова в документе
get_feature_names()
Выводит массив из списков с количеством появлений каждого слова
Выводит список уникальных слов из предложенного массива
"""

def __init__(self):
self.uniquewords = []
self.wordcount = []

def fit_transform(self, array_of_strings: List[str]) -> List[int]:
def fit_transform(self, array_of_strings: list[str]) -> list[list[int]]:
"""
Считывает массив из строк и выводит терм-документную матрицу для каждой строки из массива
Считывает массив из строк и выводит терм-документный вектор
для каждой строки из массив. На выходе получается терм-документная матрица.

Parameters
----------
array_of_strings : list
массив строк
"""
uniquewords = []
for j in array_of_strings:
for i in j.lower().split():
if i not in uniquewords:
uniquewords.append(i)
self.uniquewords = uniquewords
wordcount = []
for j in array_of_strings:
result = {}
for i in uniquewords:
result[i] = j.lower().count(i)
wordcount.append(list(result.values()))
self.wordcount = wordcount
for string in array_of_strings:
for word in string.lower().split():
if word not in self.uniquewords:
self.uniquewords.append(word)

for string in array_of_strings:
result = []
for word in self.uniquewords:
result.append(string.lower().count(word))
self.wordcount.append(result)

return self.wordcount

def get_feature_names(self) -> List[str]:
"""Выводит массив из списков с количеством появлений каждого слова"""
def get_feature_names(self) -> list[str]:
"""
Выводит массив уникальных слов,
встречающихся хотя бы в одном документе из предложенного массива
"""
return self.uniquewords


Expand Down
144 changes: 0 additions & 144 deletions report.py
Original file line number Diff line number Diff line change
@@ -1,145 +1 @@
import csv
from collections import defaultdict


def open_csv_file(input_path: str) -> list:
"""
The function reads a csv file and saves it in the form of a list consisting of lists
:param input_path: The path to csv file we are getting information from
:type: str
:return: list
"""
try:
with open(input_path, encoding='utf-8', newline='') as csvfile:
reader = csv.reader(csvfile, delimiter=';')
data = list(reader)
return data[1::]
except FileNotFoundError:
return 'Ссылка на файл не корректна. Попробуйте еще раз!'


def command_hierarchy(data: list) -> None:
"""
The function gets a list with the profiles of each employee in the company
and displays the department and the branches that they include
:param data: The list of the profiles of each employee
:type: list
:return: None
"""
departments_branches = defaultdict(set)
for lst in data:
departments_branches[lst[1]].add(lst[2])
print("Иерархия команд:")
print()
for department, branches in departments_branches.items():
print(f"Название департамента:\n{department}")
print("Отделы, входящие в этот департамент: ", *branches, sep='\n')
print()


def total_report(data: list) -> list:
"""
The function gets a list with the profiles of each employee in the company
and displays the report on each department
:param data: The list of the profiles of each employee
:type: list
:return: list
"""
salary = defaultdict(list)
for lst in data:
salary[lst[1]].append(int(lst[-1]))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тут нужно заменить на float судя по всему. Иначе падает на некоторых строках

report = []
for key, value in salary.items():
line = []
line.append(key)
line.append(len(value))
line.append(min(value))
line.append(max(value))
line.append(sum(value) // len(value))
report.append(line)
return report


def summary_report(report: list) -> None:
"""
The function gets a list with the report on each department and displays it
:param report: The list the report on each department
:type: list
:return: None
"""
print('Сводный отчёт по департаментам:')
for lst in report:
print(f'Департамент: {lst[0]}')
print(f'Численность: {lst[1]}')
print('"Вилка" зарплат:')
print(f'Минимальная зарплата: {lst[2]}')
print(f'Максимальная зарплата: {lst[3]}')
print(f'Средняя зарплата: {lst[4]}')
print('-' * 30)


def close_csv_file(report: list, output_path: str) -> None:
"""
The function gets the list with the summary report on each department
and writes it to a csv file
:param report: The list the report on each department
:type: list
:param output_path: The path to csv file we are writing information in
:type: str
:return: None
"""
columns = ['Департамент', 'Численность', 'Минимальная зарплата',
'Максимальная зарплата', 'Средняя зарплата']
with open(output_path, 'w', encoding='utf-8', newline='') as csvfile:
writer = csv.writer(csvfile, delimiter=';')
writer.writerow(columns)
writer.writerows(report)
print('Ваш отчет сохранен.')


def main() -> None:
"""
The function gives several options to choose from and performs the specified actions set in them
:return: None
"""
options = {0: '0. Выход',
1: '1. Вывести иерархию команд',
2: '2. Вывести сводный отчёт по департаментам',
3: '3. Сохранить сводный отчёт в виде csv-файла'}
print('Добрый день!', *options.values(), sep='\n')
var = ''
if var == 0:
return None
while var != 0:
var = int(input('Выберете опцию(0/1/2/3): '))
while var not in options:
try:
var = int(input('Выберете опцию(0/1/2/3): '))
except ValueError:
print('Введите цифру из предложенных.')
if var == 1:
data = input('Введите название csv файла, с которым будем работать: ')
while not data[-4:] == '.csv':
print('Название файла введено неверно.')
data = input('Введите название csv файла, с которым будем работать: ')
command_hierarchy(open_csv_file(data))
elif var == 2:
data = input('Введите название csv файла, с которым будем работать: ')
while not data[-4:] == '.csv':
print('Название файла введено неверно.')
data = input('Введите название csv файла, с которым будем работать: ')
summary_report(total_report(open_csv_file(data)))
elif var == 3:
data = input('Введите название csv файла, с которым будем работать: ')
while not data[-4:] == '.csv':
print('Название файла введено неверно.')
data = input('Введите название csv файла, с которым будем работать: ')
output_path = input('Введите название csv файла в который вы хотите сохранить сводный отчет: ')
while not output_path[-4:] == '.csv':
print('Название файла введено неверно.')
output_path = input('Введите название csv файла в который вы хотите сохранить сводный отчет: ')
close_csv_file(total_report(open_csv_file(data)), output_path)


if __name__ == '__main__':
main()