https://github.com/jaytwolab/nl_to_uml
[ChatGPT] Generating UML from Natural Language :kr: 자연어로 UML 생성
https://github.com/jaytwolab/nl_to_uml
Last synced: 9 months ago
JSON representation
[ChatGPT] Generating UML from Natural Language :kr: 자연어로 UML 생성
- Host: GitHub
- URL: https://github.com/jaytwolab/nl_to_uml
- Owner: JayTwoLab
- License: mit
- Created: 2024-11-20T08:33:53.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-12-31T07:46:36.000Z (over 1 year ago)
- Last Synced: 2025-10-08T23:18:53.429Z (9 months ago)
- Language: Python
- Homepage:
- Size: 96.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.ENG.md
- License: LICENSE
Awesome Lists containing this project
README
## `nl_to_uml` : Automatically Generating UML from Natural Language using ChatGPT
> [English](README.ENG.md), [Korean](README.md)
- **Natural language** text files are read, and a corresponding **UML** file is **generated**.
- Mapping information file: `mapping.json`
```
{
"input1.txt": "output1.puml"
}
```
- `input1.txt`: A natural language text file (description of UML)
- `output1.puml`: UML file name
- Install the OpenAI library before running the Python file.
```
pip install openai
```
### Python Code
> nl_to_uml.py
```python
if __name__ == "__main__":
# Read API key from environment variable
# Set the environment variable 'OPENAI_API_KEY' with your OpenAI project API key
openai.api_key = os.getenv("OPENAI_API_KEY")
# Check if the key is properly set (optional)
if not openai.api_key:
raise ValueError("OPENAI_API_KEY environment variable is not set.")
# Enter the mapping file name
mapping_file = input("Enter the mapping file name (e.g., mapping.json): ")
# mapping_file = 'mapping.json'
# Process the mapping file
process_mapping_file(mapping_file)
```
### Functionality Overview
1. **Input `mapping.json` File**:
- The user inputs the mapping file name.
- The mapping file contains 1:1 mapping information between natural language text files and PlantUML files.
- Example:
```json
{
"input1.txt": "output1.puml",
"input2.txt": "output2.puml"
}
```
2. **Read Natural Language Text File**:
- Reads each mapped natural language text file and extracts its content.
3. **Generate PlantUML Code**:
- Calls the OpenAI API to convert natural language to PlantUML code.
4. **Save as PlantUML File**:
- Saves the converted code with the mapped file name.
### Example Execution
#### 1. Create Input File: `mapping.json`
> You can name the JSON file as desired.
```json
{
"input1.txt": "output1.puml",
"input2.txt": "output2.puml"
}
```
#### 2. Run the Program
```
python nl_to_uml.py
Enter the mapping file name (e.g., mapping.json): mapping.json
```
> If the source code does not take arguments, modify the comments to use it.
#### 3. Output Files (`*.puml`) Created
```
Processing: input1.txt -> output1.puml
Saved as PlantUML file: output1.puml
Processing: input2.txt -> output2.puml
Saved as PlantUML file: output2.puml
```
---
#### Example 1: `input1.txt`
```
Generate a class diagram.
The Teacher class includes a name and a subject, and contains setSubject and getSubject methods.
```
#### Example 1 Output: `output1.puml`
```plantuml
@startuml
class Teacher {
- String name
- String subject
+ setSubject(String subject)
+ getSubject(): String
}
@enduml
```

> UML images are not automatically generated. Use a library or program that converts PlantUML syntax (e.g., StarUML) to an image.
---
#### Example 2: `input2.txt`
```
Generate a use case diagram.
Students log in to register for courses and view subjects.
Professors log in to view enrolled students.
Staff log in to view subjects and register classes.
```
#### Example 2 Output: `output2.puml`
```plantuml
@startuml
left to right direction
actor Student
actor Professor
actor Staff
usecase "Register for Courses"
usecase "View Subjects"
usecase "View Enrolled Students"
usecase "Register Classes"
Student --> "Register for Courses" : Login
Student --> "View Subjects" : Login
Professor --> "View Enrolled Students" : Login
Staff --> "View Subjects" : Login
Staff --> "Register Classes" : Login
@enduml
```

---
### Notes
1. The `mapping.json` file must comply with the JSON format.
2. Ensure that text files and PlantUML files are stored in the same directory.
3. The OpenAI API key must be configured (replace `YOUR_API_KEY` with your key).
### License
- MIT License
- See https://github.com/j2doll/nl_to_uml for more information.