In the world of natural language processing (NLP), the dream has always been to create systems that can understand, analyze, and generate human-like text. The good news is we are getting closer to this aspiration, thanks to innovations in artificial intelligence, particularly through platforms like OpenAI.
In this article, we’ll explore text analysis with OpenAI, exploring the methodologies, tools, and metrics utilized to decipher the complex tapestry of human language. Let’s get started.
Fundamentals of Text Analysis Metrics with OpenAI
1.Semantic Similarity
OpenAI uses smart methods to determine whether two pieces of text mean the same thing, even if they’re worded differently. It measures this with things like “cosine similarity,” which is a fancy way of saying how much alike two texts are.
2.Sentiment Analysis
Understanding whether a piece of writing is happy, sad, or neutral is important. OpenAI has tools that learn from many examples to tell the feeling behind a piece of writing. They measure how good they are with things like “accuracy,” which tells how often they’re right.
3.Named Entity Recognition (NER)
NER involves identifying and classifying entities mentioned in a text, such as the names of people, organizations, locations, and more. Students learn from lots of examples and get better over time. They measure their progress with things like “precision” and “recall,” which tell them if they’re getting all the names right.
4.Text Summarization
OpenAI can summarize large writing pieces into short versions while keeping the important parts. They use a method called “ROUGE,” which measures how much the short version resembles the original.
5.Topic Modeling
OpenAI employs topic modeling techniques to discover the underlying themes or topics in a collection of documents. Algorithms such as Latent Dirichlet Allocation (LDA) or more advanced neural network-based approaches are utilized.
6.Text Classification
Their models are adept at classifying text into predefined categories or labels. This may involve sentiment classification, topic categorization, or intent detection. The processes may also include metrics such as accuracy, precision, recall, and F1-score, which evaluate the performance of text classification models.
7.Language Generation and Understanding
OpenAI can both write like a human and understand what humans write. They use a way of measuring called “perplexity” to see how good they are at this.
A Guide for Analyzing user comments using OpenAI within a Node.js
Understanding user opinions about products or services is important for businesses today. This can help you to gain valuable insights and make better business decisions. Below are a few steps showing how to analyze user comments using OpenAI in a Node.js environment.
Step 1. Create an account on the OpenAI portal and generate an API key. Here is the link to OpenAI official documentation.
Step 2. Set API key to ENV variable. Here is the link to OpenAI official documentation. For macOS, these are the steps:
- Open Terminal.
- Edit bash profile: Use the command nano ~/.bash_profile or nano ~/.zshrc (for newer MacOS versions) to open the profile file in a text editor.
- Add Environment Variable: In the editor, add the line below, replacing your-api-key-here with your actual API key: export OPENAI_API_KEY=’your-api-key-here’
- Save and exit.
- Load your profile: Use the command source ~/.bash_profile or source ~/.zshrc to load the updated profile.
- Verification: Verify the setup by typing echo $OPENAI_API_KEY in the terminal. It should display your API key.
Step 3: Install openai package: npm i openai
Step 4: Initialize the openAI with your API key:
import OpenAI from "openai"; const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
Step 5. Create a function that will make a request to OpenAI to analyze comments:
async getAiModelAnswer(comment: string) { const prompt: string = ` Please analyze user comments. Your response should contain only the JSON and no other words. Please provide the following information in the JSON response: toneOfVoice (Positive/Negative/Ambiguous/Neutral), tonePhrases (parts for the text that reflect tone of voice which can be later highlighted for the user. It should return all phrases that you used to recognize the tone of voice), inappropriateLanguageDetected (true/false), inappropriateWords (list of detected inappropriate words, if any, only include if inappropriateLanguageDetected flag is true), tags (max of 1 to 3 generalized tags that represents what this text is about), emotion (Emotion category based on the user's tone), topic (1-3 words representing the main topic of the comment) intentRecognition (1-3 words) Here is the comment to analyze: '${comment}'`; const chatCompletion = await openai.chat.completions.create({ messages: [{ role: "user", content: prompt }], model: 'gpt-3.5-turbo', temperature: 1 }); const result = chatCompletion.choices[0].message.content; return result; }
In our prompt, we ask the model to return a result as a JSON string. Therefore, the response will contain only the JSON with all the properties we mentioned in our prompt.
Step 6. Parse the response into your class if needed. Let’s create a class called CommentDetails:
export class CommentDetails { toneOfVoice: string; tonePhrases: string[]; inappropriateLanguageDetected: boolean; inappropriateWords: string[]; tags: string[]; emotion: string; topic: string; intentRecognition: string; }
Now we can create a method that will make a request to OpenAI and will return comment details as JSON:
async getCommentAnalysisDetails(comment: string): Promise<CommentDetails> { const result = await getAiModelAnswer(comment); const commentDetails: CommentDetails = JSON.parse(result); return commentDetails; }
Example:
Let’s run our code and try to use positive and negative comments and how OpenAI will handle it:
Comment 1:
“I just finished reading this book and can’t stop raving about it! The characters were so vivid, and the storyline kept me hooked from start to finish. I couldn’t put it down! Definitely a must-read for anyone looking for a captivating story with a powerful message.”
Result:
{ "toneOfVoice": "Positive", "tonePhrases": [ "can't stop raving about it", "characters were so vivid", "storyline kept me hooked", "couldn't put it down", "Definitely a must-read" ], "inappropriateLanguageDetected": false, "inappropriateWords": [], "tags": [ "book review", "positivity", "recommendation" ], "emotion": "Excitement", "topic": "Book Review", "intentRecognition": "Praise, Recommendation", }
Comment 2:
“I had a terrible experience at this restaurant. The service was incredibly slow, and the staff seemed indifferent to our needs. The food was overcooked and tasteless, and to top it off, the atmosphere was unpleasant. I definitely won’t be returning anytime soon.”
Result:
{ "toneOfVoice": "Negative", "tonePhrases": [ "terrible experience", "incredibly slow", "indifferent to our needs", "overcooked and tasteless", "unpleasant", "won't be returning" ], "inappropriateLanguageDetected": false, "inappropriateWords": [], "tags": [ "restaurant", "dining", "experience" ], "emotion": "Displeasure", "topic": "Restaurant experience", "intentRecognition": "Negative feedback", }
The following is an example of how this comment analysis can be represented on a webpage. It is a simple webpage based on React. Tone-of-voice phrases are highlighted for better visual representation.
Feel free to create your own visuals for the provided JSON response; your imagination is the only limit.
Conclusion
OpenAI’s suite of tools and models has revolutionized text analysis, enabling various applications across various domains. They have achieved this by leveraging state-of-the-art techniques and evaluation metrics. Moreover, they continue to push the boundaries of what’s possible in understanding and generating natural language text. Learn from these industry leaders to maintain a competitive edge in the market.