コード例 #1
0
ファイル: MorphAutomat.cs プロジェクト: svn2github/seman
 public bool Load(string grammarFileName, FileManager manager)
 {
     Clear();
     using (var file = manager.GetFile(Lemmatizer.Registry, grammarFileName, Lemmatizer.CodePage)) {
         string line   = null;
         var    reader = new BinaryReader(file, _tools.InternalEncoding(Lemmatizer.CodePage));
         line = Tools.ReadLine(reader);
         if (line == null)
         {
             return(false);
         }
         int nodeCount;
         int.TryParse(line, out nodeCount);
         if (nodeCount <= 0)
         {
             return(false);
         }
         if (_nodes != null)
         {
             throw new MorphException("_nodes != null");
         }
         var len    = nodeCount * sizeof(int);
         var buffer = reader.ReadBytes(len);
         if (buffer.Length != len)
         {
             return(false);
         }
         var bufferInt = new int[nodeCount];
         Buffer.BlockCopy(buffer, 0, bufferInt, 0, buffer.Length);
         _nodes = new MorphAutomNode[nodeCount];
         for (var i = 0; i < nodeCount; i++)
         {
             _nodes[i] = new MorphAutomNode {
                 Data = bufferInt[i]
             };;
         }
         line = Tools.ReadLine(reader);
         if (line == null)
         {
             return(false);
         }
         int.TryParse(line, out nodeCount);
         len    = nodeCount * sizeof(int);
         buffer = reader.ReadBytes(len);
         if (buffer.Length != len)
         {
             return(false);
         }
         bufferInt = new int[nodeCount];
         Buffer.BlockCopy(buffer, 0, bufferInt, 0, buffer.Length);
         _relations = new MorphAutomRelation[nodeCount];
         for (int i = 0; i < nodeCount; i++)
         {
             _relations[i] = new MorphAutomRelation {
                 Data = bufferInt[i]
             };
         }
         len    = Constants.AlphabetSize * sizeof(int);
         buffer = reader.ReadBytes(len);
         if (buffer.Length != len)
         {
             return(false);
         }
         var buf = new int[Constants.AlphabetSize];
         Buffer.BlockCopy(buffer, 0, buf, 0, buffer.Length);
         if (!Tools.ListEquals(buf, _alphabet2Code))
         {
             throw new MorphException(Tools.GetStringByLanguage(Language) + "alphabet has changed; cannot load morph automat");
         }
     }
     BuildChildrenCache();
     return(true);
 }
コード例 #2
0
ファイル: MorphAutomat.cs プロジェクト: tvi123/rep123
		public bool Load(string grammarFileName,FileManager manager) {
			Clear();
			using (var file = manager.GetFile(Lemmatizer.Registry, grammarFileName)) {
				string line = null;
				var reader = new BinaryReader(file, Tools.InternalEncoding);
				line = Tools.ReadLine(reader);
				if (line == null) {
					return false;
				}
				int nodeCount;
				int.TryParse(line, out nodeCount);
				if (nodeCount <= 0) {
					return false;
				}
				if (_nodes != null) {
					throw new MorphException("_nodes != null");
				}
				var len = nodeCount * sizeof(int);
				var buffer = reader.ReadBytes(len);
				if (buffer.Length != len) {
					return false;
				}
				var bufferInt = new int[nodeCount];
				Buffer.BlockCopy(buffer, 0, bufferInt, 0, buffer.Length);
				_nodes = new MorphAutomNode[nodeCount];
				for (var i = 0; i < nodeCount; i++) {
					_nodes[i] = new MorphAutomNode { Data = bufferInt[i] }; ;
				}
				line = Tools.ReadLine(reader);
				if (line == null) {
					return false;
				}
				int.TryParse(line, out nodeCount);
				len = nodeCount * sizeof(int);
				buffer = reader.ReadBytes(len);
				if (buffer.Length != len) {
					return false;
				}
				bufferInt = new int[nodeCount];
				Buffer.BlockCopy(buffer, 0, bufferInt, 0, buffer.Length);
				_relations = new MorphAutomRelation[nodeCount];
				for (int i = 0; i < nodeCount; i++) {
					_relations[i] = new MorphAutomRelation { Data = bufferInt[i] };
				}
				len = Constants.AlphabetSize * sizeof(int);
				buffer = reader.ReadBytes(len);
				if (buffer.Length != len) {
					return false;
				}
				var buf = new int[Constants.AlphabetSize];
				Buffer.BlockCopy(buffer, 0, buf, 0, buffer.Length);
				if (!Tools.ListEquals(buf, _alphabet2Code)) {
					throw new MorphException(Tools.GetStringByLanguage(Language) + "alphabet has changed; cannot load morph automat");
				}
			}
			BuildChildrenCache();
			return true;
		}