pyBigKinds 1.2.11__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pybigkinds-1.2.11/LICENSE +21 -0
- pybigkinds-1.2.11/PKG-INFO +79 -0
- pybigkinds-1.2.11/README.md +51 -0
- pybigkinds-1.2.11/pyBigKinds/__init__.py +38 -0
- pybigkinds-1.2.11/pyBigKinds/_version.py +1 -0
- pybigkinds-1.2.11/pyBigKinds/base.py +152 -0
- pybigkinds-1.2.11/pyBigKinds/preprocessing.py +159 -0
- pybigkinds-1.2.11/pyBigKinds/representation.py +321 -0
- pybigkinds-1.2.11/pyBigKinds/visualization.py +166 -0
- pybigkinds-1.2.11/pyBigKinds.egg-info/PKG-INFO +79 -0
- pybigkinds-1.2.11/pyBigKinds.egg-info/SOURCES.txt +20 -0
- pybigkinds-1.2.11/pyBigKinds.egg-info/dependency_links.txt +1 -0
- pybigkinds-1.2.11/pyBigKinds.egg-info/requires.txt +15 -0
- pybigkinds-1.2.11/pyBigKinds.egg-info/top_level.txt +1 -0
- pybigkinds-1.2.11/pyproject.toml +52 -0
- pybigkinds-1.2.11/setup.cfg +15 -0
- pybigkinds-1.2.11/setup.py +4 -0
- pybigkinds-1.2.11/test/test_base.py +52 -0
- pybigkinds-1.2.11/test/test_preprocessing.py +48 -0
- pybigkinds-1.2.11/test/test_representation.py +95 -0
- pybigkinds-1.2.11/test/test_visualization.py +26 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Jaeseong Choe
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pyBigKinds
|
|
3
|
+
Version: 1.2.11
|
|
4
|
+
Summary: BigKinds Data Analysis Toolkit for python
|
|
5
|
+
Author: Sorrychoe
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Source Code, https://github.com/sorrychoe/pyBigKinds
|
|
8
|
+
Project-URL: Bug Tracker, https://github.com/sorrychoe/pyBigKinds/issues
|
|
9
|
+
Keywords: Journalism,Preprocessing-data,Text-mining,BigKinds
|
|
10
|
+
Requires-Python: >=3.10
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Requires-Dist: matplotlib>=3.5.3
|
|
14
|
+
Requires-Dist: pandas<3,>=2
|
|
15
|
+
Requires-Dist: wordcloud>=1.8.2.2
|
|
16
|
+
Requires-Dist: scikit-learn<2,>=1.0.0
|
|
17
|
+
Requires-Dist: tomotopy>=0.14.0
|
|
18
|
+
Requires-Dist: openpyxl>=3.1.2
|
|
19
|
+
Requires-Dist: mlxtend<0.24,>=0.22.0
|
|
20
|
+
Provides-Extra: dev
|
|
21
|
+
Requires-Dist: build>=0.7; extra == "dev"
|
|
22
|
+
Requires-Dist: flake8>=3.9; extra == "dev"
|
|
23
|
+
Requires-Dist: isort>=5.7; extra == "dev"
|
|
24
|
+
Requires-Dist: pre-commit>=3; extra == "dev"
|
|
25
|
+
Requires-Dist: pylint>=3.2.0; extra == "dev"
|
|
26
|
+
Requires-Dist: pytest>=7; extra == "dev"
|
|
27
|
+
Dynamic: license-file
|
|
28
|
+
|
|
29
|
+
# pyBigKinds
|
|
30
|
+
|
|
31
|
+
pyBigKinds는 한국 언론의 빅데이터 저장소인 BigKinds에서 추출한 데이터를 low-Code로 분석할 수 있게 만든 툴입니다.
|
|
32
|
+
|
|
33
|
+
## Requirements
|
|
34
|
+
|
|
35
|
+
- Python >= 3.10
|
|
36
|
+
- 한글 폰트: Windows(맑은 고딕)와 macOS(AppleGothic)는 기본 폰트를 사용합니다.
|
|
37
|
+
Linux에서는 `NanumGothic`(예: `sudo apt-get install fonts-nanum`) 등 한글 폰트를
|
|
38
|
+
설치해야 워드클라우드와 그래프의 한글이 정상 출력됩니다.
|
|
39
|
+
|
|
40
|
+
## Installation
|
|
41
|
+
|
|
42
|
+
- macOS
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
python3 -m pip install pyBigKinds
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
- Windows, linux
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
python -m pip install pyBigKinds
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Usage
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
import pyBigKinds as bk
|
|
58
|
+
|
|
59
|
+
bk.press_counter(df)
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+

