Exemplo n.º 1
0
func main() {
	flag.Parse()

	if flag.NArg() != 1 {
		fmt.Fprintln(os.Stderr, "Usage: distance vectors.bin")
		os.Exit(1)
	}

	f, err := os.Open(flag.Arg(0))
	defer f.Close()
	if err != nil {
		log.Fatal(err)
	}

	embeds, err := go2vec.ReadWord2VecBinary(bufio.NewReader(io.Reader(f)), true)
	if err != nil {
		log.Fatal(err)
	}

	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for scanner.Scan() {
		token := scanner.Text()
		results, err := embeds.Similarity(token, 10)
		if err != nil {
			fmt.Println(os.Stderr, err.Error())
			os.Exit(1)
		}

		for _, wordSimilarity := range results {
			fmt.Println(wordSimilarity.Word, wordSimilarity.Similarity)
		}
	}
}
Exemplo n.º 2
0
func ReadEmbeddingsOrExit(config Embedding) *go2vec.Embeddings {
	if config.Filename == "" {
		return nil
	}

	if config.Normalize {
		log.Printf("Reading normalized vectors from %s...\n", config.Filename)
	} else {
		log.Printf("Reading vectors from %s...\n", config.Filename)
	}

	vecf, err := os.Open(config.Filename)
	ExitIfError("Cannot open vectors file: ", err)
	defer vecf.Close()

	vecs, err := go2vec.ReadWord2VecBinary(bufio.NewReader(vecf), config.Normalize)
	ExitIfError("Cannot read vectors: ", err)

	return vecs
}
Exemplo n.º 3
0
func main() {
	flag.Parse()

	if flag.NArg() != 1 {
		fmt.Fprintln(os.Stderr, "Usage: analogy vectors.bin")
		os.Exit(1)
	}

	f, err := os.Open(flag.Arg(0))
	defer f.Close()
	if err != nil {
		log.Fatal(err)
	}

	embeds, err := go2vec.ReadWord2VecBinary(bufio.NewReader(io.Reader(f)), true)
	if err != nil {
		log.Fatal(err)
	}

	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanLines)
	for scanner.Scan() {
		line := scanner.Text()

		parts := strings.Split(line, " ")
		if len(parts) != 3 {
			fmt.Fprintf(os.Stderr, "Skipping line that does not have three words: %s\n", line)
			continue
		}

		results, err := embeds.Analogy(parts[0], parts[1], parts[2], 10)
		if err != nil {
			fmt.Fprintln(os.Stderr, err.Error())
			continue
		}

		for _, wordSimilarity := range results {
			fmt.Println(wordSimilarity.Word, wordSimilarity.Similarity)
		}
	}
}
Exemplo n.º 4
0
func main() {
	flag.Parse()

	if flag.NArg() != 1 {
		os.Exit(1)
	}

	f, err := os.Open(flag.Arg(0))
	common.ExitIfError("Cannot open file: ", err)
	defer f.Close()

	embeds, err := go2vec.ReadWord2VecBinary(bufio.NewReader(f), true)
	common.ExitIfError("Cannot read vectors: ", err)

	embeds.Iterate(func(word string, embedding []float32) bool {
		fmt.Print(word + " ")

		fmt.Println(floatSliceToString(embedding))

		return true
	})
}