MCPcopy Create free account
hub / github.com/OpenRaiser/PaperFlow / extract_keywords

Function extract_keywords

skills/paper-processor/scripts/embed.py:99–143  ·  view source on GitHub ↗

Extract keywords Args: text: Text to extract from top_k: Number of keywords to return Returns: List of keywords

(text: str, top_k: int = 5)

Source from the content-addressed store, hash-verified

97
98
99def extract_keywords(text: str, top_k: int = 5) -> List[str]:
100 """
101 Extract keywords
102
103 Args:
104 text: Text to extract from
105 top_k: Number of keywords to return
106
107 Returns:
108 List of keywords
109 """
110 # Tokenize
111 words = re.findall(r'\b\w+\b', text.lower())
112
113 # Remove stop words and short words
114 words = [w for w in words if w not in STOP_WORDS and len(w) > 3]
115
116 # Calculate word frequency
117 word_freq = {}
118 for word in words:
119 word_freq[word] = word_freq.get(word, 0) + 1
120
121 # Match domain keywords
122 all_keywords = set()
123 for keywords in DOMAIN_KEYWORDS.values():
124 all_keywords.update(keywords)
125
126 matched = []
127 for keyword in all_keywords:
128 if keyword in text.lower():
129 matched.append(keyword)
130
131 # Sort by frequency
132 sorted_words = sorted(word_freq.items(), key=lambda x: x[1], reverse=True)
133
134 # Combine results
135 result = matched[:top_k]
136 if len(result) < top_k:
137 for word, _ in sorted_words:
138 if word not in result:
139 result.append(word)
140 if len(result) >= top_k:
141 break
142
143 return result[:top_k]
144
145
146def generate_embedding(text: str, model_name: str = "text-embedding-3-small") -> List[float]:

Callers 2

process_paperFunction · 0.85
embed.pyFile · 0.85

Calls 1

getMethod · 0.80

Tested by

no test coverage detected