From 092486052ce5fc2785a440c9d09153569842b873 Mon Sep 17 00:00:00 2001 From: maksim Date: Thu, 6 Jun 2024 22:49:43 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A4=D1=83=D1=85,=20=D0=B4=D0=BE=D0=B1=D0=B0?= =?UTF-8?q?=D0=B2=D0=B8=D0=BB=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20=D0=BF?= =?UTF-8?q?=D0=BE=20=D0=B0=D1=8D=D1=80=D0=BE=D0=BF=D1=80=D1=82=D0=B0=D0=BC?= =?UTF-8?q?,=20=D0=BF=D0=BB=D1=8E=D1=81=20=D1=80=D0=BE=D1=83=D1=82=D0=B5?= =?UTF-8?q?=D1=80=D1=8B=20=D0=B8=20=D0=BF=D1=80=D0=BE=D1=87=D0=B5=D0=B5=20?= =?UTF-8?q?=D0=B8=20=D0=BF=D1=80=D0=BE=D1=87=D0=B5=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 3 ++ neural_network/dataset/sortTest.py | 51 ---------------------------- router_class.py | 31 +---------------- router_flight.py | 31 +++++++++++++++++ router_questions.py | 6 ++-- sity/airports.json | 51 ++++++++++++++++++++++++++++ sity/conversion.py | 53 ++++++++++++++++++++++++++++++ 7 files changed, 142 insertions(+), 84 deletions(-) delete mode 100644 neural_network/dataset/sortTest.py create mode 100644 router_flight.py create mode 100644 sity/airports.json create mode 100644 sity/conversion.py diff --git a/main.py b/main.py index 1f134a5..1886409 100644 --- a/main.py +++ b/main.py @@ -5,6 +5,7 @@ from contextlib import asynccontextmanager from database import create_tables, delete_tables from router_questions import router as questions_router from router_class import router as class_router +from router_flight import router as flight_router # Настройка логирования logging.basicConfig(level=logging.INFO) @@ -22,3 +23,5 @@ async def lifespan(app: FastAPI): app = FastAPI(lifespan=lifespan) app.include_router(questions_router) app.include_router(class_router) +app.include_router(flight_router) + diff --git a/neural_network/dataset/sortTest.py b/neural_network/dataset/sortTest.py deleted file mode 100644 index d312471..0000000 --- a/neural_network/dataset/sortTest.py +++ /dev/null @@ -1,51 +0,0 @@ -import json -import pandas as pd - -# JSON данные -json_data = ''' -[ - { "value": "AAQ", "label": "Анапа, Витязево, AAQ", "timezone": "+3", "latitude": 45.0029, "longitude": 37.3473 }, - { "value": "ARH", "label": "Архангельск, Талаги, ARH", "timezone": "+3", "latitude": 64.6003, "longitude": 40.7168 }, - { "value": "ASF", "label": "Астрахань, им. Б.Н. Кустодиева, ASF", "timezone": "+4", "latitude": 46.2833, "longitude": 48.0063 }, - { "value": "BAX", "label": "Барнаул, Михайловка, BAX", "timezone": "+7", "latitude": 53.3638, "longitude": 83.5385 } -] -''' - -data = json.loads(json_data) - -# Извлекаем первое слово перед запятой и первое слово после запятой -cities_set = set() -for entry in data: - parts = entry['label'].split(',') - if len(parts) > 1: - city1 = parts[0].strip().split()[0] if parts[0].strip().split() else '' - city2 = parts[1].strip().split()[0] if parts[1].strip().split() else '' - cities_set.update([city1, city2]) - -# Прочитаем CSV файл -csv_file_path = 'geo-reviews-dataset-2023.csv' -df = pd.read_csv(csv_file_path) - -# Функция для проверки совпадения города в адресе -def find_city(address, cities_set): - parts = address.split(',') - if len(parts) > 1: - word1 = parts[0].strip().split()[0] if parts[0].strip().split() else '' - word2 = parts[1].strip().split()[0] if parts[1].strip().split() else '' - if word1 in cities_set: - return word1 - elif word2 in cities_set: - return word2 - return None - -# Добавим новый столбец на основе первого слова из адреса -df['city'] = df['address'].apply(lambda x: find_city(x, cities_set)) - -# Оставим только те строки, где город из CSV файла совпадает с городом из JSON -df_filtered = df[df['city'].notnull()] - -# Сохраним отсортированный DataFrame обратно в CSV -output_file_path = 'sorted_filtered_geo-reviews-dataset-2023.csv' -df_filtered.to_csv(output_file_path, index=False) - -print(df_filtered[:15]) diff --git a/router_class.py b/router_class.py index 5d717b6..02d581d 100644 --- a/router_class.py +++ b/router_class.py @@ -1,41 +1,12 @@ -from typing import Annotated -from fastapi import APIRouter, Depends, HTTPException +from fastapi import APIRouter from typing import List -from pydantic import ValidationError - -from enums import TypeMood, TypeModel -from genetic_algorithm.genetic_algorithm import genetic_algorithm, load_graph_from_request -from repository import QuestionRepository -from schemas import SQuestionAdd, SQuestion, SQuestionId, Flight, TripRequest router = APIRouter( prefix="/class", tags=["Class"], ) - -@router.post("/get_flight/") -async def get_flight(request: TripRequest): - graphTo, graphBack, start_point, end_point, flightsDataTo, flightsDataBack, departure_date, departure_date_return = load_graph_from_request(request) - if request.returnDate: - resultTo = genetic_algorithm(start=start_point, end=end_point, graph=graphTo, flights_data=flightsDataTo, type = "to", departure_date = departure_date) - resulFrom = genetic_algorithm(end=start_point, start=end_point, graph=graphBack, flights_data=flightsDataBack, type = "back", departure_date = departure_date_return) - if not resultTo or not resulFrom: - raise HTTPException(status_code=404, detail="No valid paths found") - return resultTo, resulFrom - if not request.returnDate: - resultTo = genetic_algorithm(start=start_point, end=end_point, graph=graphTo, flights_data=flightsDataTo, - type="to", departure_date = departure_date) - resultTo.append({ - "back": [] - }) - - if not resultTo: - raise HTTPException(status_code=404, detail="No valid paths found") - return resultTo - - @router.get("/negative") async def get_class_names() -> List[str]: with open(".//neural_network/classification/class_names_negative.txt", "r", encoding="utf-8") as file: diff --git a/router_flight.py b/router_flight.py new file mode 100644 index 0000000..c74d6ab --- /dev/null +++ b/router_flight.py @@ -0,0 +1,31 @@ +from fastapi import APIRouter, HTTPException + +from genetic_algorithm.genetic_algorithm import genetic_algorithm, load_graph_from_request +from schemas import TripRequest + +router = APIRouter( + prefix="/flight", + tags=["Flight"], +) + + +@router.post("/get/") +async def get_flight(request: TripRequest): + graphTo, graphBack, start_point, end_point, flightsDataTo, flightsDataBack, departure_date, departure_date_return = load_graph_from_request(request) + if request.returnDate: + resultTo = genetic_algorithm(start=start_point, end=end_point, graph=graphTo, flights_data=flightsDataTo, type = "to", departure_date = departure_date) + resulFrom = genetic_algorithm(end=start_point, start=end_point, graph=graphBack, flights_data=flightsDataBack, type = "back", departure_date = departure_date_return) + if not resultTo or not resulFrom: + raise HTTPException(status_code=404, detail="No valid paths found") + return resultTo, resulFrom + if not request.returnDate: + resultTo = genetic_algorithm(start=start_point, end=end_point, graph=graphTo, flights_data=flightsDataTo, + type="to", departure_date = departure_date) + + if resultTo: + resultTo.append({ + "back": [] + }) + else: + raise HTTPException(status_code=404, detail="No valid paths found") + return resultTo diff --git a/router_questions.py b/router_questions.py index b49e9b0..abeb8df 100644 --- a/router_questions.py +++ b/router_questions.py @@ -11,7 +11,7 @@ router = APIRouter( tags=["Questions"], ) -@router.post("") +@router.post("/add") async def add_question( question: Annotated[SQuestionAdd, Depends()], type_mood: TypeMood, # Добавлен параметр type_mood @@ -20,13 +20,13 @@ async def add_question( question_id, answer = await QuestionRepository.add_one(question, type_mood, type_model) # Передача параметров type_mood и type_model return {"question_id": question_id, "answer": answer} -@router.get("") +@router.get("/get_all") async def get_questions() -> list[SQuestion]: questions = await QuestionRepository.find_all() return questions -@router.get("/{email_user}") +@router.get("/get/{email_user}") async def get_questions_by_email(email_user: str) -> list[SQuestion]: questions = await QuestionRepository.find_by_email(email_user) return questions diff --git a/sity/airports.json b/sity/airports.json new file mode 100644 index 0000000..d08944f --- /dev/null +++ b/sity/airports.json @@ -0,0 +1,51 @@ +[ + { "value": "ABA", "label": "Абакан, Абакан, ABA", "timezone": "+7", "latitude": 53.7400, "longitude": 91.3850 }, + { "value": "AAQ", "label": "Анапа, Витязево, AAQ", "timezone": "+3", "latitude": 45.0029, "longitude": 37.3473 }, + { "value": "ARH", "label": "Архангельск, Талаги, ARH", "timezone": "+3", "latitude": 64.6003, "longitude": 40.7168 }, + { "value": "ASF", "label": "Астрахань, им. Б.Н. Кустодиева, ASF", "timezone": "+4", "latitude": 46.2833, "longitude": 48.0063 }, + { "value": "BAX", "label": "Барнаул, Михайловка, BAX", "timezone": "+7", "latitude": 53.3638, "longitude": 83.5385 }, + { "value": "EGO", "label": "Белгород, им. В.Г. Шухова, EGO", "timezone": "+3", "latitude": 50.6439, "longitude": 36.5901 }, + { "value": "BQS", "label": "Благовещенск, Игнатьево, BQS", "timezone": "+9", "latitude": 50.4250, "longitude": 127.4125 }, + { "value": "BTK", "label": "Братск, Братск, BTK", "timezone": "+8", "latitude": 56.3706, "longitude": 101.6983 }, + { "value": "BZK", "label": "Брянск, Брянск, BZK", "timezone": "+3", "latitude": 53.2142, "longitude": 34.1769 }, + { "value": "VVO", "label": "Владивосток, Кневичи, VVO", "timezone": "+10", "latitude": 43.3989, "longitude": 132.1478 }, + { "value": "VOZ", "label": "Воронеж, Чертовицкое, VOZ", "timezone": "+3", "latitude": 51.8142, "longitude": 39.2296 }, + { "value": "YKS", "label": "Якутск, Якутск, YKS", "timezone": "+9", "latitude": 62.0933, "longitude": 129.7708 }, + { "value": "SVX", "label": "Екатеринбург, Кольцово, SVX", "timezone": "+5", "latitude": 56.7431, "longitude": 60.8027 }, + { "value": "KZN", "label": "Казань, Казань, KZN", "timezone": "+3", "latitude": 55.6062, "longitude": 49.2787 }, + { "value": "KUF", "label": "Самара, Курумоч, KUF", "timezone": "+4", "latitude": 53.5049, "longitude": 50.1643 }, + { "value": "IKT", "label": "Иркутск, Иркутск, IKT", "timezone": "+8", "latitude": 52.2680, "longitude": 104.3880 }, + { "value": "KGD", "label": "Калининград, Храброво, KGD", "timezone": "+2", "latitude": 54.8900, "longitude": 20.5926 }, + { "value": "KEJ", "label": "Кемерово, Кемерово, KEJ", "timezone": "+7", "latitude": 55.2700, "longitude": 86.1072 }, + { "value": "KRR", "label": "Краснодар, Пашковский, KRR", "timezone": "+3", "latitude": 45.0347, "longitude": 39.1705 }, + { "value": "KJA", "label": "Красноярск, Емельяново, KJA", "timezone": "+7", "latitude": 56.1729, "longitude": 92.4933 }, + { "value": "MCX", "label": "Махачкала, Уйташ, MCX", "timezone": "+3", "latitude": 42.8168, "longitude": 47.6523 }, + { "value": "MRV", "label": "Минеральные Воды, Минеральные Воды, MRV", "timezone": "+3", "latitude": 44.2251, "longitude": 43.0819 }, + { "value": "NBC", "label": "Набережные Челны, Бегишево, NBC", "timezone": "+3", "latitude": 55.5647, "longitude": 52.0938 }, + { "value": "NNM", "label": "Нарьян-Мар, Нарьян-Мар, NNM", "timezone": "+3", "latitude": 67.6392, "longitude": 53.1219 }, + { "value": "NJC", "label": "Нижневартовск, Нижневартовск, NJC", "timezone": "+5", "latitude": 60.9493, "longitude": 76.4836 }, + { "value": "GOJ", "label": "Нижний Новгород, Стригино, GOJ", "timezone": "+3", "latitude": 56.2301, "longitude": 43.7840 }, + { "value": "NOJ", "label": "Ноябрьск, Ноябрьск, NOJ", "timezone": "+5", "latitude": 63.1101, "longitude": 75.1624 }, + { "value": "NSK", "label": "Норильск, Алыкель, NSK", "timezone": "+7", "latitude": 69.3111, "longitude": 87.3322 }, + { "value": "OMS", "label": "Омск, Центральный, OMS", "timezone": "+6", "latitude": 54.9660, "longitude": 73.3105 }, + { "value": "REN", "label": "Оренбург, Оренбург, REN", "timezone": "+5", "latitude": 51.7958, "longitude": 55.4567 }, + { "value": "OSW", "label": "Орск, Орск, OSW", "timezone": "+5", "latitude": 51.0725, "longitude": 58.5956 }, + { "value": "PEE", "label": "Пермь, Большое Савино, PEE", "timezone": "+5", "latitude": 57.9145, "longitude": 56.0212 }, + { "value": "PKC", "label": "Петропавловск-Камчатский, Елизово, PKC", "timezone": "+12", "latitude": 53.1679, "longitude": 158.4536 }, + { "value": "PES", "label": "Петрозаводск, Бесовец, PES", "timezone": "+3", "latitude": 61.8852, "longitude": 34.1547 }, + { "value": "PYJ", "label": "Полярный, Полярный, PYJ", "timezone": "+9", "latitude": 66.4000, "longitude": 112.0333 }, + { "value": "ROV", "label": "Ростов-на-Дону, Платов, ROV", "timezone": "+3", "latitude": 47.4939, "longitude": 39.9242 }, + { "value": "RTW", "label": "Саратов, Гагарин, RTW", "timezone": "+4", "latitude": 51.5643, "longitude": 46.0468 }, + { "value": "AER", "label": "Сочи, Адлер, AER", "timezone": "+3", "latitude": 43.4499, "longitude": 39.9566 }, + { "value": "SGC", "label": "Сургут, Сургут, SGC", "timezone": "+5", "latitude": 61.3437, "longitude": 73.4010 }, + { "value": "THX", "label": "Тюмень, Рощино, THX", "timezone": "+5", "latitude": 57.1896, "longitude": 65.3243 }, + { "value": "UFA", "label": "Уфа, Уфа, UFA", "timezone": "+5", "latitude": 54.5575, "longitude": 55.8744 }, + { "value": "UCT", "label": "Ухта, Ухта, UCT", "timezone": "+4", "latitude": 63.5668, "longitude": 53.8047 }, + { "value": "ULY", "label": "Ульяновск, Баратаевка, ULY", "timezone": "+4", "latitude": 54.2683, "longitude": 48.2269 }, + { "value": "VOG", "label": "Волгоград, Гумрак, VOG", "timezone": "+3", "latitude": 48.7825, "longitude": 44.3456 }, + { "value": "VKO", "label": "Москва, Внуково, VKO", "timezone": "+3", "latitude": 55.5915, "longitude": 37.2615 }, + { "value": "SVO", "label": "Москва, Шереметьево, SVO", "timezone": "+3", "latitude": 55.9726, "longitude": 37.4146 }, + { "value": "DME", "label": "Москва, Домодедово, DME", "timezone": "+3", "latitude": 55.4103, "longitude": 37.9021 }, + { "value": "LED", "label": "Санкт-Петербург, Пулково, LED", "timezone": "+3", "latitude": 59.8003, "longitude": 30.2625 }, + { "value": "CEK", "label": "Челябинск, Баландино, CEK", "timezone": "+5", "latitude": 55.3058, "longitude": 61.5033 } +] \ No newline at end of file diff --git a/sity/conversion.py b/sity/conversion.py new file mode 100644 index 0000000..9357a0b --- /dev/null +++ b/sity/conversion.py @@ -0,0 +1,53 @@ +import json +import pandas as pd + +class CityFilter: + def __init__(self, json_file_path): + self.json_file_path = json_file_path + self.cities_set = self.load_and_extract_cities() + + def load_and_extract_cities(self): + data = self.load_json(self.json_file_path) + cities_set = set() + for entry in data: + parts = entry['label'].split(',') + if len(parts) > 1: + city1 = parts[0].strip().split()[0] if parts[0].strip().split() else '' + city2 = parts[1].strip().split()[0] if parts[1].strip().split() else '' + cities_set.update([city1, city2]) + return cities_set + + @staticmethod + def load_json(file_path): + with open(file_path, 'r', encoding='utf-8') as file: + data = json.load(file) + return data + + @staticmethod + def find_city(address, cities_set): + parts = address.split(',') + for part in parts: + words = part.strip().split() + for word in words: + if word in cities_set: + return word + return None + + def filter_cities_in_csv(self, csv_file_path, output_path): + df = pd.read_csv(csv_file_path) + df['city'] = df['address'].apply(lambda x: self.find_city(x, self.cities_set)) + df = df[df['city'].notnull()] + df.to_csv(output_path, index=False) + print(f"Filtered entries:\n{df.head(15)}") + + +# Пример использования класса +json_file_path = 'airports.json' +csv_file_path_positive = '../neural_network/dataset/filtered/filtered_dataset_positive.csv' +csv_file_path_negative = '../neural_network/dataset/filtered/filtered_dataset_negative.csv' +positive_output_path_negative = '../sity/sity_negative.csv' +negative_output_path_positive = '../sity/sity_positive.csv' + +city_filter = CityFilter(json_file_path) +city_filter.filter_cities_in_csv(csv_file_path_positive, negative_output_path_positive) +city_filter.filter_cities_in_csv(csv_file_path_negative, positive_output_path_negative)