/// <summary> /// Calculates the search sense in a definition by the specified word /// </summary> /// <param name="def">The definition to use</param> /// <param name="whichWord">The word to calculate on</param> /// <returns>Any non-negative number if successfull; otherwise -1</returns> private static int GetSearchSense( Definition def, int whichWord ) { int retVal = -1; DbPartOfSpeechType indexType = GetSynSetTypeCode( def.PartOfSpeech ); string dbFileName = DbFileHelper.GetIndexForType( indexType )[ 0 ]; if( File.Exists( dbFileName ) ) { long offset = FastSearch( def.Words[ whichWord ], dbFileName ); Index idx = ParseIndex( offset, dbFileName ); for( int i = 0; i < idx.OffsetCount; i++ ) { retVal = 0; if( idx.SynSetsOffsets[ i ] == def.Position ) { retVal = i + 1; break; } } } return retVal; }
/// <summary> /// Parses a word definition at the specified offset in the specified file /// </summary> /// <param name="offset">The offset in the file at which to begin parsing</param> /// <param name="dbFileName">The full path of the file to open</param> /// <param name="word">The word that will be defined by the parsed definition</param> /// <returns>A populated Definition object is successful; otherwise null</returns> internal static Definition ParseDefinition( long offset, string dbFileName, string word ) { Definition retVal = null; try { Definition tempDef = new Definition(); string data = ReadRecord( offset, dbFileName ); if( !string.IsNullOrEmpty( data ) ) { int i = 0; bool foundPert = false; string[] tokens = data.Split( Constants.Tokenizer, StringSplitOptions.RemoveEmptyEntries ); tempDef.Position = Convert.ToInt64( tokens[ i ] ); i++; if( tempDef.Position != offset ) throw new ArithmeticException( "The stream position is not aligned with the specified offset!" ); tempDef.FileNumber = Convert.ToInt32( tokens[ i ] ); i++; tempDef.PartOfSpeech = tokens[ i ]; i++; if( GetSynSetTypeCode( tempDef.PartOfSpeech ) == DbPartOfSpeechType.Satellite ) tempDef.DefinitionType = Constants.INDIRECT_ANT; tempDef.WordCount = Convert.ToInt32( tokens[ i ] ); i++; for( int j = 0; j < tempDef.WordCount * 2; j += 2 ) //Step by two for lexid { string tempWord = tokens[ i + j ]; if( !string.IsNullOrEmpty( tempWord ) ) tempDef.Words.Add( DecodeWord( tempWord ) ); if( tempWord.ToLower() == word.ToLower() ) tempDef.WhichWord = ( i + j ); } i = ( i + ( tempDef.WordCount * 2 ) ); tempDef.PtrCount = Convert.ToInt32( tokens[ i ] ); i++; for( int j = i; j < ( i + ( tempDef.PtrCount * 4 ) ); j += 4 ) { int pointerIndex = GetPointerTypeIndex( tokens[ j ] ); long pointerOffset = Convert.ToInt64( tokens[ j + 1 ] ); int pointerPartOfSpeech = GetPartOfSpeech( Convert.ToChar( tokens[ j + 2 ] ) ); string lexToFrom = tokens[ j + 3 ]; int lexFrom = Convert.ToInt32( lexToFrom.Substring( 0, 2 ) ); int lexTo = Convert.ToInt32( lexToFrom.Substring( 1, 2 ) ); tempDef.PtrTypes.Add( pointerIndex ); tempDef.PtrOffsets.Add( pointerOffset ); tempDef.PtrPartOfSpeech.Add( pointerPartOfSpeech ); tempDef.PtrFromFields.Add( lexFrom ); tempDef.PtrToFields.Add( lexTo ); if( AssertDatabaseType( dbFileName, DbPartOfSpeechType.Adj ) && tempDef.DefinitionType == Constants.DONT_KNOW ) { if( pointerIndex == Constants.PointerTypeContants.ANTPTR ) { tempDef.DefinitionType = Constants.DIRECT_ANT; } else if( pointerIndex == Constants.PointerTypeContants.PERTPTR ) { foundPert = true; } } } i += ( tempDef.PtrCount * 4 ); if( AssertDatabaseType( dbFileName, DbPartOfSpeechType.Adj ) && tempDef.DefinitionType == Constants.DONT_KNOW && foundPert ) { tempDef.DefinitionType = Constants.PERTAINY; } if( AssertDatabaseType( dbFileName, DbPartOfSpeechType.Verb ) ) { int verbFrames = Convert.ToInt32( tokens[ i ] ); tempDef.VerbFrameCount = verbFrames; i++; for( int j = i; j < i + ( tempDef.VerbFrameCount * 3 ); j += 3 ) { int frameId = Convert.ToInt32( tokens[ j + 1 ] ); int frameTo = Convert.ToInt32( tokens[ j + 2 ] ); tempDef.FrameIds.Add( frameId ); tempDef.FrameToFields.Add( frameTo ); } i += ( tempDef.VerbFrameCount * 3 ); } i++; string definition = string.Join( " ", tokens, i, tokens.Length - i ); tempDef.DefinitionText = definition; tempDef.SenseNumbers = new List<int>( new int[ tempDef.WordCount ] ); for( int j = 0; j < tempDef.WordCount; j++ ) { tempDef.SenseNumbers[ j ] = GetSearchSense( tempDef, j ); } } retVal = tempDef; } catch { retVal = null; } return retVal; }