-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
77 lines (60 loc) · 2.08 KB
/
Copy pathutils.py
File metadata and controls
77 lines (60 loc) · 2.08 KB
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
"""
Common utility functions
Defines functions commonly used across multiple sections of the paper.
"""
import re
from typing import List, Set
def in_stopwords(string: str, stopwords: Set[str] = None) -> bool:
"""
Check if a specific stopword is contained in the string
Parameters:
-----------
string : str
String to check
stopwords : Set[str], optional
Set of stopwords (default: None, loaded from config)
Returns:
--------
bool : True if stopword is contained, False otherwise
"""
if stopwords is None:
from libmatch.config import STOPWORDS
stopwords = STOPWORDS
if string == 'learning':
return True
# Check stopword inclusion using regex
strings = '|'.join([r'\b\w*' + i + r'\w*\b' for i in stopwords])
pattern = re.compile(strings, re.IGNORECASE)
matches = re.findall(pattern, string)
return len(matches) > 0
def remove_stopwords_from_keywords(keywords: List[str], stopwords: Set[str] = None) -> List[str]:
"""
Remove stopwords from keywords list
Parameters:
-----------
keywords : List[str]
List of keywords
stopwords : Set[str], optional
Set of stopwords (default: None, loaded from config)
Returns:
--------
List[str] : Keywords list with stopwords removed
"""
if stopwords is None:
from libmatch.config import STOPWORDS
stopwords = STOPWORDS
return [word for word in keywords if not in_stopwords(word, stopwords)]
def extract_library_name_from_import(import_statement: str) -> str:
"""
Extract library name from import statement
Parameters:
-----------
import_statement : str
Import statement (e.g., "import numpy", "from sklearn import")
Returns:
--------
str : Top-level package name (e.g., "numpy", "sklearn")
"""
# Replace dots with spaces and extract the first word (top-level package)
library_name = import_statement.replace('.', ' ').split()[1]
return library_name.split('.')[0] # tensorflow.keras -> tensorflow