import copy

import numpy as np

from matchzoo import tasks
from matchzoo import models


def prepare(
    model,
    train_pack,
    preprocessor=None,
    verbose=1
):
    """

    :param model:
    :param train_pack:
    :param preprocessor:
    :param verbose:
    :return:

    """
    params = copy.deepcopy(model.params)
    if preprocessor:
        new_preprocessor = copy.deepcopy(preprocessor)
    else:
        new_preprocessor = model.get_default_preprocessor()

    train_pack_processed = new_preprocessor.fit_transform(train_pack, verbose)

    if not params['task']:
        params['task'] = _guess_task(train_pack)

    context = {}
    if 'input_shapes' in new_preprocessor.context:
        context['input_shapes'] = new_preprocessor.context['input_shapes']

    if isinstance(model, models.DSSMModel):
        params['input_shapes'] = context['input_shapes']

    if 'with_embedding' in params:
        term_index = new_preprocessor.context['vocab_unit'].state['term_index']
        vocab_size = len(term_index) + 1
        params['embedding_input_dim'] = vocab_size

    new_model = type(model)(params=params)
    new_model.guess_and_fill_missing_params(verbose=verbose)
    new_model.build()
    new_model.compile()

    return new_model, train_pack_processed, new_preprocessor


def _guess_task(train_pack):
    if np.issubdtype(train_pack.relation['label'].dtype, np.number):
        return tasks.Ranking()
    elif np.issubdtype(train_pack.relation['label'].dtype, list):
        num_classes = int(train_pack.relation['label'].apply(len).max())
        return tasks.Classification(num_classes)

@@ -1,15 +1,79 @@
-import copy
+%22%22%22Prepare mode, preprocessor, and data pack.%22%22%22%0A%0Aimport copy%0Aimport typing
 %0A%0Aim
@@ -90,16 +90,32 @@
  as np%0A%0A
+import matchzoo%0A
 from mat
@@ -187,29 +187,28 @@
  model,%0A    
-train
+data
 _pack,%0A    p
@@ -244,17 +244,196 @@
 =1%0A)
-:%0A    %22%22%22
+ -%3E typing.Tuple%5Bmatchzoo.engine.BaseModel,%0A                  matchzoo.DataPack,%0A                  matchzoo.engine.BasePreprocessor%5D:%0A    %22%22%22%0A    Prepare mode, preprocessor, and data pack.
 %0A%0A  
@@ -459,21 +459,20 @@
  :param 
-train
+data
 _pack:%0A 
@@ -783,21 +783,20 @@
 ansform(
-train
+data
 _pack, v
@@ -864,29 +864,28 @@
 _guess_task(
-train
+data
 _pack)%0A%0A    

Rename var in prepare.