How to generate all combinations of a contextual conversation dataset using tensorflow datasets

Viewed 39

Suppose I have a tsv dataset file of conversations with arbitrary lengths, with each message tab seperated, each line representing a full conversation:

Hi\tHow are you?\tIm doing well
This is a conversation?\tYes.\tHuh\tIts also a test

I would like to create a tensorflow dataset from this containing all combinations of the conversation in order, like this (I'll seperate the inputs and targets with \t and the individual messages with \b):

Hi\tHow 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./bHuh\tIts also a test

I'm essentially looking to implement this but in tensorflow datasets:

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()

which returns:

[{'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'}]

This is what I have so far:

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
0 Answers
Related