Danielchris145/TruthCheck-AI
0
1# models/keyword_extractor.py2import spacy3from collections import Counter4 5class KeywordExtractor:6 def __init__(self): # Corrected __init__7 try:8 self.nlp = spacy.load("en_core_web_sm")9 except OSError:10 print("Please install spaCy English model: python -m spacy download en_core_web_sm")11 raise12 13 def extract_keywords(self, text):14 """Extract keywords and named entities from text"""15 doc = self.nlp(text)16 17 keywords = []18 19 # Extract named entities20 for ent in doc.ents:21 if ent.label_ in ['PERSON', 'ORG', 'GPE', 'PRODUCT', 'EVENT', 'DATE']:22 keywords.append(ent.text)23 24 # Extract noun phrases and important words25 for chunk in doc.noun_chunks:26 if len(chunk.text.split()) <= 3: # Avoid very long phrases27 keywords.append(chunk.text)28 29 # Extract individual important words30 for token in doc:31 if (token.pos_ in ['NOUN', 'PROPN'] and 32 not token.is_stop and 33 not token.is_punct and 34 len(token.text) > 2):35 keywords.append(token.text)36 37 # Remove duplicates and return most common38 keyword_counts = Counter(keywords)39 return [word for word, count in keyword_counts.most_common(10)]40 41 