Skip to content

Instantly share code, notes, and snippets.

@cipepser
Last active September 4, 2017 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cipepser/7abcc67e5e41ca5b7de31020c28aac7a to your computer and use it in GitHub Desktop.
Save cipepser/7abcc67e5e41ca5b7de31020c28aac7a to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"fmt"
"io"
"log"
"math/rand"
"os"
"strings"
)
func main() {
sentiment := []string{}
// label for positive sentence.
fpos, err := os.Open("../data/rt-polaritydata/rt-polarity.pos")
defer fpos.Close()
if err != nil {
panic(err)
}
r := bufio.NewReaderSize(fpos, 4096)
for {
l, _, err := r.ReadLine()
if err == io.EOF {
break
}
if err != nil {
panic(err)
}
sentiment = append(sentiment, "+1 "+string(l))
}
// label for negative sentence.
fneg, err := os.Open("../data/rt-polaritydata/rt-polarity.neg")
defer fneg.Close()
if err != nil {
panic(err)
}
r = bufio.NewReaderSize(fneg, 4096)
for {
l, _, err := r.ReadLine()
if err == io.EOF {
break
}
if err != nil {
panic(err)
}
sentiment = append(sentiment, "-1 "+string(l))
}
// shuffle sentences
sent := make([]string, len(sentiment))
for i, n := range rand.Perm(len(sentiment)) {
sent[i] = sentiment[n]
}
// write the result to txt file
fw, err := os.Create("../data/sentiment.txt")
if err != nil {
log.Fatal(err)
}
defer fw.Close()
for _, s := range sent {
fw.Write([]byte(s))
fw.Write([]byte(string('\n')))
}
// count the number of positive sentences and negative.
var p, n int
f, err := os.Open("../data/sentiment.txt")
defer f.Close()
if err != nil {
panic(err)
}
r = bufio.NewReaderSize(f, 4096)
for {
l, _, err := r.ReadLine()
if err == io.EOF {
break
}
if err != nil {
panic(err)
}
s := strings.Split(string(l), " ")[0]
if s == "+1" {
p++
} else {
n++
}
}
fmt.Println("positive: ", p)
fmt.Println("negative: ", n)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment