Browse Source

analyzes week days frequency and hour frequency

master
arnaucode 7 years ago
parent
commit
0d10f80cef
7 changed files with 131 additions and 52 deletions
  1. +1
    -1
      README.md
  2. +72
    -0
      analyzeDates.go
  3. +52
    -0
      analyzeWords.go
  4. BIN
      build/goTweetsAnalyze
  5. +3
    -24
      getUser.go
  6. +3
    -1
      main.go
  7. +0
    -26
      sortMap.go

+ 1
- 1
README.md

@ -1,7 +1,7 @@
# goTweetsAnalyze
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",

+ 72
- 0
analyzeDates.go

@ -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
- 0
analyzeWords.go

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

BIN
build/goTweetsAnalyze


analyzeUsername.go → getUser.go

@ -9,25 +9,7 @@ import (
"github.com/dghubble/go-twitter/twitter"
)
type Tweet struct {
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) {
func getUser(client *twitter.Client) {
newcommand := bufio.NewReader(os.Stdin)
fmt.Print("enter username: @")
username, _ := newcommand.ReadString('\n')
@ -41,10 +23,7 @@ func analyzeUsername(client *twitter.Client) {
Count: 200,
})
for _, v := range tweets {
mapWords(v.Text)
}
analyzeWords(tweets)
fmt.Println(len(words))
_ = sortMap(words)
analyzeDates(tweets)
}

+ 3
- 1
main.go

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

+ 0
- 26
sortMap.go

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

Loading…
Cancel
Save