示例#1
0
        private static void initWord2VecModel(List <Tuple <string, uint> > hashGroup, int hash, float[][] vectors, SortedList <uint, List <Edge> > cayleyTable)
        {
            string path = System.IO.Path.Combine(DirectoryName, "hashsort" + hash.ToString() + ".bin");

            if (File.Exists(path) && hashGroup != null)
            {
                Word2VecModel        currentWord;
                Tuple <string, uint> findedWord;
                int  lenght;
                int  maxlenght;
                long delta, startPosition, position;
                int  borderLeft, borderRight, mid;
                int  comp;
                hashGroup.Sort();
                try
                {
                    using (BinaryReader reader = new BinaryReader(new FileStream(path, FileMode.Open), Encoding.UTF8))
                    {
                        lenght        = reader.ReadInt32();
                        maxlenght     = reader.ReadInt32();
                        delta         = maxlenght * 2 + 2000;
                        borderLeft    = 0;
                        startPosition = reader.BaseStream.Position;
                        foreach (Tuple <string, uint> word in hashGroup)
                        {
                            borderRight = lenght;
                            while (borderLeft + 1 != borderRight)
                            {
                                mid      = (borderRight + borderLeft) / 2;
                                position = startPosition + delta * mid;
                                reader.BaseStream.Seek(position, SeekOrigin.Begin);
                                currentWord = Word2VecModel.getWord(reader, maxlenght);
                                comp        = String.Compare(word.Item1, currentWord.word);
                                if (comp == 0)
                                {
                                    vectors[word.Item2] = currentWord.vector;
                                    cayleyTable.Add(word.Item2, new List <Edge>());
                                    findedWord = word;
                                    borderLeft = mid;
                                    break;
                                }
                                else if (comp > 0)
                                {
                                    borderLeft = mid;
                                }
                                else
                                {
                                    borderRight = mid;
                                }
                            }
                        }
                    }
                }
                catch { }
            }
        }
示例#2
0
        internal static void setWord(BinaryWriter writer, Word2VecModel word, int maxlenght)
        {
            maxlenght -= word.word.Length;
            int i;

            for (i = 0; i < word.word.Length; i++)
            {
                writer.Write(word.word[i]);
            }
            for (i = 0; i < maxlenght; i++)
            {
                writer.Write(space);
            }
            for (i = 0; i < word.vector.Length; i++)
            {
                writer.Write(word.vector[i]);
            }
        }