An open API service indexing awesome lists of open source software.

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 생성

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
```

![](output1.svg)

> 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
```

![](output2.svg)

---

### 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.