Created
April 25, 2018 11:44
-
-
Save toVersus/41a5930cb7ffa35d743f7dafa16407a7 to your computer and use it in GitHub Desktop.
[Language Processing 100 Essentials] #70: Discriminate whether the text represents positive or negative sentiment or not
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"math/rand" | |
"os" | |
"strings" | |
"time" | |
) | |
func main() { | |
var lines []string | |
posFile, err := os.Open("./rt-polarity.pos") | |
if err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
defer posFile.Close() | |
sc := bufio.NewScanner(posFile) | |
for sc.Scan() { | |
lines = append(lines, "+1 "+sc.Text()) | |
} | |
negFile, err := os.Open("./rt-polarity.neg") | |
if err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
defer negFile.Close() | |
sc = bufio.NewScanner(negFile) | |
for sc.Scan() { | |
lines = append(lines, "-1 "+sc.Text()) | |
} | |
r := rand.New(rand.NewSource(time.Now().UnixNano())) | |
r.Shuffle(len(lines), func(i, j int) { | |
lines[i], lines[j] = lines[j], lines[i] | |
}) | |
dest, err := os.Create("sentiment.txt") | |
if err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
defer dest.Close() | |
dest.WriteString(strings.Join(lines, "\n")) | |
pos, neg, err := countSentimentRatio("./sentiment.txt") | |
if err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
fmt.Println("pos: ", pos, "neg: ", neg) | |
} | |
func countSentimentRatio(path string) (int, int, error) { | |
dest, err := os.Open(path) | |
if err != nil { | |
return 0, 0, fmt.Errorf("could not open a file: %s\n %s", path, err) | |
} | |
lineCount, posCount := 0, 0 | |
sc := bufio.NewScanner(dest) | |
for sc.Scan() { | |
lineCount++ | |
if sc.Text()[:2] == "+1" { | |
posCount++ | |
} | |
} | |
return posCount, lineCount - posCount, nil | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"math/rand" | |
"os" | |
"testing" | |
"github.com/go-test/deep" | |
) | |
var formatDataTests = []struct { | |
name string | |
posFile string | |
posText string | |
negFile string | |
negText string | |
seed int64 | |
want []string | |
}{ | |
{ | |
name: "could not ", | |
posFile: "./test.pos", | |
posText: `campaign . | |
interim . | |
effective `, | |
negFile: "./test.neg", | |
negText: `simplistic . | |
laddish . | |
exploitative . `, | |
seed: 10, | |
want: []string{"-1 laddish . ", "+1 campaign . ", "+1 interim . ", "-1 exploitative . ", "+1 effective ", "-1 simplistic . "}, | |
}, | |
} | |
func TestFormatData(t *testing.T) { | |
for _, testcase := range formatDataTests { | |
t.Log(testcase.name) | |
var result []string | |
posFile, err := os.Create(testcase.posFile) | |
if err != nil { | |
t.Errorf("could not create a file: %s\n %s", testcase.posFile, err) | |
} | |
posFile.WriteString(testcase.posText) | |
posFile.Close() | |
posFile, _ = os.Open(testcase.posFile) | |
sc := bufio.NewScanner(posFile) | |
for sc.Scan() { | |
result = append(result, "+1 "+sc.Text()) | |
} | |
posFile.Close() | |
negFile, err := os.Create(testcase.negFile) | |
if err != nil { | |
t.Errorf("could not create a file: %s\n %s", testcase.negFile, err) | |
} | |
negFile.WriteString(testcase.negText) | |
negFile.Close() | |
negFile, _ = os.Open(testcase.negFile) | |
sc = bufio.NewScanner(negFile) | |
for sc.Scan() { | |
result = append(result, "-1 "+sc.Text()) | |
} | |
negFile.Close() | |
r := rand.New(rand.NewSource(testcase.seed)) | |
r.Shuffle(len(result), func(i, j int) { | |
result[i], result[j] = result[j], result[i] | |
}) | |
t.Log(result) | |
if diff := deep.Equal(result, testcase.want); diff != nil { | |
t.Error(diff) | |
} | |
if err = os.Remove(testcase.posFile); err != nil { | |
t.Errorf("could not delete a file: %s\n %s\n", testcase.posFile, err) | |
} | |
if err = os.Remove(testcase.negFile); err != nil { | |
t.Errorf("could not delete a file: %s\n %s\n", testcase.negFile, err) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment