# luyabs/literary-works-recommendation-algorithm architecture

> Architecture diagram of the GitHub repository luyabs/literary-works-recommendation-algorithm, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/luyabs/literary-works-recommendation-algorithm
- Repository: https://github.com/luyabs/literary-works-recommendation-algorithm

## Overview

##### Project Type & Purpose
- This project implements the algorithmic part of a hybrid literary works recommendation system. It combines collaborative filtering (e.g., LFM), factorization machines (FM/DeepFM), and deep learning (with BERT for semantic feature extraction) to generate recommendations.
- The README explains that the recommendation process involves a dual-stage prediction (recall using LFM and re-ranking with FM or DeepFM) along with an additional “基于Embedding”的近似向量推荐流程, plus mechanisms for incremental training and index mapping.

##### Major Components
a. API/Controller Layer:
– Represented by the Flask-based controller (controller/recommed_controller.py).
– This is the interaction endpoint that triggers predictions.
b. Database Access & Mapping Layer:
– Include the mapper directory (dataset_mapper, id_mapping_mapper, train_and_predictor_mapper) which encapsulates the logic for database communication.
– Also include the mysql_connector for wrapping and managing MySQL connections.
c. Model & Training Modules:
– The model_and_train directory includes multiple recommendation models and their respective training, prediction, and evaluation scripts. Break these out into:
- LFM: Contains lfm_model.py and lfm_trainer_and_predictor.py.
- FM & DeepFM: Represented by fm and deep_fm directories. Emphasize that these come with associated training logs and model definitions.
- Multi-feature FM: Incorporates additional components for text extraction using BERT (with supporting configuration and weight files).
- id_mapping.py: This is key to translating raw (possibly non-sequential) database IDs into continuous indices for the models’ embedding layers.
d. Data Ingestion & Preprocessing:
– The raw_data directory includes data files (and subdirectories like Dou Ban Books Dataset), a script for data cleaning (crawler.py), and a script for importing data into the database (save_into_db.py).
e. Model Parameters:
– The model_params directory stores pre-trained model weights that can be loaded without retraining.

##### Relationships & Interactions
– Show that these modules fetch and update data in the underlying MySQL database.
– For the Model & Training Modules, indicate the training pipeline flow: raw data ➔ preprocessing (via raw_data scripts) ➔ mapping (index translations through id_mapping.py) ➔ model training (LFM/FM/DeepFM) ➔ predictions.
– Emphasize the dual prediction stages: first a recall stage using LFM, then a re-ranking stage using FM/DeepFM, with additional incorporation of embedding-based similarity.
– Highlight how incremental training is managed by dual index mapping objects and atomic model updates (using locks).

