From ff38d34dc459ddb37d8380b80420b31faed7ed88 Mon Sep 17 00:00:00 2001 From: ruidajo Date: Tue, 21 Jun 2022 11:59:13 -0500 Subject: [PATCH] Refactoring histograms data calculation and exporters. --- .../instance/exportEmptyHistogramNamed..st | 9 ++++ .../instance/exportHistogramFor.By..st | 30 ++++++++++++ .../instance/exportQuotesHistogram.st | 2 +- .../exportQuotesHistogramWithBars..st | 2 +- .../exportRepliesHistogramWithBars..st | 48 +++---------------- .../instance/exportRetweetsHistogram.st | 2 +- .../exportRetweetsHistogramWithBars..st | 2 +- .../exportTweetsHistogramWithBars..st | 39 +++------------ .../instance/quotesReportData.st | 2 +- .../instance/quotesSortedByOccurrences.st | 9 ---- .../instance/retweetsReportData.st | 2 +- .../instance/retweetsSortedByOccurrences.st | 9 ---- .../asMessagesUserNamesSortedByOccurrences.st | 9 ++++ 13 files changed, 68 insertions(+), 97 deletions(-) create mode 100644 Socialmetrica.package/NitterUser.class/instance/exportEmptyHistogramNamed..st create mode 100644 Socialmetrica.package/NitterUser.class/instance/exportHistogramFor.By..st delete mode 100644 Socialmetrica.package/NitterUser.class/instance/quotesSortedByOccurrences.st delete mode 100644 Socialmetrica.package/NitterUser.class/instance/retweetsSortedByOccurrences.st create mode 100644 Socialmetrica.package/TweetsCollection.class/instance/asMessagesUserNamesSortedByOccurrences.st diff --git a/Socialmetrica.package/NitterUser.class/instance/exportEmptyHistogramNamed..st b/Socialmetrica.package/NitterUser.class/instance/exportEmptyHistogramNamed..st new file mode 100644 index 0000000..b816192 --- /dev/null +++ b/Socialmetrica.package/NitterUser.class/instance/exportEmptyHistogramNamed..st @@ -0,0 +1,9 @@ +accessing +exportEmptyHistogramNamed: aDictionary + + | histogram | + histogram := RSChart new. + histogram extent: (aDictionary at: 'extent'). + histogram build. + histogram canvas exportAsFileNamed: (aDictionary at: 'messagesType'), '-histogram' into: self folder. + ^ self \ No newline at end of file diff --git a/Socialmetrica.package/NitterUser.class/instance/exportHistogramFor.By..st b/Socialmetrica.package/NitterUser.class/instance/exportHistogramFor.By..st new file mode 100644 index 0000000..469ed68 --- /dev/null +++ b/Socialmetrica.package/NitterUser.class/instance/exportHistogramFor.By..st @@ -0,0 +1,30 @@ +accessing +exportHistogramFor: aDictionary By: aTypeString + "TODO: quotes and retweets" + | messagesByTimeSpan histogram diagram tempMessages | + tempMessages := self perform: (aDictionary at: 'messagesType') asSymbol. + tempMessages ifEmpty: [ self exportEmptyHistogramNamed: + (aDictionary at: 'messagesType'), '-histogram' ]. + aTypeString isNumber ifTrue: [ messagesByTimeSpan := tempMessages splitBytimeSpansOf: aTypeString ]. + (aTypeString = 'day' or: [ aTypeString = 'days' ]) + ifTrue: [ messagesByTimeSpan := tempMessages splitByDays ]. + (aTypeString = 'week' or: [ aTypeString = 'weeks' ]) + ifTrue: [ messagesByTimeSpan := tempMessages splitByWeeks ]. + + histogram := RSChart new. + histogram extent: (aDictionary at: 'extent'). + diagram := RSBarPlot new + y: (messagesByTimeSpan values collect: [ :collection | collection size ]). + "diagram color: (Color r:(91/255) g:(131/255) b:(222/255))." + diagram color: (aDictionary at: 'color'). + histogram addPlot: diagram. + histogram addDecoration: (RSHorizontalTick new + fromNames: messagesByTimeSpan keys; + labelRotation: 0; + fontSize: 68 /messagesByTimeSpan size; + yourself). + histogram addDecoration: (RSVerticalTick new + integer; + fontSize: 68 /messagesByTimeSpan size). + histogram build. + ^ histogram canvas exportAsFileNamed: (aDictionary at: 'messagesType'), '-histogram' into: self folder \ No newline at end of file diff --git a/Socialmetrica.package/NitterUser.class/instance/exportQuotesHistogram.st b/Socialmetrica.package/NitterUser.class/instance/exportQuotesHistogram.st index 649a5d5..f738f0e 100644 --- a/Socialmetrica.package/NitterUser.class/instance/exportQuotesHistogram.st +++ b/Socialmetrica.package/NitterUser.class/instance/exportQuotesHistogram.st @@ -14,7 +14,7 @@ exportQuotesHistogram export. ^ self ]. - quotesOccurrences := self quotesSortedByOccurrences. + quotesOccurrences := self quotes asMessagesUserNamesSortedByOccurrences. labels := quotesOccurrences keys. labels := labels collect: [ :profiles | ('@', profiles) ]. quotesHistogram := RSChart new. diff --git a/Socialmetrica.package/NitterUser.class/instance/exportQuotesHistogramWithBars..st b/Socialmetrica.package/NitterUser.class/instance/exportQuotesHistogramWithBars..st index 5b1dd04..6060ce7 100644 --- a/Socialmetrica.package/NitterUser.class/instance/exportQuotesHistogramWithBars..st +++ b/Socialmetrica.package/NitterUser.class/instance/exportQuotesHistogramWithBars..st @@ -2,7 +2,7 @@ accessing exportQuotesHistogramWithBars: aNumberOfBars | keysToRemove quotes labels quotesHistogram diagram | - quotes := self quotesSortedByOccurrences. + quotes := self quotes asMessagesUserNamesSortedByOccurrences. (aNumberOfBars > quotes size) ifTrue: [ ^ self exportQuotesHistogram ]. keysToRemove := OrderedCollection new. 1 to: quotes size - aNumberOfBars do: diff --git a/Socialmetrica.package/NitterUser.class/instance/exportRepliesHistogramWithBars..st b/Socialmetrica.package/NitterUser.class/instance/exportRepliesHistogramWithBars..st index f0bd874..c9fd4bc 100644 --- a/Socialmetrica.package/NitterUser.class/instance/exportRepliesHistogramWithBars..st +++ b/Socialmetrica.package/NitterUser.class/instance/exportRepliesHistogramWithBars..st @@ -1,44 +1,10 @@ accessing exportRepliesHistogramWithBars: aNumberOfBars - | tweetsByTimeSpan subtotals tweetsHistogram diagram | - - self replies ifEmpty: [ - tweetsHistogram := RSChart new. - tweetsHistogram extent: 800@200. - tweetsHistogram build. - tweetsHistogram canvas pdfExporter - zoomToShapes; - noFixedShapes; - fileName: (self folder / 'replies-histogram.pdf')fullName; - export. - ^ self - ]. - tweetsByTimeSpan := self collectMessages: [ self replies ] byTimeSpanSplits: aNumberOfBars. - subtotals := tweetsByTimeSpan values collect: [ :collection | collection size ]. - tweetsHistogram := RSChart new. - tweetsHistogram extent: 800@200. - diagram := RSBarPlot new - y:subtotals. - diagram color: (Color r:(246/255) g:(185/255) b:(46/255)). - tweetsHistogram addPlot: diagram. - tweetsHistogram addDecoration: (RSHorizontalTick new - fromNames: tweetsByTimeSpan keys; - labelRotation: 0; - fontSize: 72 /aNumberOfBars; - yourself). - tweetsHistogram addDecoration: (RSVerticalTick new - asFloat: 2; - fontSize: 72 /aNumberOfBars). - tweetsHistogram build. - tweetsHistogram canvas pngExporter - zoomToShapes; - noFixedShapes; - fileName: (self folder / 'replies-histogram.png')fullName; - export. - tweetsHistogram canvas pdfExporter - zoomToShapes; - noFixedShapes; - fileName: (self folder / 'replies-histogram.pdf')fullName; - export. - ^ self folder / 'replies-histogram.png' \ No newline at end of file + | repliesDict | + repliesDict := { + 'messagesType' -> 'replies'. + 'extent' -> (800@200). + 'color' -> (Color r:(246/255) g:(185/255) b:(46/255)) + } asDictionary. + ^ self exportHistogramFor: repliesDict By: aNumberOfBars \ No newline at end of file diff --git a/Socialmetrica.package/NitterUser.class/instance/exportRetweetsHistogram.st b/Socialmetrica.package/NitterUser.class/instance/exportRetweetsHistogram.st index 28d306f..a068910 100644 --- a/Socialmetrica.package/NitterUser.class/instance/exportRetweetsHistogram.st +++ b/Socialmetrica.package/NitterUser.class/instance/exportRetweetsHistogram.st @@ -3,7 +3,7 @@ exportRetweetsHistogram | retweetsOccurrences labels retweetColor diagram retweetsHistogram | - retweetsOccurrences := self retweetsSortedByOccurrences. + retweetsOccurrences := self retweets asMessagesUserNamesSortedByOccurrences. retweetColor := (Color r:(217/255) g:(56/255) b: (124/255)). labels := retweetsOccurrences keys. labels := labels collect: [ :profiles | ('@', profiles) ]. diff --git a/Socialmetrica.package/NitterUser.class/instance/exportRetweetsHistogramWithBars..st b/Socialmetrica.package/NitterUser.class/instance/exportRetweetsHistogramWithBars..st index b7d84cb..3c51f52 100644 --- a/Socialmetrica.package/NitterUser.class/instance/exportRetweetsHistogramWithBars..st +++ b/Socialmetrica.package/NitterUser.class/instance/exportRetweetsHistogramWithBars..st @@ -2,7 +2,7 @@ accessing exportRetweetsHistogramWithBars: aNumberOfBars | keysToRemove retweets labels retweetsHistogram diagram | - retweets := self retweetsSortedByOccurrences. + retweets := self retweets asMessagesUserNamesSortedByOccurrences. (aNumberOfBars > retweets size) ifTrue: [ ^ self exportRetweetsHistogram ]. keysToRemove := OrderedCollection new. 1 to: retweets size - aNumberOfBars do: diff --git a/Socialmetrica.package/NitterUser.class/instance/exportTweetsHistogramWithBars..st b/Socialmetrica.package/NitterUser.class/instance/exportTweetsHistogramWithBars..st index b39c703..0226508 100644 --- a/Socialmetrica.package/NitterUser.class/instance/exportTweetsHistogramWithBars..st +++ b/Socialmetrica.package/NitterUser.class/instance/exportTweetsHistogramWithBars..st @@ -1,35 +1,10 @@ accessing exportTweetsHistogramWithBars: aNumberOfBars - | tweetsByTimeSpan subtotals tweetsHistogram diagram | - - self tweets ifEmpty: [ - tweetsHistogram := RSChart new. - tweetsHistogram extent: 800@200. - tweetsHistogram build. - tweetsHistogram canvas pdfExporter - zoomToShapes; - noFixedShapes; - fileName: (self folder / 'tweets-histogram.pdf')fullName; - export. - ^ {self. 'self tweets is empty'} - ]. - - tweetsByTimeSpan := self collectMessages: [ self tweets] byTimeSpanSplits: aNumberOfBars. - subtotals := tweetsByTimeSpan values collect: [ :collection | collection size ]. - tweetsHistogram := RSChart new. - tweetsHistogram extent: 800@200. - diagram := RSBarPlot new - y:subtotals. - diagram color: (Color r:(91/255) g:(131/255) b:(222/255)). - tweetsHistogram addPlot: diagram. - tweetsHistogram addDecoration: (RSHorizontalTick new - fromNames: tweetsByTimeSpan keys; - labelRotation: 0; - fontSize: 72 /aNumberOfBars; - yourself). - tweetsHistogram addDecoration: (RSVerticalTick new - integer; - fontSize: 72 /aNumberOfBars). - tweetsHistogram build. - ^ tweetsHistogram canvas exportAsFileNamed: 'tweets-histogram' into: self folder \ No newline at end of file + | tweetsDict | + tweetsDict := { + 'messagesType' -> 'tweets'. + 'extent' -> (800@200). + 'color' -> (Color r:(91/255) g:(131/255) b:(222/255)) + } asDictionary. + ^ self exportHistogramFor: tweetsDict By: aNumberOfBars \ No newline at end of file diff --git a/Socialmetrica.package/NitterUser.class/instance/quotesReportData.st b/Socialmetrica.package/NitterUser.class/instance/quotesReportData.st index f44c3c6..271b8fc 100644 --- a/Socialmetrica.package/NitterUser.class/instance/quotesReportData.st +++ b/Socialmetrica.package/NitterUser.class/instance/quotesReportData.st @@ -2,7 +2,7 @@ accessing quotesReportData | tempDict labels xAxis | - tempDict := self quotesSortedByOccurrences. + tempDict := self quotes asMessagesUserNamesSortedByOccurrences. tempDict size > 10 ifTrue: [ tempDict := (tempDict associations copyFrom: 1 to: 10) asOrderedDictionary ]. diff --git a/Socialmetrica.package/NitterUser.class/instance/quotesSortedByOccurrences.st b/Socialmetrica.package/NitterUser.class/instance/quotesSortedByOccurrences.st deleted file mode 100644 index 94e17a2..0000000 --- a/Socialmetrica.package/NitterUser.class/instance/quotesSortedByOccurrences.st +++ /dev/null @@ -1,9 +0,0 @@ -accessing -quotesSortedByOccurrences - - | response quotesUserOccurrences | - quotesUserOccurrences := (self quotes messages collect: [ :tweet | ((tweet metadata at: 'quote') splitOn: '/') first ]) asBag contents asDictionary. - response := OrderedDictionary new. - (quotesUserOccurrences associations asSortedCollection: [:x :y | x value > y value]) - collect: [:assoc | response at: assoc key put:assoc value]. - ^ response \ No newline at end of file diff --git a/Socialmetrica.package/NitterUser.class/instance/retweetsReportData.st b/Socialmetrica.package/NitterUser.class/instance/retweetsReportData.st index 8e7ad75..03ba6fa 100644 --- a/Socialmetrica.package/NitterUser.class/instance/retweetsReportData.st +++ b/Socialmetrica.package/NitterUser.class/instance/retweetsReportData.st @@ -2,7 +2,7 @@ accessing retweetsReportData | tempDict labels xAxis | - tempDict := self retweetsSortedByOccurrences. + tempDict := self retweets asMessagesUserNamesSortedByOccurrences. tempDict size > 10 ifTrue: [ tempDict := (tempDict associations copyFrom: 1 to: 10) asOrderedDictionary ]. diff --git a/Socialmetrica.package/NitterUser.class/instance/retweetsSortedByOccurrences.st b/Socialmetrica.package/NitterUser.class/instance/retweetsSortedByOccurrences.st deleted file mode 100644 index 8c957c0..0000000 --- a/Socialmetrica.package/NitterUser.class/instance/retweetsSortedByOccurrences.st +++ /dev/null @@ -1,9 +0,0 @@ -accessing -retweetsSortedByOccurrences - - | response retweetsOccurrences | - retweetsOccurrences := (self retweets messages collect: [ :tweet | tweet user userName ]) asBag contents asDictionary. - response := OrderedDictionary new. - (retweetsOccurrences associations asSortedCollection: [:x :y | x value > y value]) - collect: [:assoc | response at: assoc key put:assoc value]. - ^ response \ No newline at end of file diff --git a/Socialmetrica.package/TweetsCollection.class/instance/asMessagesUserNamesSortedByOccurrences.st b/Socialmetrica.package/TweetsCollection.class/instance/asMessagesUserNamesSortedByOccurrences.st new file mode 100644 index 0000000..d160c62 --- /dev/null +++ b/Socialmetrica.package/TweetsCollection.class/instance/asMessagesUserNamesSortedByOccurrences.st @@ -0,0 +1,9 @@ +accessing +asMessagesUserNamesSortedByOccurrences + + | response messagesOccurrences | + messagesOccurrences := (self collect: [ :tweet | tweet user userName ]) asBag contents asDictionary. + response := OrderedDictionary new. + (messagesOccurrences associations asSortedCollection: [:x :y | x value > y value]) + collect: [:assoc | response at: assoc key put:assoc value]. + ^ response \ No newline at end of file