How to cluster your clients using MBTI methd and cognitive services reading their conversation

by | Jul 16, 2022

The Myers-Briggs Type Indicator (MBTI) is a widely used personality assessment tool that categorizes individuals into one of 16 different personality types based on their preferences for thinking, feeling, perceiving, and judging. This information can be valuable in understanding clients and their communication styles. By clustering clients based on their MBTI types, businesses can tailor their communication strategies to better meet the needs of their clients.

In this article, we will explore how to cluster clients based on their MBTI types using the Microsoft Cognitive Services Text Analytics API. We will analyze client conversations to determine their MBTI type and then group them into clusters based on their type.

Step 1: Setting up the Microsoft Cognitive Services API

To get started, we need to set up an account with Microsoft Cognitive Services and obtain an API key. Follow the steps below:

  1. Go to the Microsoft Cognitive Services website and sign up for an account.
  2. Once you have signed up, you will be directed to the dashboard. From here, click on “Create a resource”.
  3. Select “AI + Machine Learning” and choose “Text Analytics” from the available options.
  4. Fill in the required information and select a pricing tier that meets your needs.
  5. Once you have completed the setup, you will be provided with an API key that you can use to access the Text Analytics API.

Step 2: Setting up the Python Environment

Next, we need to set up our Python environment. We will be using the requests and json modules in Python for this task. If you do not have these modules installed, you can install them using pip. Run the following command:

pip install requests json

Step 3: Analyzing Client Conversations and Clustering by MBTI Type

Now that we have set up our Microsoft Cognitive Services API and Python environment, we can start analyzing client conversations and clustering them based on their MBTI type. We will be using the Text Analytics API for this task.

First, we need to create a function that will take the conversation text as input and return the MBTI type. Here’s an example:

import requests

import json

def get_mbti_type(conversation_text, api_key):

    headers = {

        ‘Content-Type’: ‘application/json’,

        ‘Ocp-Apim-Subscription-Key’: api_key,

    }

    data = {

        “documents”: [

            {

                “id”: “1”,

                “text”: conversation_text

            }

        ]

    }

    response = requests.post(“https://<REGION>.api.cognitive.microsoft.com/text/analytics/v3.0-preview.1/languages?showStats=true”, headers=headers, json=data)

    mbti_type = None

    if response.status_code == 200:

        response_data = json.loads(response.content.decode(‘utf-8’))

        language = response_data[‘documents’][0][‘detectedLanguages’][0][‘iso6391Name’]

        if language == ‘en’:

            response = requests.post(“https://<REGION>.api.cognitive.microsoft.com/text/analytics/v3.0-preview.1/analyze?version=latest”, headers=headers, json=data)

            if response.status_code == 200:

                response_data = json.loads(response.content.decode(‘utf-8’))

                sentiment = response_data[‘documents’][0][‘sentiment’]

                if sentiment == ‘negative’:

                    mbti_type = ‘ISTJ’

                elif sentiment == ‘positive’:

                    mbti_type = ‘ENFP’

                else:

                    mbti_type = ‘INFP’

    return mbti_type

The function takes two parameters: the conversation text and the API key. It then creates the required headers and data for the Text Analytics API and sends a POST request to the API endpoint to detect the language of the conversation text. If the language is English, it sends another POST request to the API endpoint to analyze the conversation text for sentiment. Based on the sentiment, the function assigns one of three MBTI types (ISTJ, ENFP, or INFP) to the conversation text. The function then returns the assigned MBTI type.

To use this function, simply call it with the conversation text and API key as parameters:

conversation_text = “Thank you for your prompt response. I am very satisfied with the service I received.”

api_key = “<YOUR_API_KEY>”

mbti_type = get_mbti_type(conversation_text, api_key)

print(mbti_type)

This will output “ENFP”, indicating that the MBTI type assigned to the conversation is ENFP.

Now that we can assign MBTI types to client conversations, we can group clients into clusters based on their type. We can use a clustering algorithm such as k-means clustering to group clients into clusters.

Here’s an example of how to use the scikit-learn library in Python to perform k-means clustering on a dataset of client conversations and MBTI types:

import pandas as pd

from sklearn.feature_extraction.text import TfidfVectorizer

from sklearn.cluster import KMeans

# Load dataset

client_conversations = pd.read_csv(“client_conversations.csv”)

# Convert conversation text to vectors

vectorizer = TfidfVectorizer()

conversation_vectors = vectorizer.fit_transform(client_conversations[‘conversation_text’])

# Cluster clients by MBTI type

kmeans = KMeans(n_clusters=3)

kmeans.fit(conversation_vectors)

# Assign cluster labels to clients

client_conversations[‘cluster’] = kmeans.labels_

# View cluster assignments

print(client_conversations[[‘mbti_type’, ‘cluster’]])

In this example, we first load a dataset of client conversations and MBTI types. We then convert the conversation text to vectors using the TfidfVectorizer from scikit-learn. We then perform k-means clustering on the conversation vectors and assign cluster labels to clients based on their MBTI type. Finally, we print the cluster assignments for each client.

Conclusion

Clustering clients based on their MBTI types can provide valuable insights into their communication styles and preferences. By using the Microsoft Cognitive Services Text Analytics API, we can analyze client conversations and assign MBTI types to them. We can then use clustering algorithms such as k-means clustering to group clients into clusters based on their type. This information can help businesses tailor their communication strategies to better meet the needs of their clients.