





/* Stored Procedure usp_a125RandomPhraseRandomExpand

*

* Created By:	Gus Mueller

* Date:		 8.22.00 

* Purpose:	Gets fully expanded random phrase starting with a Token

* 

*/ 

Alter  Procedure usp_a125RandomPhraseRandomExpand

	(

		@vcToken varchar(20),

		@iApplication int,

		@rSeed real=null

	)

	AS

	set nocount on 

	if @rSeed is null

		begin

			set @rSeed=rand()*24487

		end

	declare @iWeightMin int

	declare @iWeightMax int

	declare @iDelta int

	declare @rNonLinearity real

	declare @iMaxRows int

	declare @iThis int

	declare @vcOut varchar(8000)

	declare @iBracketPosition int

	declare @iEndBracketPosition int

	declare @vcThisToken varchar(20)

	declare @vcReplacement varchar(4000)

	declare @vcToBeReplaced varchar(20)

	declare @iIter int

	declare @rScaleFactor real

	set @iIter=0

	set @vcThisToken=@vcToken

	set @vcOut= '[' + @vcToken + ']'

	create table #Temp

		(

		RowNum integer not null identity,

		vcPhrase varchar(2000) null,

		iWeight int null

		)

		

	while @vcThisToken is not null and @iIter<999

	begin

		truncate table #Temp

		

		insert into #Temp(vcPhrase, iWeight)

		select 

			vcPhrase, iWeight

		from 

			a125RandomPhrase

		where 

			iApplication=@iApplication

		and

			iWeight>0

		and 

			vcToken=@vcThisToken

		order by iWeight desc

		

		

		

		set @iWeightMin=

			(

			select

				min(iWeight)

			from

				#Temp

			)

			

		set @iWeightMax=

			(

			select

				max(iWeight)

			from

				#Temp

			)

		set @iDelta=@iWeightMax- @iWeightMin

		set @rNonLinearity = convert(real(60, 30), @iDelta)+1

		

		set  @iMaxRows=

			(

			select 

				max(rownum)

			from 

				#Temp

			)

		--select * from #temp

		

		--select @iMaxRows as 'max'

		set @rScaleFactor = convert(real(60, 30),power(rand(),@rNonLinearity))

		set @rSeed=rand(@rseed)*24487

		--select @rseed as 'poo'

		set @iThis=1+convert(integer, ((-.05+@iMaxRows)* @rScaleFactor))

		--select @rNonLinearity as 'nonlinear',  @iThis as 'iThis', @rScaleFactor as 'scale'

		--select rand() as 'random'

		--select @iThis as 'this'

		set @vcReplacement=

			(

			select 

				vcPhrase 

			from

				#Temp

			where

				rownum=@iThis

			)



		if @vcOut='' 

			begin

				set @vcOut=@vcReplacement

			end

		else

			

			begin

				set @iBracketPosition=charindex('[', @vcOut)-1

				set @iEndBracketPosition=charindex(']',@vcOut, @iBracketPosition)+1

				--select  @vcReplacement as 'replace', @iBracketPosition as 'bracket1', @iEndBracketPosition as 'bracket2',@vcOut as 'vcOut'

				if @iBracketPosition>-1 and @iEndBracketPosition>@iBracketPosition

					begin

						set @vcOut=left(@vcOut, @iBracketPosition) + @vcReplacement + substring(@vcOut,@iEndBracketPosition,len(@vcOut)-@iEndBracketPosition+1)

					end

			end

			



		set @iBracketPosition=charindex('[', @vcOut)+1

		set @iEndBracketPosition=charindex(']',@vcOut, @iBracketPosition)

		if @iBracketPosition is not null and @iEndBracketPosition is not null and  @iBracketPosition>-1 and @iEndBracketPosition>0

			begin

				set @vcThisToken=substring(@vcOut, @iBracketPosition, @iEndBracketPosition-@iBracketPosition)

			end

		else

			begin

				set @vcThisToken=null

			end

		set @iIter=@iIter+1

		--select @vcThisToken as 'nexttoken'

		continue

	end

	--capitalize first letter

	set @vcOut=upper(substring(@vcOut, 1, 1)) + substring(@vcOut, 2, len(@vcOut)-1)

	select @vcOut as 'vcPhrase'

	return(0)

























