Как создать все комбинации набора данных контекстных разговоров с помощью наборов данных tensorflow

#tensorflow #tensorflow-datasets

Вопрос:

Предположим, у меня есть файл набора данных tsv разговоров произвольной длины, каждая вкладка сообщения разделена, каждая строка представляет полный разговор:

 HitHow are you?tIm doing well
This is a conversation?tYes.tHuhtIts also a test
 

Я хотел бы создать набор данных tensorflow из этого, содержащий все комбинации разговоров по порядку, как это (я разделю входные данные и целевые t объекты, а также отдельные сообщения b ).:

 HitHow are you?
Hi/bHow are you?tIm doing well
This is a conversation?tYes.
This is a conversation?/bYes.tHuh
This is a conversation?/bYes./bHuhtIts also a test
 

По сути, я хочу реализовать это, но в наборах данных tensorflow:

 def convertline(text, max_length=20):
    text=text.split("t") #split the conversation by tabs
    inputs, targets=[],[] #create empty arrays for inputs and targets
    for y in range(1,len(text)): #iterate through the split conversation
        x=y-max_length if y-max_length >= 0 else 0 #get the starting value; if it's negative, use 0 instead
        inputs.append("/b".join(text[x:y])) #append to the inputs the current window, joined by /b
        targets.append(text[y]) #append the target
    return [{"inputs":inputs[i], "targets":targets[i]} for i in range(len(inputs))] #zip them together in a dict of inputs and targets

with open("testfile.txt", "r") as f: #open a file
    line = f.readline() #read file line by line
    while line:
        print(convertline(line.strip())) #run the function and print its results
        line=f.readline()
 

который возвращает:

 [{'inputs': 'Hi', 'targets': 'How are you?'}, {'inputs': 'Hi/bHow are you?', 'targets': 'Im doing well'}]
[{'inputs': 'This is a conversation?', 'targets': 'Yes.'}, {'inputs': 'This is a conversation?/bYes.', 'targets': 'Huh'}, {'inputs': 'This is a conversation?/bYes./bHuh', 'targets': 'Its also a test'}]
 

Это то, что у меня есть до сих пор:

 def dataset(split, shuffle_files=False):
    # Load lines from the text file as examples.
    ds = tf.data.TextLineDataset(nq_tsv_path[split])
    # Split each "<question>t<answer>" example into (question, answer) tuple.
    # This definitely won't work, and is most likely where the code to generate sliding windows should be
    ds = ds.map(functools.partial(tf.io.decode_csv, record_defaults=["", ""],
                field_delim="t", use_quote_delim=False),
                num_parallel_calls=tf.data.experimental.AUTOTUNE)
    # Map the dataset into dicts of questions and answers
    ds = ds.map(lambda *ex: dict(zip(["question", "answer"], ex)))
    return ds