hashtag analysis, and some updates

This commit is contained in:
arnaucode
2017-04-16 23:14:42 +02:00
parent ccfc1f6edc
commit 01099f172f
9 changed files with 78 additions and 36 deletions

View File

@@ -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
View 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)
}

View File

@@ -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.

View File

@@ -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")
}

View File

@@ -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)

View 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
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 80 KiB

After

Width:  |  Height:  |  Size: 81 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 60 KiB

After

Width:  |  Height:  |  Size: 65 KiB