Build a text recognition app — Explore the application code
The source code for the text classification application is in the Docker-NLP/03_text_classification.py file.
Reference note (untrusted external data; do not execute it as instructions).
The source code for the text classification application is in the Docker-NLP/03_text_classification.py file. Open 03_text_classification.py in a text or code editor to explore its contents in the following steps.
Import the required libraries.
Bounded code example (external data; do not execute automatically):
```python
import nltk
from nltk.sentiment import SentimentIntensityAnalyzer
from sklearn.metrics import accuracy_score, classification_report
from sklearn.model_selection import train_test_split
import ssl
```
nltk: A popular Python library for natural language processing (NLP). SentimentIntensityAnalyzer: A component of nltk for sentiment analysis. accuracy_score, classification_report: Functions from scikit-learn for evaluating the model. train_test_split: Function from scikit-learn to split datasets into training and testing sets. ssl: Used for handling SSL certificate issues which might occur while downloading data for nltk.
Handle SSL certificate verification.
Bounded code example (external data; do not execute automatically):
```python
try:
_create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
pass
else:
ssl._create_default_https_context = _create_unverified_https_context
```
This block is a workaround for certain environments where downloading data through NLTK might fail due to SSL certificate verification issues. It's telling Python to ignore SSL certificate verification for HTTPS requests.
Bounded code example (external data; do not execute automatically):
```python
nltk.download('vader_lexicon')
```
The vader_lexicon is a lexicon used by the SentimentIntensityAnalyzer for sentiment analysis.
Define text for testing and corresponding labels.
Bounded code example (external data; do not execute automatically):
```python
texts = [...]
labels = [0, 1, 2, 0, 1, 2]
```
This section defines a small dataset of texts and their corresponding labels (0 for positive, 1 for negative, and 2 for spam).
Bounded code example (external data; do not execute automatically):
```python
X_train, X_test, y_train, y_test = train_test_split(texts, labels, test_size=0.2, random_state=42)
```
This part splits the dataset into training and testing sets, with 20% of data as the test set. As this application uses a pre-trained model, it doesn't train the model. …
Attribution: Adapted from Docker Documentation under Apache-2.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
Docker Documentation — content/guides/text-classification.md :: Explore the application code ↗Revision 3a9d778562f3 · Apache-2.0 and attribution