Need assitance on python script to evalaute text
Budget: ₹600 – ₹1,500 INR
I am trying to run a project where I can evaluate the user stories - Source of user story will be Excel or CSV and result in excel.
I want someone who can help me to complete my code :
import pandas as pd
import re
import nltk
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
nltk.download('punkt')
# Sample data: Add your own user stories and label them (1=Good, 0=Poor)
data = {
'user_story': [
"As a sales rep, I want to view leads assigned to me so that I can follow up quickly.",
"Need login feature.",
"As an admin, I want to generate monthly reports to analyze user engagement.",
"Create dashboard.",
"As a customer, I want to reset my password so that I can access my account."
],
'label': [1, 0, 1, 0, 1]
}
df = pd.DataFrame(data)
# NLP Preprocessing
def preprocess(text):
text = text.lower()
text = re.sub(r'[^a-z\s]', '', text)
return text
df['clean_story'] = df['user_story'].apply(preprocess)
# Feature engineering based on presence of structure: "As a <role>, I want <action> so that <benefit>"
def has_role_action_benefit(text):
role = re.search(r"as a [\w\s]+,", text)
action = re.search(r"i want to? [\w\s]+", text)
benefit = re.search(r"so that [\w\s]+", text)
return int(bool(role and action and benefit))
df['structure_score'] = df['clean_story'].apply(has_role_action_benefit)
# Use simple bag-of-words + structure score
vectorizer = CountVectorizer()
X_bow = vectorizer.fit_transform(df['clean_story'])
# Combine features
import numpy as np
X = np.hstack((X_bow.toarray(), df[['structure_score']].values))
y = df['label']
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Train model
model = RandomForestClassifier()
model.fit(X_train, y_train)
# Evaluate
y_pred = model.predict(X_test)
print(classification_report(y_test, y_pred))
# Predict on new user stories
def evaluate_user_story(story):
story_clean = preprocess(story)
bow = vectorizer.transform([story_clean])
struct_score = has_role_action_benefit(story_clean)
features = np.hstack((bow.toarray(), [[struct_score]]))
prediction = model.predict(features)
return "Good" if prediction[0] == 1 else "Poor"
# Try with a new story
new_story = "As a manager, I want to approve time-off requests so that team scheduling stays accurate."
print(f"Evaluation: {evaluate_user_story(new_story)}")
I want someone who can help me to complete my code :
import pandas as pd
import re
import nltk
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
nltk.download('punkt')
# Sample data: Add your own user stories and label them (1=Good, 0=Poor)
data = {
'user_story': [
"As a sales rep, I want to view leads assigned to me so that I can follow up quickly.",
"Need login feature.",
"As an admin, I want to generate monthly reports to analyze user engagement.",
"Create dashboard.",
"As a customer, I want to reset my password so that I can access my account."
],
'label': [1, 0, 1, 0, 1]
}
df = pd.DataFrame(data)
# NLP Preprocessing
def preprocess(text):
text = text.lower()
text = re.sub(r'[^a-z\s]', '', text)
return text
df['clean_story'] = df['user_story'].apply(preprocess)
# Feature engineering based on presence of structure: "As a <role>, I want <action> so that <benefit>"
def has_role_action_benefit(text):
role = re.search(r"as a [\w\s]+,", text)
action = re.search(r"i want to? [\w\s]+", text)
benefit = re.search(r"so that [\w\s]+", text)
return int(bool(role and action and benefit))
df['structure_score'] = df['clean_story'].apply(has_role_action_benefit)
# Use simple bag-of-words + structure score
vectorizer = CountVectorizer()
X_bow = vectorizer.fit_transform(df['clean_story'])
# Combine features
import numpy as np
X = np.hstack((X_bow.toarray(), df[['structure_score']].values))
y = df['label']
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Train model
model = RandomForestClassifier()
model.fit(X_train, y_train)
# Evaluate
y_pred = model.predict(X_test)
print(classification_report(y_test, y_pred))
# Predict on new user stories
def evaluate_user_story(story):
story_clean = preprocess(story)
bow = vectorizer.transform([story_clean])
struct_score = has_role_action_benefit(story_clean)
features = np.hstack((bow.toarray(), [[struct_score]]))
prediction = model.predict(features)
return "Good" if prediction[0] == 1 else "Poor"
# Try with a new story
new_story = "As a manager, I want to approve time-off requests so that team scheduling stays accurate."
print(f"Evaluation: {evaluate_user_story(new_story)}")