Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add word diagram #32

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 68 additions & 1 deletion chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
ResultFields,
ResultTableModel
)
import string
import re


class ChannelsPieChart(QChart):
Expand Down Expand Up @@ -92,6 +94,7 @@ def _on_slice_hovered(self, pie_slice, state):
return
pie_slice.setLabelVisible(state)


class VideoDurationChart(QChart):
def __init__(self, model: ResultTableModel):
super().__init__()
Expand Down Expand Up @@ -154,9 +157,73 @@ def _on_model_reset(self):
self.set_current_index(None)
self.rebuild()

def _find_category_for_value(self, duration:int ):
def _find_category_for_value(self, duration: int):
for i in range(len(self._categories) - 1):
if duration > self._categories[i][0]:
continue
return i
return len(self._categories) - 1


class WordsPieChart(QChart):
def __init__(self, model: ResultTableModel):
super().__init__()
self._current_index = None
self._model = model
self._model.modelReset.connect(self._on_model_reset)
self._series = QPieSeries()
self._series.setHoleSize(0.3)
self._series.hovered.connect(self._on_slice_hovered)
self.addSeries(self._series)
self._last_pen = None
self._last_brush = None

def rebuild(self):
self._series.clear()
if len(self._model.result) == 0:
return
text_title = str('')
for row in range(len(self._model.result)):
text_title += str(self._model.result[row][0]) + ' '

for p in string.punctuation + '\n':
if p in text_title:
text_title = text_title.replace(p, '')

regrex_pattern = re.compile(pattern="["
u"\U00000000-\U00000009"
u"\U0000000B-\U0000001F"
u"\U00000080-\U00000400"
u"\U00000402-\U0000040F"
u"\U00000450-\U00000450"
u"\U00000452-\U0010FFFF"
"]+", flags=re.UNICODE)

text = regrex_pattern.sub(r'', text_title)

text = text.lower()
list_word = text.split()
count = {}
for element in list_word:
if (len(element) > 2):
if count.get(element, None):
count[element] += 1
else:
count[element] = 1

sorted_values = sorted(count.items(), key=lambda tpl: tpl[1], reverse=True)
dict(sorted_values)
for name in range(len(sorted_values)):
if (name < 5):
count = '('+str(sorted_values[name][1])+')'
self._series.append(sorted_values[name][0]+count, sorted_values[name][1])

def _on_model_reset(self):
self.rebuild()

def _on_slice_hovered(self, pie_slice, state):
row = self._current_index.row() if self._current_index is not None else None
channel_name = self._model.get_field_data(row, ResultFields.VideoTitle)
if pie_slice.label() == channel_name:
return
pie_slice.setLabelVisible(state)
18 changes: 12 additions & 6 deletions widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
)
from chart import (
ChannelsPieChart,
VideoDurationChart
VideoDurationChart,
WordsPieChart
)


Expand All @@ -50,16 +51,16 @@ def setPixmap(self, pixmap: QPixmap | QImage | str):
scaled_pixmap = self._scaled_pixmap()
if scaled_pixmap is not None:
return super().setPixmap(scaled_pixmap)

def heightForWidth(self, width: int):
if self._pixmap is None or self._pixmap.width() == 0:
return self.height()
return self._pixmap.height() * width / self._pixmap.width()

def sizeHint(self):
width = self.width()
return QSize(width, self.heightForWidth(width))

def resizeEvent(self, _event: QResizeEvent):
if self._pixmap is not None:
QTimer.singleShot(0, self, self._set_pixmap_delayed)
Expand All @@ -68,7 +69,7 @@ def _scaled_pixmap(self):
if self._pixmap is None or self._pixmap.width() == 0:
return None
return self._pixmap.scaled(self.size(), Qt.AspectRatioMode.KeepAspectRatio, Qt.TransformationMode.SmoothTransformation)

def _set_pixmap_delayed(self):
scaled_pixmap = self._scaled_pixmap()
if scaled_pixmap is not None:
Expand Down Expand Up @@ -149,7 +150,7 @@ def __init__(self, model: ResultTableModel, parent: QWidget = None):
main_layout.addWidget(self._tags_edit)

main_widget.setLayout(main_layout)

scroll_area = QScrollArea(self)
scroll_area.setWidgetResizable(True)
scroll_area.setWidget(main_widget)
Expand Down Expand Up @@ -238,6 +239,8 @@ def __init__(self, model: ResultTableModel, parent: QWidget = None):
self._chart_combobox = QComboBox()
self._chart_combobox.addItem(self.tr("Channels distribution chart"))
self._chart_combobox.addItem(self.tr("Video duration chart"))
self._chart_combobox.addItem(self.tr("Title words chart"))

self._chart_combobox.currentIndexChanged.connect(self._on_current_chart_changed)
main_layout.addWidget(self._chart_combobox)

Expand All @@ -251,6 +254,9 @@ def __init__(self, model: ResultTableModel, parent: QWidget = None):
video_duration_chart = VideoDurationChart(model)
self._charts.append(video_duration_chart)

words_pie_chart = WordsPieChart(model)
self._charts.append(words_pie_chart)

self._chart_view.setChart(channels_pie_chart)

def set_current_index(self, index: QModelIndex):
Expand Down
Loading