In the Python programming language, first read from a CSV file containing three columns:
ID
title
content
then read a list of sentences from the 'content' column, and use the small Chinese model in the SpaCy toolkit to process that list, and return the following:
Tokenized sentences
POS tags of each token
dependency trees
NER
Vectors
Display the results as a dataframe using the display() function, and perform PCA on the average vector for each sentence. Use the first two dimensions as axes of a scatter plot.
Assistant
Here is an example code snippet that reads a CSV file, processes the 'content' column using the small Chinese model in SpaCy, and displays the results as a Pandas DataFrame. It also performs PCA on the average vector for each sentence and creates a scatter plot using the first two principal components:
python
User
Annotate the scatter plot with the corresponding 'title' column from the CSV file instead
Assistant
Sure, you can annotate the scatter plot with the corresponding 'title' column from the CSV file instead of the 'sentences' column. Here's the modified code snippet:
python
User
Add the 'content' column to the final dataframe. Remove the two PCA results from the final dataframe.
Assistant
Sure, you can add the 'content' column to the final DataFrame and remove the 'pca1' and 'pca2' columns. Here's the modified code:
python
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
import pandas as pd
import spacy
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
# Load the small Chinese model in SpaCy
nlp = spacy.load('zh_core_web_sm')
# Read the CSV file into a Pandas DataFrame
df = pd.read_csv('data.csv')
# Tokenize sentences, get POS tags, dependency trees, NER, and vectors
sentences = list(df['content'])
docs = list(nlp.pipe(sentences))
tokenized_sentences = [[token.text for token in doc] for doc in docs]
pos_tags = [[token.pos_ for token in doc] for doc in docs]
dependency_trees = [[(token.head.text, token.dep_, token.text) for token in doc] for doc in docs]
ner = [[(ent.text, ent.label_) for ent in doc.ents] for doc in docs]
vectors = [doc.vector for doc in docs]
# Create a new DataFrame with the processed data
new_df = pd.DataFrame({'sentences': sentences,
'tokenized_sentences': tokenized_sentences,
'pos_tags': pos_tags,
'dependency_trees': dependency_trees,
'ner': ner,
'vectors': vectors})
# Perform PCA on the vectors
pca = PCA(n_components=2)
pca_vectors = pca.fit_transform(list(new_df['vectors']))
new_df['pca1'] = pca_vectors[:, 0]
new_df['pca2'] = pca_vectors[:, 1]
# Create a scatter plot using the first two principal components
fig, ax = plt.subplots(figsize=(10, 10))
ax.scatter(new_df['pca1'], new_df['pca2'])
for i, txt in enumerate(new_df['sentences']):
ax.annotate(txt, (new_df['pca1'][i], new_df['pca2'][i]))
plt.show()
# Display the new DataFrame
display(new_df)
You will need to replace 'data.csv' with the filename of your CSV file containing the ID, title, and content columns. You can modify the column names in the DataFrame as desired. The scatter plot will show the sentences plotted using the first two principal components, with the sentence text annotated next to each point.
import pandas as pd
import spacy
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
# Load the small Chinese model in SpaCy
nlp = spacy.load('zh_core_web_sm')
# Read the CSV file into a Pandas DataFrame
df = pd.read_csv('data.csv')
# Tokenize sentences, get POS tags, dependency trees, NER, and vectors
sentences = list(df['content'])
docs = list(nlp.pipe(sentences))
tokenized_sentences = [[token.text for token in doc] for doc in docs]
pos_tags = [[token.pos_ for token in doc] for doc in docs]
dependency_trees = [[(token.head.text, token.dep_, token.text) for token in doc] for doc in docs]
ner = [[(ent.text, ent.label_) for ent in doc.ents] for doc in docs]
vectors = [doc.vector for doc in docs]
# Create a new DataFrame with the processed data
new_df = pd.DataFrame({'title': df['title'],
'tokenized_sentences': tokenized_sentences,
'pos_tags': pos_tags,
'dependency_trees': dependency_trees,
'ner': ner,
'vectors': vectors})
# Perform PCA on the vectors
pca = PCA(n_components=2)
pca_vectors = pca.fit_transform(list(new_df['vectors']))
new_df['pca1'] = pca_vectors[:, 0]
new_df['pca2'] = pca_vectors[:, 1]
# Create a scatter plot using the first two principal components
fig, ax = plt.subplots(figsize=(10, 10))
ax.scatter(new_df['pca1'], new_df['pca2'])
for i, txt in enumerate(new_df['title']):
ax.annotate(txt, (new_df['pca1'][i], new_df['pca2'][i]))
plt.show()
# Display the new DataFrame
display(new_df)
This code is similar to the previous code, except that we create a new DataFrame with the 'title' column instead of the 'sentences' column, and we annotate the scatter plot with the 'title' column instead of the 'sentences' column. The resulting scatter plot will show the titles plotted using the first two principal components, with the title text annotated next to each point.
import pandas as pd
import spacy
import matplotlib.pyplot as plt
# Load the small Chinese model in SpaCy
nlp = spacy.load('zh_core_web_sm')
# Read the CSV file into a Pandas DataFrame
df = pd.read_csv('data.csv')
# Tokenize sentences, get POS tags, dependency trees, NER, and vectors
sentences = list(df['content'])
docs = list(nlp.pipe(sentences))
tokenized_sentences = [[token.text for token in doc] for doc in docs]
pos_tags = [[token.pos_ for token in doc] for doc in docs]
dependency_trees = [[(token.head.text, token.dep_, token.text) for token in doc] for doc in docs]
ner = [[(ent.text, ent.label_) for ent in doc.ents] for doc in docs]
vectors = [doc.vector for doc in docs]
# Create a new DataFrame with the processed data
new_df = pd.DataFrame({'ID': df['ID'],
'title': df['title'],
'content': df['content'],
'tokenized_sentences': tokenized_sentences,
'pos_tags': pos_tags,
'dependency_trees': dependency_trees,
'ner': ner,
'vectors': vectors})
# Remove the 'pca1' and 'pca2' columns
new_df = new_df.drop(['pca1', 'pca2'], axis=1)
# Display the new DataFrame
display(new_df)
This code is similar to the previous code, except that we add the 'content' column to the new DataFrame, and we remove the 'pca1' and 'pca2' columns from the DataFrame. The resulting DataFrame will show the ID, title, content, tokenized sentences, POS tags, dependency trees, NER, and vectors for each row.