##### Architectural Patterns & Design Principles
– Modularization: Clearly separate core concerns (API, database access, model training, raw data ingestion) into distinct components.
– Flexibility & Extensibility: Make note of the design for switching models (controller can be configured to choose between lfm and fm/deep_fm) and additional support for BERT-based features.
– Data Flow: Clearly detail the data flow from raw input (dat files and crawled data) through ETL into the MySQL backend; then on-demand retrieval by mappers for recommendation inference.
– Concurrency & Atomicity: Include components representing scheduling and concurrency control (mutex locks during incremental training).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Online Prediction Pipeline
    subgraph "Online Prediction Pipeline"
        A1["API Controller (Flask)"]:::service
        subgraph "Data Access & Mapping Layer"
            B1["Dataset Mapper"]:::mapper
            B2["ID Mapping Mapper"]:::mapper
            B3["Train & Predictor Mapper"]:::mapper
            subgraph "MySQL Connector"
                C1["Base MySQL Connector"]:::db
                C2["MySQL Connector"]:::db
                C3["MySQL Connector Keep Alive"]:::db
            end
        end
    end

    %% ML Model & Training Modules
    subgraph "ML Model & Training Modules"
        subgraph "LFM Module"
            LFM1["LFM Model"]:::model
            LFM2["LFM Trainer & Predictor"]:::model
            LFM3["LFM Training Logs"]:::logs
        end
        subgraph "FM Module"
            FM1["FM Model"]:::model
            FM2["FM Trainer & Predictor"]:::model
            FM3["FM Training Logs"]:::logs
        end
        subgraph "DeepFM Module"
            DF1["DeepFM Model"]:::model
            DF2["DeepFM Trainer & Predictor"]:::model
            DF3["DeepFM Training Logs"]:::logs
        end
        subgraph "Multi-feature FM Module (BERT)"
            MF1["Multi-feature FM Model"]:::model
            MF2["Multi-feature FM Trainer & Predictor"]:::model
            MF3["BERT Config"]:::config
            MF4["BERT Tokenizer"]:::config
            MF5["BERT Vocab"]:::config
        end
        IDM["ID Mapping for Embedding"]:::utility
    end

    %% Offline Training / Data Ingestion
    subgraph "Offline Training / Data Ingestion"
        RD["Raw Data"]:::data
        CR["Crawler Script"]:::utility
        SI["Save into DB Script"]:::utility
    end

    E1["Model Parameters"]:::params
    F1["Scheduler/Concurrency Control"]:::service

    %% Connections between Online Pipeline components
    A1 -->|"调用"| B1
    A1 -->|"调用"| B2
    A1 -->|"调用"| B3

    B1 -->|"查询"| C2
    B2 -->|"查询"| C2
    B3 -->|"查询"| C2

    %% Data ingestion feeding into MySQL Connector
    SI -->|"导入"| C2
    RD -->|"ETL"| CR
    RD -->|"ETL"| SI

    %% Flow from MySQL Connector to ML Modules via ID Mapping
    C2 -->|"映射"| IDM
    IDM -->|"输入"| LFM1
    IDM -->|"输入"| FM1
    IDM -->|"输入"| DF1
    IDM -->|"输入"| MF1

    %% Predictions flowing back to Controller
    LFM2 -->|"预测结果"| A1
    FM2 -->|"预测结果"| A1
    DF2 -->|"预测结果"| A1
    MF2 -->|"预测结果"| A1

    %% Model Parameters loaded into ML Modules
    E1 -->|"加载"| LFM1
    E1 -->|"加载"| FM1
    E1 -->|"加载"| DF1
    E1 -->|"加载"| MF1

    %% Incremental training scheduling
    F1 -->|"调度训练"| LFM2
    F1 -->|"调度训练"| FM2
    F1 -->|"调度训练"| DF2
    F1 -->|"调度训练"| MF2

    %% Click Events
    click A1 "https://github.com/luyabs/literary-works-recommendation-algorithm/blob/master/controller/recommed_controller.py"
    click B1 "https://github.com/luyabs/literary-works-recommendation-algorithm/blob/master/mapper/dataset_mapper.py"
    click B2 "https://github.com/luyabs/literary-works-recommendation-algorithm/blob/master/mapper/id_mapping_mapper.py"
    click B3 "https://github.com/luyabs/literary-works-recommendation-algorithm/blob/master/mapper/train_and_predictor_mapper.py"
    click C1 "https://github.com/luyabs/literary-works-recommendation-algorithm/blob/master/mysql_connector/base_mysql_connector.py"
    click C2 "https://github.com/luyabs/literary-works-recommendation-algorithm/blob/master/mysql_connector/mysql_connector.py"
    click C3 "https://github.com/luyabs/literary-works-recommendation-algorithm/blob/master/mysql_connector/mysql_connector_keep_alive.py"
    click LFM1 "https://github.com/luyabs/literary-works-recommendation-algorithm/blob/master/model_and_train/lfm/lfm_model.py"
    click LFM2 "https://github.com/luyabs/literary-works-recommendation-algorithm/blob/master/model_and_train/lfm/lfm_trainer_and_predictor.py"
    click LFM3 "https://github.com/luyabs/literary-works-recommendation-algorithm/blob/master/model_and_train/lfm/LFM训练记录.txt"
    click FM1 "https://github.com/luyabs/literary-works-recommendation-algorithm/blob/master/model_and_train/fm/fm_model.py"
    click FM2 "https://github.com/luyabs/literary-works-recommendation-algorithm/blob/master/model_and_train/fm/fm_trainer_and_predictor.py"
    click FM3 "https://github.com/luyabs/literary-works-recommendation-algorithm/blob/master/model_and_train/fm/FM训练记录.txt"
    click DF1 "https://github.com/luyabs/literary-works-recommendation-algorithm/blob/master/model_and_train/deep_fm/deep_fm_model.py"
    click DF2 "https://github.com/luyabs/literary-works-recommendation-algorithm/blob/master/model_and_train/deep_fm/deep_fm_trainer_and_predictor.py"
    click DF3 "https://github.com/luyabs/literary-works-recommendation-algorithm/blob/master/model_and_train/deep_fm/DeepFM训练记录.txt"
    click MF1 "https://github.com/luyabs/literary-works-recommendation-algorithm/blob/master/model_and_train/multi_feature_fm/mult_feature_fm.py"
    click MF2 "https://github.com/luyabs/literary-works-recommendation-algorithm/blob/master/model_and_train/multi_feature_fm/multi_feature_fm_trainer_and_predictor.py"
    click MF3 "https://github.com/luyabs/literary-works-recommendation-algorithm/blob/master/model_and_train/multi_feature_fm/base_bert_chinese/config.json"
    click MF4 "https://github.com/luyabs/literary-works-recommendation-algorithm/blob/master/model_and_train/multi_feature_fm/base_bert_chinese/tokenizer.json"
    click MF5 "https://github.com/luyabs/literary-works-recommendation-algorithm/blob/master/model_and_train/multi_feature_fm/base_bert_chinese/vocab.txt"
    click IDM "https://github.com/luyabs/literary-works-recommendation-algorithm/blob/master/model_and_train/id_mapping.py"
    click RD "https://github.com/luyabs/literary-works-recommendation-algorithm/tree/master/raw_data"
    click CR "https://github.com/luyabs/literary-works-recommendation-algorithm/blob/master/raw_data/crawler.py"
    click SI "https://github.com/luyabs/literary-works-recommendation-algorithm/blob/master/raw_data/save_into_db.py"
    click E1 "https://github.com/luyabs/literary-works-recommendation-algorithm/blob/master/model_params/model参数太大 传到了百度云.txt"

    %% Styles
    classDef service fill:#f9f,stroke:#333,stroke-width:2px;
    classDef mapper fill:#bbf,stroke:#333,stroke-width:2px;
    classDef db fill:#cfc,stroke:#333,stroke-width:2px;
    classDef model fill:#fc9,stroke:#333,stroke-width:2px;
    classDef logs fill:#ff9,stroke:#333,stroke-width:2px;
    classDef config fill:#9ff,stroke:#333,stroke-width:2px;
    classDef utility fill:#ddd,stroke:#333,stroke-width:2px;
    classDef data fill:#fdd,stroke:#333,stroke-width:2px;
    classDef params fill:#dfd,stroke:#333,stroke-width:2px;
```
