from flask import render_template, jsonify, request
from app import app
from app import evolver


@app.route('/')
@app.route('/index')
def index():
    return render_template('index.html')


def format_transcriptions(transcriptions):
    '''Split the raw string of transcriptions into
    the correct tuple rules.'''
    return [(pair.split(':')[0], pair.split(':')[1]) for pair in transcriptions.split('\n')]


@app.route('/evolve', methods=['POST'])
def evolve():
    words = request.form['words'].split()

    try:
        transcriptions = format_transcriptions(request.form['transcriptions'])
    except IndexError:
        return jsonify({'error': 'Error: Transcription seperator must be a colon'})

    try:
        generations = int(request.form['generations'])
    except ValueError:
        return jsonify({'error': 'Error: Generations must be an integer'})

    words, rules = evolver.evolve(words, generations, transcriptions)

    return jsonify({'rules': rules, 'words': words, 'error': 0})

from flask import render_template, jsonify, request
from app import app
from app import evolver


@app.route('/')
@app.route('/index')
def index():
    return render_template('index.html')


def format_transcriptions(transcriptions):
    '''Split the raw string of transcriptions into
    the correct tuple rules.'''
    clean_transcriptions = transcriptions.strip().lower()

    if len(clean_transcriptions) == 0:
        return []
    else:
        return [(pair.split(':')[0], pair.split(':')[1]) for pair in clean_transcriptions.split('\n')]


@app.route('/evolve', methods=['POST'])
def evolve():
    words = request.form['words'].split()

    try:
        transcriptions = format_transcriptions(request.form['transcriptions'])
    except IndexError:
        return jsonify({'error': 'Error: Transcription seperator must be a colon'})

    try:
        generations = int(request.form['generations'])
    except ValueError:
        return jsonify({'error': 'Error: Generations must be an integer'})

    words, rules = evolver.evolve(words, generations, transcriptions)

    return jsonify({'rules': rules, 'words': words, 'error': 0})

Fix bug when transcriptions are empty
Fix bug when transcriptions are empty

  from flask import render_template, jsonify, request
  from app import app
  from app import evolver
  
  
  @app.route('/')
  @app.route('/index')
  def index():
      return render_template('index.html')
  
  
  def format_transcriptions(transcriptions):
      '''Split the raw string of transcriptions into
      the correct tuple rules.'''
+     clean_transcriptions = transcriptions.strip().lower()
+ 
+     if len(clean_transcriptions) == 0:
+         return []
+     else:
-     return [(pair.split(':')[0], pair.split(':')[1]) for pair in transcriptions.split('\n')]
+         return [(pair.split(':')[0], pair.split(':')[1]) for pair in clean_transcriptions.split('\n')]
  
  
  @app.route('/evolve', methods=['POST'])
  def evolve():
      words = request.form['words'].split()
  
      try:
          transcriptions = format_transcriptions(request.form['transcriptions'])
      except IndexError:
          return jsonify({'error': 'Error: Transcription seperator must be a colon'})
  
      try:
          generations = int(request.form['generations'])
      except ValueError:
          return jsonify({'error': 'Error: Generations must be an integer'})
  
      words, rules = evolver.evolve(words, generations, transcriptions)
  
      return jsonify({'rules': rules, 'words': words, 'error': 0})
  
Fix bug when transcriptions are empty
## Code Before:
from flask import render_template, jsonify, request
from app import app
from app import evolver


@app.route('/')
@app.route('/index')
def index():
    return render_template('index.html')


def format_transcriptions(transcriptions):
    '''Split the raw string of transcriptions into
    the correct tuple rules.'''
    return [(pair.split(':')[0], pair.split(':')[1]) for pair in transcriptions.split('\n')]


@app.route('/evolve', methods=['POST'])
def evolve():
    words = request.form['words'].split()

    try:
        transcriptions = format_transcriptions(request.form['transcriptions'])
    except IndexError:
        return jsonify({'error': 'Error: Transcription seperator must be a colon'})

    try:
        generations = int(request.form['generations'])
    except ValueError:
        return jsonify({'error': 'Error: Generations must be an integer'})

    words, rules = evolver.evolve(words, generations, transcriptions)

    return jsonify({'rules': rules, 'words': words, 'error': 0})

## Instruction:
Fix bug when transcriptions are empty
## Code After:
from flask import render_template, jsonify, request
from app import app
from app import evolver


@app.route('/')
@app.route('/index')
def index():
    return render_template('index.html')


def format_transcriptions(transcriptions):
    '''Split the raw string of transcriptions into
    the correct tuple rules.'''
    clean_transcriptions = transcriptions.strip().lower()

    if len(clean_transcriptions) == 0:
        return []
    else:
        return [(pair.split(':')[0], pair.split(':')[1]) for pair in clean_transcriptions.split('\n')]


@app.route('/evolve', methods=['POST'])
def evolve():
    words = request.form['words'].split()

    try:
        transcriptions = format_transcriptions(request.form['transcriptions'])
    except IndexError:
        return jsonify({'error': 'Error: Transcription seperator must be a colon'})

    try:
        generations = int(request.form['generations'])
    except ValueError:
        return jsonify({'error': 'Error: Generations must be an integer'})

    words, rules = evolver.evolve(words, generations, transcriptions)

    return jsonify({'rules': rules, 'words': words, 'error': 0})
