scripts: Add a script to generate pseudo levels

This commit is contained in:
Alexander Polynomdivision 2018-10-07 14:35:36 +02:00
parent eed1149b0c
commit d24893d75d
2 changed files with 71 additions and 1 deletions

View File

@ -36,4 +36,16 @@ Asks about a new level to add
### Usage ### Usage
- `URI`: The URI of the MongoDB instance - `URI`: The URI of the MongoDB instance
- `Database`: The name of the database in the MongoDB instance - `Database**: The name of the database in the MongoDB instance
## generate_test_levels.py
Generates 10 levels with names and a description. The name and the description are generated using
Markov Chains, while the Bee Movie script serves as data for the model.
**Note**: Requires `mongodb`, `markovify` to be installed via pip
**Note**: Requires the file `bee-movie.txt` to be in the same folder as the script
### Usage
- `URI`: The URI of the MongoDB instance
- `Database**: The name of the database in the MongoDB instance

View File

@ -0,0 +1,58 @@
import pymongo
import markovify
import sys
import random
def log(msg, err=False, tabs=0):
if (not err):
print("[*] " + "\t" * tabs + msg)
else:
print("[X] " + "\t" * tabs + msg)
def dbg(msg, tabs=0):
print("[D] " + "\t" * tabs + msg)
if (len(sys.argv) < 3):
log("Not enough arguments!", err=True)
sys.exit(1)
# Load the model for the markov chains
file = open("bee-movie.txt", "r")
text = file.read()
file.close()
# Create the model using Markov Chains
model = markovify.Text(text)
# Generate 10 levels
levels = []
vocab = list(range(1, 100))
level = 1
for i in range(10):
name = model.make_short_sentence(50, tries=100)
description = ". ".join([model.make_sentence(tries=100) for x in range(4)])
level_vocab = []
# Get random vocabulary
for v in range(4):
index = random.randint(0, len(vocab))
level_vocab.append(index)
del vocab[index]
print("{0}: {1} -> {2}".format(level, name, description))
levels.append({
"level": level,
"name": name,
"description": description,
"vocab": level_vocab
})
level += 1
log("Connecting to database...")
client = pymongo.MongoClient(sys.argv[1])
log("Getting DB...")
db = client[sys.argv[2]]
log("Inserting...")
res = db["levels"].insert_many(levels)
log("Success", tabs=1)