import os

import nltk
from rake_nltk import Rake

class StructureAnalyzer(object):
    def __init__(self, config=None):
        self.config = config
        self.keywords = []
        self.extractor = Rake(min_length=1, max_length=3)
        

    def extract_keywords(self, text):
        
          #change min/max length of the keywords. 
        self.extractor.extract_keywords_from_text(text)
        keywords = self.extractor.get_ranked_phrases()
        
        # Optional: POS tagging to keep only nouns and verbs
        # pos_tags = nltk.pos_tag(keywords)
        # keywords = [word for word, pos in pos_tags if pos.startswith('NN') or pos.startswith('VB')]
        
        return keywords

    def extract_keywords_combined(text, top_n=10):
        """
        Combined approach: Use POS tagging for filtering and frequency for ranking
        This is often the most effective method
        """
        try:
            lemmatizer = WordNetLemmatizer()
            
            # Tokenize
            tokens = word_tokenize(text.lower())
            
            # POS tagging
            pos_tags = pos_tag(tokens)
            
            # Filter for important POS tags and lemmatize
            stop_words = set(stopwords.words('english'))
            keywords = [
                lemmatizer.lemmatize(word) for word, pos in pos_tags 
                if (pos.startswith('NN') or pos.startswith('JJ') or pos.startswith('VB')) 
                and word.isalnum() 
                and word not in stop_words
                and len(word) > 2  # Filter out very short words
            ]
            
            # Count frequency
            keyword_freq = Counter(keywords)

            self.keywords =keyword_freq.most_common(top_n)
            
            return keyword_freq.most_common(top_n)
        except Exception as e:
            print(f"Error in combined extraction: {e}")
            return []