mirror of
https://github.com/arnaucube/argos.git
synced 2026-02-07 11:06:39 +01:00
hashtag analysis, and some updates
This commit is contained in:
@@ -12,13 +12,13 @@ import (
|
||||
var week = [7]string{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}
|
||||
|
||||
func printBar(n int, lowest int, highest int) {
|
||||
bar := int((float64(n) / float64(highest)) * 50)
|
||||
bar := int((float64(n) / float64(highest)) * 40)
|
||||
|
||||
if n == lowest {
|
||||
fmt.Print("\x1b[36;1m")
|
||||
fmt.Print("\x1b[36;1m") //cyan
|
||||
}
|
||||
if n == highest {
|
||||
fmt.Print("\x1b[31;1m")
|
||||
fmt.Print("\x1b[31;1m") //red
|
||||
}
|
||||
|
||||
for i := 0; i < bar; i++ {
|
||||
@@ -34,7 +34,7 @@ func printBar(n int, lowest int, highest int) {
|
||||
if n == highest {
|
||||
fmt.Print(" highest")
|
||||
}
|
||||
fmt.Print("\x1b[0m")
|
||||
fmt.Print("\x1b[0m") //defaultColor
|
||||
fmt.Println(" ")
|
||||
}
|
||||
func getHigherValueOfMap(m map[string]int) (int, int) {
|
||||
|
||||
17
analyzeHashtags.go
Normal file
17
analyzeHashtags.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/dghubble/go-twitter/twitter"
|
||||
)
|
||||
|
||||
func analyzeHashtags(tweets []twitter.Tweet, words map[string]int) map[string]int {
|
||||
var hashtags = make(map[string]int)
|
||||
for v, k := range words {
|
||||
if strings.Contains(v, "#") {
|
||||
hashtags[v] = k
|
||||
}
|
||||
}
|
||||
return (hashtags)
|
||||
}
|
||||
@@ -2,33 +2,12 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/dghubble/go-twitter/twitter"
|
||||
)
|
||||
|
||||
func sortMapWords(m map[string]int) map[int][]string {
|
||||
n := map[int][]string{}
|
||||
var a []int
|
||||
for k, v := range m {
|
||||
if v > minNumWords {
|
||||
n[v] = append(n[v], k)
|
||||
}
|
||||
}
|
||||
for k := range n {
|
||||
a = append(a, k)
|
||||
}
|
||||
sort.Sort(sort.Reverse(sort.IntSlice(a)))
|
||||
for _, k := range a {
|
||||
for _, s := range n[k] {
|
||||
fmt.Printf("%d - %s,\n", k, s)
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func mapWords(text string, words map[string]int) {
|
||||
func mapWords(text string, words map[string]int) map[string]int {
|
||||
s := strings.Split(text, " ")
|
||||
|
||||
for _, v := range s {
|
||||
@@ -38,16 +17,18 @@ func mapWords(text string, words map[string]int) {
|
||||
words[v] = 1
|
||||
}
|
||||
}
|
||||
return words
|
||||
}
|
||||
func analyzeWords(tweets []twitter.Tweet) {
|
||||
func analyzeWords(tweets []twitter.Tweet) map[string]int {
|
||||
var words = make(map[string]int)
|
||||
|
||||
for _, v := range tweets {
|
||||
mapWords(v.Text, words)
|
||||
words = mapWords(v.Text, words)
|
||||
}
|
||||
|
||||
fmt.Println(len(words))
|
||||
//get sorted list of frequency words
|
||||
_ = sortMapWords(words)
|
||||
_ = printSortedMapStringInt(words, minNumWords)
|
||||
fmt.Println(" ")
|
||||
return words
|
||||
}
|
||||
|
||||
Binary file not shown.
@@ -19,6 +19,10 @@ func getTweets(client *twitter.Client, username string, iterations int) []twitte
|
||||
Count: 200,
|
||||
MaxID: maxid,
|
||||
})
|
||||
//if no tweets, stop getting tweets
|
||||
if len(tweetsRaw) == 0 {
|
||||
break
|
||||
}
|
||||
maxid = tweetsRaw[len(tweetsRaw)-1].ID
|
||||
|
||||
for _, v := range tweetsRaw {
|
||||
@@ -33,23 +37,34 @@ func getUserTweets(client *twitter.Client) {
|
||||
fmt.Print("enter username: @")
|
||||
username, _ := newcommand.ReadString('\n')
|
||||
username = strings.TrimSpace(username)
|
||||
fmt.Println("user selected: " + username)
|
||||
|
||||
fmt.Println("user selected: \x1b[36;1m@" + username)
|
||||
fmt.Print("\x1b[0m") //defaultColor
|
||||
fmt.Println("-----------------------")
|
||||
|
||||
//get tweets
|
||||
tweets := getTweets(client, username, iterationsCount)
|
||||
|
||||
if len(tweets) == 0 {
|
||||
fmt.Println("User @" + username + " does not have tweets")
|
||||
return
|
||||
}
|
||||
//now analyze words and dates
|
||||
fmt.Println("word count")
|
||||
analyzeWords(tweets)
|
||||
fmt.Println("Word frequency (more than " + strconv.Itoa(minNumWords) + " times):")
|
||||
words := analyzeWords(tweets)
|
||||
|
||||
fmt.Println("Hashtags used (more than " + strconv.Itoa(minNumHashtag) + " times): ")
|
||||
hashtags := analyzeHashtags(tweets, words)
|
||||
printSortedMapStringInt(hashtags, minNumHashtag)
|
||||
fmt.Println("")
|
||||
|
||||
analyzeDates(tweets)
|
||||
fmt.Println("")
|
||||
fmt.Println("Devices:")
|
||||
sources := analyzeSource(tweets)
|
||||
for k, v := range sources {
|
||||
fmt.Print("\x1b[32;1m") //cyan
|
||||
fmt.Print(k + ": ")
|
||||
fmt.Print("\x1b[0m") //defaultColor
|
||||
fmt.Println(strconv.Itoa(v) + "tw ")
|
||||
}
|
||||
|
||||
@@ -60,7 +75,7 @@ func getUserTweets(client *twitter.Client) {
|
||||
fmt.Println(tweets[0].CreatedAt)
|
||||
|
||||
fmt.Println(" ")
|
||||
fmt.Println("total of " + strconv.Itoa(len(tweets)) + " tweets")
|
||||
fmt.Println("Total of " + strconv.Itoa(len(tweets)) + " tweets analyzed")
|
||||
fmt.Println(" ")
|
||||
fmt.Println("User @" + username + " analysis finished")
|
||||
}
|
||||
|
||||
7
main.go
7
main.go
@@ -8,23 +8,26 @@ import (
|
||||
)
|
||||
|
||||
const minNumWords = 10
|
||||
const minNumHashtag = 2
|
||||
const iterationsCount = 3
|
||||
|
||||
func main() {
|
||||
fmt.Println("---------------")
|
||||
fmt.Print("\x1b[36;1m") //cyan
|
||||
fmt.Println("goTweetsAnalyze initialized")
|
||||
fmt.Print("\x1b[0m") //defaultColor
|
||||
fmt.Println("Reading twitterConfig.json file")
|
||||
client := readConfigTokensAndConnect()
|
||||
|
||||
fmt.Println("---------------")
|
||||
newcommand := bufio.NewReader(os.Stdin)
|
||||
fmt.Print("Please select command number:")
|
||||
fmt.Print("Please select command number")
|
||||
options := `
|
||||
1 - Analyze username
|
||||
0 - Exit script
|
||||
option to select: `
|
||||
for {
|
||||
fmt.Print(options)
|
||||
|
||||
option, _ := newcommand.ReadString('\n')
|
||||
option = strings.TrimSpace(option)
|
||||
|
||||
|
||||
26
printSortedMapStringInt.go
Normal file
26
printSortedMapStringInt.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
)
|
||||
|
||||
func printSortedMapStringInt(m map[string]int, threshold int) map[int][]string {
|
||||
n := map[int][]string{}
|
||||
var a []int
|
||||
for k, v := range m {
|
||||
if v > threshold {
|
||||
n[v] = append(n[v], k)
|
||||
}
|
||||
}
|
||||
for k := range n {
|
||||
a = append(a, k)
|
||||
}
|
||||
sort.Sort(sort.Reverse(sort.IntSlice(a)))
|
||||
for _, k := range a {
|
||||
for _, s := range n[k] {
|
||||
fmt.Printf("%d - %s,\n", k, s)
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
BIN
screen2.png
BIN
screen2.png
Binary file not shown.
|
Before Width: | Height: | Size: 80 KiB After Width: | Height: | Size: 81 KiB |
BIN
screen3.png
BIN
screen3.png
Binary file not shown.
|
Before Width: | Height: | Size: 60 KiB After Width: | Height: | Size: 65 KiB |
Reference in New Issue
Block a user