|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
import pyBigKinds as bk
|
|
66
|
+
|
|
67
|
+
bk.keywords_wordcloud(df, "중앙일보")
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+

|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
## License
|
|
74
|
+
|
|
75
|
+
[MIT](https://choosealicense.com/licenses/mit/)
|
|
76
|
+
|
|
77
|
+
## you have some issue?
|
|
78
|
+
|
|
79
|
+
사용 중 문제 발생 시, 해당 Repo issue에 등록해주세요.
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# pyBigKinds
|
|
2
|
+
|
|
3
|
+
pyBigKinds는 한국 언론의 빅데이터 저장소인 BigKinds에서 추출한 데이터를 low-Code로 분석할 수 있게 만든 툴입니다.
|
|
4
|
+
|
|
5
|
+
## Requirements
|
|
6
|
+
|
|
7
|
+
- Python >= 3.10
|
|
8
|
+
- 한글 폰트: Windows(맑은 고딕)와 macOS(AppleGothic)는 기본 폰트를 사용합니다.
|
|
9
|
+
Linux에서는 `NanumGothic`(예: `sudo apt-get install fonts-nanum`) 등 한글 폰트를
|
|
10
|
+
설치해야 워드클라우드와 그래프의 한글이 정상 출력됩니다.
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
- macOS
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
python3 -m pip install pyBigKinds
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
- Windows, linux
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
python -m pip install pyBigKinds
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
import pyBigKinds as bk
|
|
30
|
+
|
|
31
|
+
bk.press_counter(df)
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+

|
|
35
|
+
|
|
36
|
+
```python
|
|
37
|
+
import pyBigKinds as bk
|
|
38
|
+
|
|
39
|
+
bk.keywords_wordcloud(df, "중앙일보")
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+

|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
## License
|
|
46
|
+
|
|
47
|
+
[MIT](https://choosealicense.com/licenses/mit/)
|
|
48
|
+
|
|
49
|
+
## you have some issue?
|
|
50
|
+
|
|
51
|
+
사용 중 문제 발생 시, 해당 Repo issue에 등록해주세요.
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"""
|
|
2
|
+
BigKindsParser: Exploratory data analysis Toolkit of Python for BigKinds Data
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from . import _version, base, preprocessing, representation, visualization
|
|
6
|
+
from ._version import __version__
|
|
7
|
+
from .base import *
|
|
8
|
+
from .preprocessing import *
|
|
9
|
+
from .representation import *
|
|
10
|
+
from .visualization import *
|
|
11
|
+
|
|
12
|
+
__all__ = [
|
|
13
|
+
"press_counter",
|
|
14
|
+
"header_remover",
|
|
15
|
+
"day_range",
|
|
16
|
+
"counter_to_dataframe",
|
|
17
|
+
"duplication_remover",
|
|
18
|
+
"keyword_parser",
|
|
19
|
+
"keyword_list",
|
|
20
|
+
"word_counter",
|
|
21
|
+
"keywords_wordcloud",
|
|
22
|
+
"top_words",
|
|
23
|
+
"scatterplot",
|
|
24
|
+
"keyword_dataframe",
|
|
25
|
+
"keyword_dataframe_no_duplicated",
|
|
26
|
+
"tfidf",
|
|
27
|
+
"tfidf_vector",
|
|
28
|
+
"normalize_vector",
|
|
29
|
+
"pca",
|
|
30
|
+
"nmf",
|
|
31
|
+
"t_sne",
|
|
32
|
+
"lsa",
|
|
33
|
+
"kmeans",
|
|
34
|
+
"dbscan",
|
|
35
|
+
"meanshift",
|
|
36
|
+
"lda",
|
|
37
|
+
"association",
|
|
38
|
+
]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "1.2.11"
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
# pylint: disable=C0301
|
|
2
|
+
|
|
3
|
+
import pandas as pd
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def header_remover(df):
|
|
7
|
+
"""
|
|
8
|
+
Removes any text enclosed in square brackets ([]) from the '제목' (title) column of a DataFrame or list.
|
|
9
|
+
|
|
10
|
+
Parameters:
|
|
11
|
+
df (pandas.DataFrame or list): The input DataFrame or list containing a column or text data where headers (enclosed in square brackets) need to be removed.
|
|
12
|
+
|
|
13
|
+
Returns:
|
|
14
|
+
pandas.Series or list: A pandas Series or list with the cleaned text where square bracketed sections are removed.
|
|
15
|
+
|
|
16
|
+
Raises:
|
|
17
|
+
TypeError: If the input is not a pandas DataFrame or list.
|
|
18
|
+
"""
|
|
19
|
+
if isinstance(df, pd.DataFrame):
|
|
20
|
+
ans = df["제목"].str.replace(r"\[[^\]]*\]", "", regex=True).str.strip()
|
|
21
|
+
elif isinstance(df, list):
|
|
22
|
+
ans = pd.Series(df).str.replace(r"\[[^\]]*\]", "", regex=True).str.strip().tolist()
|
|
23
|
+
else:
|
|
24
|
+
raise TypeError("input value is to be have to list or DataFrame")
|
|
25
|
+
return ans
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def keyword_list(df):
|
|
29
|
+
"""
|
|
30
|
+
Converts the '키워드' column of a DataFrame to a list or returns a list as-is if the input is already a list or Series.
|
|
31
|
+
|
|
32
|
+
Parameters:
|
|
33
|
+
df (pandas.DataFrame or pandas.Series or list): The input data containing keywords.
|
|
34
|
+
|
|
35
|
+
Returns:
|
|
36
|
+
list: A list of keywords.
|
|
37
|
+
|
|
38
|
+
Raises:
|
|
39
|
+
TypeError: If the input is not a pandas DataFrame, Series, or list.
|
|
40
|
+
"""
|
|
41
|
+
if isinstance(df, pd.DataFrame):
|
|
42
|
+
return df["키워드"].values.tolist()
|
|
43
|
+
elif isinstance(df, pd.Series):
|
|
44
|
+
return df.values.tolist()
|
|
45
|
+
elif isinstance(df, list):
|
|
46
|
+
return df
|
|
47
|
+
else:
|
|
48
|
+
raise TypeError("input value is to be have to list, Series or DataFrame")
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def keyword_parser(text_list):
|
|
52
|
+
"""
|
|
53
|
+
Parses and splits a list of keywords into individual words by separating them based on commas.
|
|
54
|
+
|
|
55
|
+
Parameters:
|
|
56
|
+
text_list (list): A list of strings where each string contains keywords separated by commas.
|
|
57
|
+
|
|
58
|
+
Returns:
|
|
59
|
+
list of lists: A list where each element is a sublist of keywords split from the original list of strings.
|
|
60
|
+
|
|
61
|
+
Raises:
|
|
62
|
+
TypeError: If the input is not a list.
|
|
63
|
+
ValueError: If any element in the list is not a valid string format.
|
|
64
|
+
"""
|
|
65
|
+
if isinstance(text_list, list):
|
|
66
|
+
news_key = []
|
|
67
|
+
for word in text_list:
|
|
68
|
+
if isinstance(word, str):
|
|
69
|
+
word = word.split(",")
|
|
70
|
+
news_key.append(word)
|
|
71
|
+
else:
|
|
72
|
+
raise ValueError("input list is not valid format")
|
|
73
|
+
return news_key
|
|
74
|
+
else:
|
|
75
|
+
raise TypeError("input type is to be have to list")
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def duplication_remover(news_key):
|
|
79
|
+
"""
|
|
80
|
+
Removes duplicate keywords from a list of lists.
|
|
81
|
+
|
|
82
|
+
Parameters:
|
|
83
|
+
news_key (list): A list where each element is a list of keywords.
|
|
84
|
+
|
|
85
|
+
Returns:
|
|
86
|
+
list of lists: A list where duplicate keywords within each sublist have been removed.
|
|
87
|
+
|
|
88
|
+
Raises:
|
|
89
|
+
TypeError: If the input is not a list of lists.
|
|
90
|
+
ValueError: If any element in the list is not a valid sublist.
|
|
91
|
+
"""
|
|
92
|
+
if isinstance(news_key, list):
|
|
93
|
+
news_value = []
|
|
94
|
+
for j in news_key:
|
|
95
|
+
if isinstance(j, list):
|
|
96
|
+
j = list(dict.fromkeys(j))
|
|
97
|
+
news_value.append(j)
|
|
98
|
+
else:
|
|
99
|
+
raise ValueError("input list is not valid format")
|
|
100
|
+
return news_value
|
|
101
|
+
else:
|
|
102
|
+
raise TypeError("input type is to be have to list")
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def word_counter(news_value):
|
|
106
|
+
"""
|
|
107
|
+
Counts the occurrence of each keyword from a list of lists of keywords.
|
|
108
|
+
|
|
109
|
+
Parameters:
|
|
110
|
+
news_value (list): A list where each element is a list of keywords.
|
|
111
|
+
|
|
112
|
+
Returns:
|
|
113
|
+
dict: A dictionary where each key is a keyword and each value is the frequency of that keyword.
|
|
114
|
+
|
|
115
|
+
Raises:
|
|
116
|
+
TypeError: If the input is not a list of lists.
|
|
117
|
+
"""
|
|
118
|
+
if isinstance(news_value, list):
|
|
119
|
+
key_words = {}
|
|
120
|
+
for k in range(len(news_value)):
|
|
121
|
+
for i in news_value[k]:
|
|
122
|
+
if i not in key_words:
|
|
123
|
+
key_words[i] = 1
|
|
124
|
+
elif i in key_words:
|
|
125
|
+
key_words[i] += 1
|
|
126
|
+
return key_words
|
|
127
|
+
else:
|
|
128
|
+
raise TypeError("input type is to be have to list")
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def counter_to_dataframe(key_words):
|
|
132
|
+
"""
|
|
133
|
+
Converts a dictionary of keyword counts into a sorted pandas DataFrame.
|
|
134
|
+
|
|
135
|
+
Parameters:
|
|
136
|
+
key_words (dict): A dictionary where each key is a keyword and each value is the frequency of that keyword.
|
|
137
|
+
|
|
138
|
+
Returns:
|
|
139
|
+
pandas.DataFrame: A DataFrame with two columns: '단어' (keyword) and '빈도' (frequency), sorted by frequency in descending order.
|
|
140
|
+
|
|
141
|
+
Raises:
|
|
142
|
+
TypeError: If the input is not a dictionary.
|
|
143
|
+
"""
|
|
144
|
+
if isinstance(key_words, dict):
|
|
145
|
+
word_df = pd.DataFrame(key_words.items())
|
|
146
|
+
word_df.columns = ["단어", "빈도"]
|
|
147
|
+
word_df = word_df.sort_values(["빈도"], ascending=False).reset_index(
|
|
148
|
+
drop=True,
|
|
149
|
+
)
|
|
150
|
+
return word_df
|
|
151
|
+
else:
|
|
152
|
+
raise TypeError("input type is to be have to dict")
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
# pylint: disable=C0301
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
import pandas as pd
|
|
5
|
+
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer, TfidfVectorizer
|
|
6
|
+
from sklearn.pipeline import Pipeline
|
|
7
|
+
from sklearn.preprocessing import Normalizer
|
|
8
|
+
|
|
9
|
+
from .base import (
|
|
10
|
+
counter_to_dataframe,
|
|
11
|
+
duplication_remover,
|
|
12
|
+
keyword_list,
|
|
13
|
+
keyword_parser,
|
|
14
|
+
word_counter,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def keyword_dataframe(df):
|
|
19
|
+
"""
|
|
20
|
+
Generates a DataFrame of keyword frequencies based on the '키워드' column in the input DataFrame.
|
|
21
|
+
|
|
22
|
+
This function extracts keywords from the '키워드' column, parses them, and counts the occurrence of each keyword.
|
|
23
|
+
The result is returned as a DataFrame with columns for the keyword and its frequency.
|
|
24
|
+
|
|
25
|
+
Parameters:
|
|
26
|
+
df (pandas.DataFrame): The input DataFrame containing a '키워드' column with keywords.
|
|
27
|
+
|
|
28
|
+
Returns:
|
|
29
|
+
pandas.DataFrame: A DataFrame with two columns - '단어' (keyword) and '빈도' (frequency), sorted by frequency in descending order.
|
|
30
|
+
|
|
31
|
+
Raises:
|
|
32
|
+
TypeError: If the input is not a pandas DataFrame.
|
|
33
|
+
"""
|
|
34
|
+
if isinstance(df, pd.DataFrame):
|
|
35
|
+
lis = keyword_list(df)
|
|
36
|
+
keywords = keyword_parser(lis)
|
|
37
|
+
counter = word_counter(keywords)
|
|
38
|
+
df = counter_to_dataframe(counter)
|
|
39
|
+
return df
|
|
40
|
+
else:
|
|
41
|
+
raise TypeError("input type is to be have to DataFrame")
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def keyword_dataframe_no_duplicated(df):
|
|
45
|
+
"""
|
|
46
|
+
Generates a DataFrame of keyword frequencies after removing duplicate keywords.
|
|
47
|
+
|
|
48
|
+
This function extracts keywords from the '키워드' column, removes duplicate keywords within each list, and counts the occurrence of each unique keyword.
|
|
49
|
+
The result is returned as a DataFrame with columns for the keyword and its frequency.
|
|
50
|
+
|
|
51
|
+
Parameters:
|
|
52
|
+
df (pandas.DataFrame): The input DataFrame containing a '키워드' column with keywords.
|
|
53
|
+
|
|
54
|
+
Returns:
|
|
55
|
+
pandas.DataFrame: A DataFrame with two columns - '단어' (keyword) and '빈도' (frequency), sorted by frequency in descending order.
|
|
56
|
+
|
|
57
|
+
Raises:
|
|
58
|
+
TypeError: If the input is not a pandas DataFrame.
|
|
59
|
+
"""
|
|
60
|
+
if isinstance(df, pd.DataFrame):
|
|
61
|
+
lis = keyword_list(df)
|
|
62
|
+
keywords = keyword_parser(lis)
|
|
63
|
+
keywords_set = duplication_remover(keywords)
|
|
64
|
+
counter = word_counter(keywords_set)
|
|
65
|
+
df = counter_to_dataframe(counter)
|
|
66
|
+
return df
|
|
67
|
+
else:
|
|
68
|
+
raise TypeError("input type is to be have to DataFrame")
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def tfidf(df, col=None):
|
|
72
|
+
"""
|
|
73
|
+
Calculates the Term Frequency-Inverse Document Frequency (TF-IDF) for keywords in the input DataFrame.
|
|
74
|
+
|
|
75
|
+
This function takes an optional column name (col) to select a specific column for TF-IDF calculations. It uses the TfidfVectorizer to compute TF-IDF values for the keywords
|
|
76
|
+
and returns a DataFrame of words with their corresponding TF-IDF scores.
|
|
77
|
+
|
|
78
|
+
Parameters:
|
|
79
|
+
df (pandas.DataFrame): The input DataFrame containing text data, typically in a '키워드' column.
|
|
80
|
+
col (str, optional): A column name specifying which column to apply the TF-IDF transformation. Defaults to None.
|
|
81
|
+
|
|
82
|
+
Returns:
|
|
83
|
+
pandas.DataFrame: A DataFrame with two columns - '단어' (keyword) and '빈도' (TF-IDF score), sorted by score in descending order.
|
|
84
|
+
|
|
85
|
+
Raises:
|
|
86
|
+
TypeError: If the input is not a pandas DataFrame.
|
|
87
|
+
"""
|
|
88
|
+
if isinstance(df, pd.DataFrame):
|
|
89
|
+
if isinstance(col, str):
|
|
90
|
+
df = df[col]
|
|
91
|
+
lis = keyword_list(df)
|
|
92
|
+
|
|
93
|
+
tfidfv = TfidfVectorizer()
|
|
94
|
+
tdm = tfidfv.fit_transform(lis)
|
|
95
|
+
|
|
96
|
+
word_count = (
|
|
97
|
+
pd.DataFrame(
|
|
98
|
+
{
|
|
99
|
+
"단어": tfidfv.get_feature_names_out(),
|
|
100
|
+
"빈도": tdm.sum(axis=0).flat,
|
|
101
|
+
},
|
|
102
|
+
)
|
|
103
|
+
.sort_values("빈도", ascending=False)
|
|
104
|
+
.reset_index(drop=True)
|
|
105
|
+
)
|
|
106
|
+
return word_count
|
|
107
|
+
else:
|
|
108
|
+
raise TypeError("input type is to be have to DataFrame")
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def tfidf_vector(df):
|
|
112
|
+
"""
|
|
113
|
+
Creates a TF-IDF vector representation of the keywords in the input DataFrame.
|
|
114
|
+
|
|
115
|
+
This function uses a scikit-learn Pipeline that combines CountVectorizer and TfidfTransformer to generate a TF-IDF matrix (vectorized form) for the keywords in the DataFrame.
|
|
116
|
+
|
|
117
|
+
Parameters:
|
|
118
|
+
df (pandas.DataFrame): The input DataFrame containing a '키워드' column with keywords.
|
|
119
|
+
|
|
120
|
+
Returns:
|
|
121
|
+
numpy.ndarray: A 2D array where each row corresponds to the TF-IDF vector for the keywords in each row of the DataFrame.
|
|
122
|
+
|
|
123
|
+
Raises:
|
|
124
|
+
TypeError: If the input is not a pandas DataFrame.
|
|
125
|
+
"""
|
|
126
|
+
if isinstance(df, pd.DataFrame):
|
|
127
|
+
lis = keyword_list(df)
|
|
128
|
+
pipeline = Pipeline(
|
|
129
|
+
[
|
|
130
|
+
("vect", CountVectorizer()),
|
|
131
|
+
("tfidf", TfidfTransformer()),
|
|
132
|
+
],
|
|
133
|
+
)
|
|
134
|
+
vec = pipeline.fit_transform(lis).toarray()
|
|
135
|
+
return vec
|
|
136
|
+
else:
|
|
137
|
+
raise TypeError("input type is to be have to DataFrame")
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def normalize_vector(vec):
|
|
141
|
+
"""
|
|
142
|
+
Normalizes a TF-IDF vector or any other vector to unit length (L2 norm).
|
|
143
|
+
|
|
144
|
+
This function uses the Normalizer from scikit-learn to scale each vector such that the Euclidean norm of each vector becomes 1.
|
|
145
|
+
|
|
146
|
+
Parameters:
|
|
147
|
+
vec (numpy.ndarray): A 2D array where each row is a vector to be normalized.
|
|
148
|
+
|
|
149
|
+
Returns:
|
|
150
|
+
numpy.ndarray: A 2D array with normalized vectors.
|
|
151
|
+
|
|
152
|
+
Raises:
|
|
153
|
+
TypeError: If the input is not a numpy ndarray.
|
|
154
|
+
"""
|
|
155
|
+
if isinstance(vec, np.ndarray):
|
|
156
|
+
vec_nor = Normalizer().fit_transform(vec)
|
|
157
|
+
return vec_nor
|
|
158
|
+
else:
|
|
159
|
+
raise TypeError("input type is to be have to ndarray")
|