analyzes week days frequency and hour frequency

This commit is contained in:
arnaucode
2017-04-13 17:36:40 +02:00
parent c48bf60578
commit 0d10f80cef
7 changed files with 131 additions and 52 deletions

View File

@@ -1,7 +1,7 @@
# goTweetsAnalyze # goTweetsAnalyze
twitter analyzer written in Go lang twitter analyzer written in Go lang
needs a .json file with the content: needs a twitterConfig.json file on the /build folder with the content:
``` ```
{ {
"consumer_key": "xxxxxxxxxxxxxxxx", "consumer_key": "xxxxxxxxxxxxxxxx",

72
analyzeDates.go Normal file
View File

@@ -0,0 +1,72 @@
package main
import (
"fmt"
"strconv"
"strings"
"github.com/dghubble/go-twitter/twitter"
)
var week = [7]string{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}
func printBar(n int) {
for i := 0; i < n; i++ {
fmt.Print("█")
}
fmt.Println(" ")
}
func printDays(days map[string]int) {
for i := 0; i < len(week); i++ {
fmt.Print(week[i] + " - " + strconv.Itoa(days[week[i]]))
fmt.Print(" ")
printBar(days[week[i]])
}
}
func analyzeDays(tweets []twitter.Tweet) {
var days = make(map[string]int)
for _, v := range tweets {
//fmt.Println(v.CreatedAt)
day := strings.Split(v.CreatedAt, " ")[0]
if _, ok := days[day]; ok {
days[day] = days[day] + 1
} else {
days[day] = 1
}
}
printDays(days)
}
func printHours(hours map[string]int) {
for i := 0; i < 24; i++ {
var h string
if i < 10 {
h = "0" + strconv.Itoa(i)
} else {
h = strconv.Itoa(i)
}
fmt.Print(h + "h - " + strconv.Itoa(hours[h]) + "tw")
fmt.Print(" ")
printBar(hours[h])
}
}
func analyzeHours(tweets []twitter.Tweet) {
var hours = make(map[string]int)
for _, v := range tweets {
time := strings.Split(v.CreatedAt, " ")[3]
hour := strings.Split(time, ":")[0]
if _, ok := hours[hour]; ok {
hours[hour] = hours[hour] + 1
} else {
hours[hour] = 1
}
}
printHours(hours)
}
func analyzeDates(tweets []twitter.Tweet) {
analyzeDays(tweets)
fmt.Println(" ")
analyzeHours(tweets)
}

52
analyzeWords.go Normal file
View File

@@ -0,0 +1,52 @@
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) {
s := strings.Split(text, " ")
for _, v := range s {
if _, ok := words[v]; ok {
words[v] = words[v] + 1
} else {
words[v] = 1
}
}
}
func analyzeWords(tweets []twitter.Tweet) {
var words = make(map[string]int)
for _, v := range tweets {
mapWords(v.Text, words)
}
fmt.Println(len(words))
//get sorted list of frequency words
_ = sortMapWords(words)
}

Binary file not shown.

View File

@@ -9,25 +9,7 @@ import (
"github.com/dghubble/go-twitter/twitter" "github.com/dghubble/go-twitter/twitter"
) )
type Tweet struct { func getUser(client *twitter.Client) {
text string `json: "Text"`
}
var words = make(map[string]int)
func mapWords(text string) {
s := strings.Split(text, " ")
for _, v := range s {
if _, ok := words[v]; ok {
words[v] = words[v] + 1
} else {
words[v] = 1
}
}
}
func analyzeUsername(client *twitter.Client) {
newcommand := bufio.NewReader(os.Stdin) newcommand := bufio.NewReader(os.Stdin)
fmt.Print("enter username: @") fmt.Print("enter username: @")
username, _ := newcommand.ReadString('\n') username, _ := newcommand.ReadString('\n')
@@ -41,10 +23,7 @@ func analyzeUsername(client *twitter.Client) {
Count: 200, Count: 200,
}) })
for _, v := range tweets { analyzeWords(tweets)
mapWords(v.Text)
}
fmt.Println(len(words)) analyzeDates(tweets)
_ = sortMap(words)
} }

View File

@@ -7,6 +7,8 @@ import (
"strings" "strings"
) )
const minNumWords = 4
func main() { func main() {
fmt.Println("---------------") fmt.Println("---------------")
fmt.Println("goTweetsAnalyze initialized") fmt.Println("goTweetsAnalyze initialized")
@@ -28,7 +30,7 @@ option to select: `
switch option { switch option {
case "1": case "1":
fmt.Println("selected 1 - Analyze username") fmt.Println("selected 1 - Analyze username")
analyzeUsername(client) getUser(client)
break break
case "2": case "2":
fmt.Println("selected 2 - Delete all Favs") fmt.Println("selected 2 - Delete all Favs")

View File

@@ -1,26 +0,0 @@
package main
import (
"fmt"
"sort"
)
func sortMap(m map[string]int) map[int][]string {
n := map[int][]string{}
var a []int
for k, v := range m {
if v > 2 {
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("%s, %d\n", s, k)
}
}
return n
}