benchmarks.wiki / Public workspace

BigCodeBench / BigCodeBench v0.1.0_hf a24735db-c917-5418-b479-f3f69301165e

Problem

Answer published by the source. Consult the official source to check your work against its answer.

complete prompt

# Importing the required libraries
import re
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
from nltk.corpus import stopwords


def task_func(text, n=2):
    """
    Analyzes a text string, removing duplicate consecutive words and stopwords defined by nltk.corpus,
    generates a square co-occurrence matrix of words, and plots this matrix.

    Parameters:
    - text (str): Input text to be analyzed.
    - n (int, optional): Size of n-grams for the co-occurrence matrix. Defaults to 2.

    Returns:
    - tuple:
        - pd.DataFrame: Square co-occurrence matrix of words.
        - matplotlib.axes.Axes: Plot object of the co-occurrence matrix.

    Requirements:
        - re
        - pandas
        - matplotlib.pyplot
        - numpy
        - sklearn.feature_extraction.text
        - nltk.corpus

    Example:
    >>> import matplotlib
    >>> text = "hello hello world world"
    >>> df, ax = task_func(text, n=2)
    >>> df.columns.tolist()
    ['hello world']
    >>> df.index.tolist()
    ['hello world']
    >>> df.iloc[0, 0]
    0
    >>> isinstance(ax, matplotlib.axes.Axes)
    True
    """

instruct prompt

Analyzes a text string, removing duplicate consecutive words and stopwords defined by nltk.corpus, generates a square co-occurrence matrix of words, and plots this matrix.
The function should output with:
    tuple:
    pd.DataFrame: Square co-occurrence matrix of words.
    matplotlib.axes.Axes: Plot object of the co-occurrence matrix.
You should write self-contained code starting with:

Code

# Importing the required libraries
import re
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
from nltk.corpus import stopwords
def task_func(text, n=2):

code prompt

Code

# Importing the required libraries
import re
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
from nltk.corpus import stopwords
def task_func(text, n=2):

entry point

task_func

libs

  • nltk
  • pandas
  • matplotlib
  • numpy
  • sklearn
  • re

Discussion

Discussion

No discussion posts on this page yet. Share a minimal failing example, an algorithm with its complexity, or a reproducible command and result. Use the posting template.

See answer Answer published by the source

Artifacts

Code, notes and reproducible work shared by participants. Files are served from a separate origin.

No artifacts on this page yet. Share reproducible code or notes in a contribution. Share a minimal failing example, an algorithm with its complexity, or a reproducible command and result. Use the posting template.

Source and history

Official source

initial import