File size: 6,496 Bytes
3a5fdfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
"""
DocMind - Utility Functions
Helper functions for the multi-agent system
"""

from typing import List, Dict
import re
from datetime import datetime


def clean_text(text: str) -> str:
    """Clean and normalize text"""
    # Remove extra whitespace
    text = re.sub(r'\s+', ' ', text)
    # Remove special characters but keep basic punctuation
    text = re.sub(r'[^\w\s.,!?;:()\-]', '', text)
    return text.strip()


def truncate_text(text: str, max_length: int = 500) -> str:
    """Truncate text to maximum length, ending at sentence boundary"""
    if len(text) <= max_length:
        return text

    # Find last sentence boundary before max_length
    truncated = text[:max_length]
    last_period = truncated.rfind('.')

    if last_period > 0:
        return truncated[:last_period + 1]
    return truncated + "..."


def format_authors(authors: List[str], max_authors: int = 3) -> str:
    """Format author list for display"""
    if len(authors) <= max_authors:
        return ", ".join(authors)
    else:
        return ", ".join(authors[:max_authors]) + " et al."


def extract_year(date_string: str) -> int:
    """Extract year from date string"""
    try:
        if isinstance(date_string, str):
            return int(date_string[:4])
        return datetime.now().year
    except:
        return datetime.now().year


def score_recency(year: int, current_year: int = None) -> float:
    """
    Score paper based on recency

    Returns:
        Score from 0-1, where 1 is most recent
    """
    if current_year is None:
        current_year = datetime.now().year

    age = current_year - year
    if age <= 0:
        return 1.0
    elif age <= 1:
        return 0.9
    elif age <= 2:
        return 0.7
    elif age <= 3:
        return 0.5
    else:
        return max(0.3, 1.0 / (age + 1))


def combine_scores(
        relevance: float,
        recency: float,
        quality: float,
        weights: Dict[str, float] = None
) -> float:
    """
    Combine multiple scores with weights

    Args:
        relevance: Relevance score (0-1)
        recency: Recency score (0-1)
        quality: Quality score (0-1)
        weights: Dict with keys 'relevance', 'recency', 'quality'

    Returns:
        Combined score (0-1)
    """
    if weights is None:
        weights = {
            'relevance': 0.6,
            'recency': 0.2,
            'quality': 0.2
        }

    return (
            relevance * weights['relevance'] +
            recency * weights['recency'] +
            quality * weights['quality']
    )


def deduplicate_papers(papers: List[Dict]) -> List[Dict]:
    """Remove duplicate papers based on arXiv ID"""
    seen = set()
    unique = []

    for paper in papers:
        paper_id = paper.get('arxiv_id', '')
        if paper_id and paper_id not in seen:
            seen.add(paper_id)
            unique.append(paper)

    return unique


def format_citation(paper: Dict, style: str = 'apa') -> str:
    """
    Format paper citation

    Args:
        paper: Paper dict with title, authors, year, arxiv_id
        style: Citation style ('apa', 'simple', 'markdown')

    Returns:
        Formatted citation string
    """
    authors = format_authors(paper.get('authors', []))
    title = paper.get('title', 'Unknown Title')
    year = extract_year(paper.get('published', ''))
    arxiv_id = paper.get('arxiv_id', '')

    if style == 'apa':
        return f"{authors} ({year}). {title}. arXiv:{arxiv_id}"

    elif style == 'markdown':
        return f"**{title}** - {authors} ({year}) - arXiv:[{arxiv_id}](https://arxiv.org/abs/{arxiv_id})"

    else:  # simple
        return f"{title} ({arxiv_id}, {year})"


def extract_keywords(text: str, top_n: int = 5) -> List[str]:
    """
    Extract simple keywords from text (frequency-based)

    Args:
        text: Input text
        top_n: Number of keywords to return

    Returns:
        List of top keywords
    """
    # Simple word frequency approach
    # Remove common words
    stop_words = {
        'the', 'a', 'an', 'and', 'or', 'but', 'in', 'on', 'at', 'to', 'for',
        'of', 'with', 'by', 'from', 'is', 'are', 'was', 'were', 'be', 'been',
        'this', 'that', 'these', 'those', 'we', 'our', 'propose', 'show'
    }

    # Tokenize and count
    words = re.findall(r'\b[a-z]{4,}\b', text.lower())
    word_freq = {}

    for word in words:
        if word not in stop_words:
            word_freq[word] = word_freq.get(word, 0) + 1

    # Get top N
    sorted_words = sorted(word_freq.items(), key=lambda x: x[1], reverse=True)
    return [word for word, freq in sorted_words[:top_n]]


class ProgressTracker:
    """Simple progress tracker for multi-step processes"""

    def __init__(self, total_steps: int):
        self.total_steps = total_steps
        self.current_step = 0
        self.step_names = []

    def next_step(self, step_name: str = None):
        """Move to next step"""
        self.current_step += 1
        if step_name:
            self.step_names.append(step_name)

    def get_progress(self) -> float:
        """Get progress as percentage"""
        return (self.current_step / self.total_steps) * 100

    def get_status(self) -> str:
        """Get status string"""
        return f"Step {self.current_step}/{self.total_steps} ({self.get_progress():.1f}%)"


def validate_paper_dict(paper: Dict) -> bool:
    """Validate that paper dictionary has required fields"""
    required_fields = ['title', 'abstract', 'arxiv_id', 'authors', 'published']
    return all(field in paper for field in required_fields)


def safe_get(dictionary: Dict, key: str, default=None):
    """Safely get value from dictionary with fallback"""
    try:
        return dictionary.get(key, default)
    except:
        return default


# Example usage
if __name__ == "__main__":
    # Test utilities
    sample_paper = {
        'title': 'Attention Is All You Need',
        'authors': ['Vaswani', 'Shazeer', 'Parmar', 'Uszkoreit'],
        'published': '2017-06-12',
        'arxiv_id': '1706.03762',
        'abstract': 'The dominant sequence transduction models are based on complex recurrent or convolutional neural networks...'
    }

    print("Citation (APA):", format_citation(sample_paper, 'apa'))
    print("Citation (Markdown):", format_citation(sample_paper, 'markdown'))
    print("Authors:", format_authors(sample_paper['authors']))
    print("Recency score:", score_recency(2017))
    print("Keywords:", extract_keywords(sample_paper['abstract']))