|
|
|
|
|
|
|
|
import re
|
|
|
import dateparser
|
|
|
|
|
|
|
|
|
PRODUCT_LIST = [
|
|
|
"productA", "productB", "productC", "laptop", "phone", "router", "headphones"
|
|
|
]
|
|
|
|
|
|
|
|
|
COMPLAINT_KEYWORDS = [
|
|
|
"broken", "late", "error", "delay", "fault", "not working", "slow", "missing", "haven’t received"
|
|
|
]
|
|
|
|
|
|
def extract_entities(text):
|
|
|
"""
|
|
|
Extracts products, dates, and complaint keywords from the input text.
|
|
|
|
|
|
Args:
|
|
|
text (str): Customer support ticket text.
|
|
|
|
|
|
Returns:
|
|
|
dict: Dictionary with lists of extracted 'products', 'dates', and 'complaints'.
|
|
|
"""
|
|
|
text_lower = text.lower()
|
|
|
|
|
|
|
|
|
products_found = [p for p in PRODUCT_LIST if p.lower() in text_lower]
|
|
|
|
|
|
|
|
|
date_phrases = re.findall(
|
|
|
r'\b(?:last week|yesterday|today|on \w+ \d{1,2}|\d{2}/\d{2}/\d{4})\b',
|
|
|
text_lower
|
|
|
)
|
|
|
|
|
|
dates_found = [d for d in date_phrases if dateparser.parse(d)]
|
|
|
|
|
|
|
|
|
complaints_found = [word for word in COMPLAINT_KEYWORDS if word in text_lower]
|
|
|
|
|
|
return {
|
|
|
'products': products_found,
|
|
|
'dates': dates_found,
|
|
|
'complaints': complaints_found
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
sample_text = (
|
|
|
"I ordered a laptop last week but still haven’t received it. "
|
|
|
"This delay is frustrating and I need help."
|
|
|
)
|
|
|
entities = extract_entities(sample_text)
|
|
|
print("Extracted Entities:", entities)
|
|
|
|