How to break conversation data into pairs of (Context , Response)
<p>I'm using Gensim Doc2Vec model, trying to cluster portions of a customer support conversations. My goal is to give the support team an auto response suggestions.</p>

<p><strong>Figure 1:</strong> shows a sample conversations where the user question is answered in the next conversation line, making it easy to extract the data:</p>

<p><a href="http://i.stack.imgur.com/N4ri4.gif"><img src="http://i.stack.imgur.com/N4ri4.gif" alt="Figure 1"></a> </p>

<p><sup>during the conversation <strong>"hello"</strong> and <strong>"Our offices are located in NYC"</strong> should be suggested</sup></p>

<hr>

<p><strong>Figure 2:</strong> describes a conversation where the questions and answers are not in sync</p>

<p><a href="http://i.stack.imgur.com/oHUQu.gif"><img src="http://i.stack.imgur.com/oHUQu.gif" alt="Figure 2"></a></p>

<p><sup>during the conversation <strong>"hello"</strong> and <strong>"Our offices are located in NYC"</strong> should be suggested</sup></p>

<hr>

<p><strong>Figure 3:</strong> describes a conversation where the context for the answer is built over time, and for classification purpose (I'm assuming) some of the lines are redundant.</p>

<p><a href="http://i.stack.imgur.com/muf6Y.gif"><img src="http://i.stack.imgur.com/muf6Y.gif" alt="Figure 3"></a></p>

<p><sup>during the conversation <strong>"here is a link for the free trial account"</strong> should be suggested</sup></p>

<hr>

<p>I have the following data per conversation line (simplified):<br>
who wrote the line (user or agent), text, time stamp</p>

<p>I'm using the following code to train my model:</p>

<pre><code>from gensim.models import Doc2Vec
from gensim.models.doc2vec import TaggedLineDocument
import datetime

print('Creating documents',datetime.datetime.now().time())
context = TaggedLineDocument('./test_data/context.csv')

print('Building model',datetime.datetime.now().time())

model = Doc2Vec(context,size = 200, window = 10, min_count = 10, workers=4)
print('Training...',datetime.datetime.now().time())

for epoch in range(10):
    print('Run number :',epoch)
    model.train(context)

model.save('./test_data/model')
</code></pre>

<p><strong>Q</strong>: How should I structure my training data and what heuristics could be applied in order to extract it from the raw data?</p>

<p>To train a model I would start by concatenating consecutive sequences of messages. What I would do is, using the timestamps, concatenate the messages without any message in between from the other entity.</p>

<p>For instance:</p>

<pre><code>Hello
I have a problem
I cannot install software X
                                       Hi
                                       What error do you get?
</code></pre>

<p>would be:</p>

<pre><code>Hello I have a problem I cannot install software X
                                       Hi What error do you get?
</code></pre>

<p>Then I would train a model with sentences in that format. I would do that because I am assuming that the conversations have a "single topic" all the time between interactions from the entities. And in that scenario suggesting a single message <code>Hi What error do you get?</code> would be totally fine.</p>

<p>Also, take a look at the data. If the questions from the users are usually single-sentenced (as in the examples) sentence detection could help a lot. In that case I would apply sentence detection on the concatenated strings (<code>nltk</code> could be an option) and use only single-sentenced questions for training. That way you can avoid the out-of-sync problem when training the model at the price of reducing the size of the dataset.</p>

<p>On the other hand, I would <em>really</em> consider to start with a very simple method. For example you could score questions by tf-idf and, to get a suggestion, you can take the most similar question in your dataset wrt some metric (e.g. cosine similarity) and suggest the answer for that question. That will perform very bad in sentences with context information (e.g. <code>how do you do it?</code>) but can perform well in sentences like <code>where are you based?</code>.</p>

<p>My last suggestion is because <a href="https://arxiv.org/abs/1509.01626" rel="nofollow">traditional methods perform even better than complex NN methods when the dataset is small</a>. How big is your dataset?</p>

<p><em>How</em> you train a NN method is also crucial, there are a lot of hyper-parameters, and tuning them properly can be difficult, that's why having a baseline with a simple method can help you a lot to check how well you are doing. In this other <a href="http://arxiv.org/abs/1607.05368" rel="nofollow">paper</a> they compare the different hyper-parameters for doc2vec, maybe you find it useful.</p>

<p><strong>Edit:</strong> a completely different option would be to train a model to "link" questions with answers. But for that you should manually tag each question with the corresponding answer and then train a supervised learning model on that data. That could potentially generalize better but with the added effort of manually labelling the sentences and still it doesn't look like an easy problem to me.</p>

python text-mining doc2vec gensym