in python/ts-to-word.py [0:0]
def write_header_graphs(data, document, temp_files):
"""
Writes out the two header-level graphs for caller sentiment and talk-time split
:param data: JSON result data from Transcribe
:param document: Word document structure to write the table into
:param temp_files: List of temporary files for later deletion
"""
characteristics = data["ConversationCharacteristics"]
# Caller sentiment graph
fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(12.5 / 2.54, 8 / 2.54), gridspec_kw={'width_ratios': [4, 3]})
period_sentiment = characteristics["Sentiment"]["SentimentByPeriod"]["QUARTER"]
# Graph configuration
ax[0].set_xlim(xmin=1, xmax=4)
ax[0].set_ylim(ymax=5, ymin=-5)
ax[0].yaxis.set_major_locator(ticker.MultipleLocator(5.0))
ax[0].spines['bottom'].set_position('zero')
ax[0].spines['top'].set_color('none')
ax[0].spines['right'].set_color('none')
ax[0].set_xticks([])
ax[0].set_title("Customer sentiment", fontsize=10, fontweight="bold", pad="12.0")
# Only draw the sentiment line if we actually have a Customer that talked
if "CUSTOMER" in period_sentiment:
# Setup our data holders, then extract it all
x_sentiment = np.array([])
y_sentiment = np.array([])
period_index = 1
for score in period_sentiment["CUSTOMER"]:
x_sentiment = np.append(x_sentiment, period_index)
y_sentiment = np.append(y_sentiment, score["Score"])
period_index += 1
# Set the line colour to match the overall sentiment
if characteristics["Sentiment"]["OverallSentiment"]["CUSTOMER"] >= 0.0:
line_colour = "darkgreen"
else:
line_colour = "red"
# Now draw out the simple line plot
x_new = np.linspace(1, 4, 200)
spline = make_interp_spline(x_sentiment, y_sentiment)
y_smooth = spline(x_new)
ax[0].plot(x_new, y_smooth, linewidth=3, color=line_colour)
# Talk time calculations and ratios
non_talk = characteristics["NonTalkTime"]["Instances"]
quiet_time = 0
for quiet in non_talk:
quiet_time += quiet["DurationMillis"]
if "AGENT" in characteristics["TalkTime"]["DetailsByParticipant"]:
agent_talk_time = characteristics["TalkTime"]["DetailsByParticipant"]["AGENT"]["TotalTimeMillis"]
else:
agent_talk_time = 0
if "CUSTOMER" in characteristics["TalkTime"]["DetailsByParticipant"]:
caller_talk_time = characteristics["TalkTime"]["DetailsByParticipant"]["CUSTOMER"]["TotalTimeMillis"]
else:
caller_talk_time = 0
total_time = agent_talk_time + caller_talk_time + quiet_time
if total_time > 0:
quiet_ratio = quiet_time / total_time * 100.0
agent_ratio = agent_talk_time / total_time * 100.0
caller_ratio = caller_talk_time / total_time * 100.0
else:
quiet_ratio = 0.0
agent_ratio = 0.0
caller_ratio = 0.0
ratio_format = "{speaker} ({ratio:.1f}%)"
# Additional configuration
ax[1].set_xticks([])
ax[1].set_yticks([])
ax[1].set_title("Talk time", fontsize=10, fontweight="bold", pad="10.0")
ax[1].spines['top'].set_color('none')
ax[1].spines['bottom'].set_color('none')
ax[1].spines['left'].set_color('none')
ax[1].spines['right'].set_color('none')
# Now draw out the plot
labels = ["time"]
width = 1.0
ax[1].bar(labels, [quiet_time], width, label=ratio_format.format(ratio=quiet_ratio, speaker="Non-Talk"),
bottom=[agent_talk_time + caller_talk_time])
ax[1].bar(labels, [caller_talk_time], width, label=ratio_format.format(ratio=caller_ratio, speaker="Customer"),
bottom=[agent_talk_time])
ax[1].bar(labels, [agent_talk_time], width, label=ratio_format.format(ratio=agent_ratio, speaker="Agent"))
box = ax[1].get_position()
ax[1].set_position([box.x0, box.y0 + box.height * 0.25, box.width, box.height * 0.75])
ax[1].legend(loc="upper center", bbox_to_anchor=(0.5, -0.05), ncol=1)
chart_file_name = "./" + "talk-time.png"
plt.savefig(chart_file_name, facecolor="aliceblue")
temp_files.append(chart_file_name)
document.add_picture(chart_file_name, width=Cm(7.5))
plt.clf()