Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
rockmoose
SQL Natt Alfen
3279 Posts |
|
rrb
SQLTeam Poet Laureate
1479 Posts |
Posted - 2005-05-08 : 20:24:48
|
Worth pointing out to him that his example "642-394-6369 can be phrased as nice-window" is erroneous, since "nice" is not in his English Word list.... PS - Working on a solution--I hope that when I die someone will say of me "That guy sure owed me a lot of money" |
 |
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-05-09 : 04:25:56
|
Yes, I saw that mentioned in the comments of the blog:>""Nice" isn't in the dictionary list that you provided. ">"neither is "in", "win", or "fox". just to let everybody know for testing purposes. ">"Your result list is bigger because your word list contains "ox", which mine doesn't have. "Not near a solution yet...rockmoose |
 |
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2005-05-09 : 09:05:48
|
Here's a rough first draft ... note that in the middle there are two parameters: the phone number (@Number) and also the maximum number of numeric digits in your results (@MaxNumericDigits); i.e., if you set @MaxNumericDigits equal to 2, then "12-this" would be allowed, but not "128-his". Setting it 0 means no numbers are allow. Without that being set to reasonable value (i.e., >=2 ) the results are pretty boring and useless. The results are cool when you set it to 1.For testing and playing around, it is quickest to create the Words table first and insert the data, then comment out the add/insert/drop statements for that table in further runs.step 1: Create the words table (run once only)create table Words(Word varchar(100) primary key)set nocount oninsert into Words (Word)select 'a' union allselect 'able' union allselect 'about' union allselect 'account' union allselect 'acid' union allselect 'across' union allselect 'act' union allselect 'addition' union allselect 'adjustment' union allselect 'advertisement' union allselect 'after' union allselect 'again' union allselect 'against' union allselect 'agreement' union allselect 'air' union allselect 'all' union allselect 'almost' union allselect 'among' union allselect 'amount' union allselect 'amusement' union allselect 'and' union allselect 'angle' union allselect 'angry' union allselect 'animal' union allselect 'answer' union allselect 'ant' union allselect 'any' union allselect 'apparatus' union allselect 'apple' union allselect 'approval' union allselect 'arch' union allselect 'argument' union allselect 'arm' union allselect 'army' union allselect 'art' union allselect 'as' union allselect 'at' union allselect 'attack' union allselect 'attempt' union allselect 'attention' union allselect 'attraction' union allselect 'authority' union allselect 'automatic' union allselect 'awake' union allselect 'baby' union allselect 'back' union allselect 'bad' union allselect 'bag' union allselect 'balance' union allselect 'ball' union allselect 'band' union allselect 'base' union allselect 'basin' union allselect 'basket' union allselect 'bath' union allselect 'be' union allselect 'beautiful' union allselect 'because' union allselect 'bed' union allselect 'bee' union allselect 'before' union allselect 'behaviour' union allselect 'belief' union allselect 'bell' union allselect 'bent' union allselect 'berry' union allselect 'between' union allselect 'bird' union allselect 'birth' union allselect 'bit' union allselect 'bite' union allselect 'bitter' union allselect 'black' union allselect 'blade' union allselect 'blood' union allselect 'blow' union allselect 'blue' union allselect 'board' union allselect 'boat' union allselect 'body' union allselect 'boiling' union allselect 'bone' union allselect 'book' union allselect 'boot' union allselect 'bottle' union allselect 'box' union allselect 'boy' union allselect 'brain' union allselect 'brake' union allselect 'branch' union allselect 'brass' union allselect 'bread' union allselect 'breath' union allselect 'brick' union allselect 'bridge' union allselect 'bright' union allselect 'broken' union allselect 'brother' union allselect 'brown' union allselect 'brush' union allselect 'bucket' union allselect 'building' union allselect 'bulb' union allselect 'burn' union allselect 'burst' union allselect 'business' union allselect 'but' union allselect 'butter' union allselect 'button' union allselect 'by' union allselect 'cake' union allselect 'camera' union allselect 'canvas' union allselect 'card' union allselect 'care' union allselect 'carriage' union allselect 'cart' union allselect 'cat' union allselect 'cause' union allselect 'certain' union allselect 'chain' union allselect 'chalk' union allselect 'chance' union allselect 'change' union allselect 'cheap' union allselect 'cheese' union allselect 'chemical' union allselect 'chest' union allselect 'chief' union allselect 'chin' union allselect 'church' union allselect 'circle' union allselect 'clean' union allselect 'clear' union allselect 'clock' union allselect 'cloth' union allselect 'cloud' union allselect 'coal' union allselect 'coat' union allselect 'cold' union allselect 'collar' union allselect 'colour' union allselect 'comb' union allselect 'come' union allselect 'comfort' union allselect 'committee' union allselect 'common' union allselect 'company' union allselect 'comparison' union allselect 'competition' union allselect 'complete' union allselect 'complex' union allselect 'condition' union allselect 'connection' union allselect 'conscious' union allselect 'control' union allselect 'cook' union allselect 'copper' union allselect 'copy' union allselect 'cord' union allselect 'cork' union allselect 'cotton' union allselect 'cough' union allselect 'country' union allselect 'cover' union allselect 'cow' union allselect 'crack' union allselect 'credit' union allselect 'crime' union allselect 'cruel' union allselect 'crush' union allselect 'cry' union allselect 'cup' union allselect 'current' union allselect 'curtain' union allselect 'curve' union allselect 'cushion' union allselect 'damage' union allselect 'danger' union allselect 'dark' union allselect 'daughter' union allselect 'day' union allselect 'dead' union allselect 'dear' union allselect 'death' union allselect 'debt' union allselect 'decision' union allselect 'deep' union allselect 'degree' union allselect 'delicate' union allselect 'dependent' union allselect 'design' union allselect 'desire' union allselect 'destruction' union allselect 'detail' union allselect 'development' union allselect 'different' union allselect 'digestion' union allselect 'direction' union allselect 'dirty' union allselect 'discovery' union allselect 'discussion' union allselect 'disease' union allselect 'disgust' union allselect 'distance' union allselect 'distribution' union allselect 'division' union allselect 'do' union allselect 'dog' union allselect 'door' union allselect 'doubt' union allselect 'down' union allselect 'drain' union allselect 'drawer' union allselect 'dress' union allselect 'drink' union allselect 'driving' union allselect 'drop' union allselect 'dry' union allselect 'dust' union allselect 'ear' union allselect 'early' union allselect 'earth' union allselect 'east' union allselect 'edge' union allselect 'education' union allselect 'effect' union allselect 'egg' union allselect 'elastic' union allselect 'electric' union allselect 'end' union allselect 'engine' union allselect 'enough' union allselect 'equal' union allselect 'error' union allselect 'even' union allselect 'event' union allselect 'ever' union allselect 'every' union allselect 'example' union allselect 'exchange' union allselect 'existence' union allselect 'expansion' union allselect 'experience' union allselect 'expert' union allselect 'eye' union allselect 'face' union allselect 'fact' union allselect 'fall' union allselect 'FALSE' union allselect 'family' union allselect 'far' union allselect 'farm' union allselect 'fat' union allselect 'father' union allselect 'fear' union allselect 'feather' union allselect 'feeble' union allselect 'feeling' union allselect 'female' union allselect 'fertile' union allselect 'fiction' union allselect 'field' union allselect 'fight' union allselect 'finger' union allselect 'fire' union allselect 'first' union allselect 'fish' union allselect 'fixed' union allselect 'flag' union allselect 'flame' union allselect 'flat' union allselect 'flight' union allselect 'floor' union allselect 'flower' union allselect 'fly' union allselect 'fold' union allselect 'food' union allselect 'foolish' union allselect 'foot' union allselect 'for' union allselect 'force' union allselect 'fork' union allselect 'form' union allselect 'forward' union allselect 'fowl' union allselect 'frame' union allselect 'free' union allselect 'frequent' union allselect 'friend' union allselect 'from' union allselect 'front' union allselect 'fruit' union allselect 'full' union allselect 'future' union allselect 'garden' union allselect 'general' union allselect 'get' union allselect 'girl' union allselect 'give' union allselect 'glass' union allselect 'glove' union allselect 'go' union allselect 'goat' union allselect 'gold' union allselect 'good' union allselect 'government' union allselect 'grain' union allselect 'grass' union allselect 'great' union allselect 'green' union allselect 'grey' union allselect 'grip' union allselect 'group' union allselect 'growth' union allselect 'guide' union allselect 'gun' union allselect 'hair' union allselect 'hammer' union allselect 'hand' union allselect 'hanging' union allselect 'happy' union allselect 'harbour' union allselect 'hard' union allselect 'harmony' union allselect 'hat' union allselect 'hate' union allselect 'have' union allselect 'he' union allselect 'head' union allselect 'healthy' union allselect 'hear' union allselect 'hearing' union allselect 'heart' union allselect 'heat' union allselect 'help' union allselect 'high' union allselect 'history' union allselect 'hole' union allselect 'hollow' union allselect 'hook' union allselect 'hope' union allselect 'horn' union allselect 'horse' union allselect 'hospital' union allselect 'hour' union allselect 'house' union allselect 'how' union allselect 'humour' union allselect 'I' union allselect 'ice' union allselect 'idea' union allselect 'if' union allselect 'ill' union allselect 'important' union allselect 'impulse' union allselect 'in' union allselect 'increase' union allselect 'industry' union allselect 'ink' union allselect 'insect' union allselect 'instrument' union allselect 'insurance' union allselect 'interest' union allselect 'invention' union allselect 'iron' union allselect 'island' union allselect 'jelly' union allselect 'jewel' union allselect 'join' union allselect 'journey' union allselect 'judge' union allselect 'jump' union allselect 'keep' union allselect 'kettle' union allselect 'key' union allselect 'kick' union allselect 'kind' union allselect 'kiss' union allselect 'knee' union allselect 'knife' union allselect 'knot' union allselect 'knowledge' union allselect 'land' union allselect 'language' union allselect 'last' union allselect 'late' union allselect 'laugh' union allselect 'law' union allselect 'lead' union allselect 'leaf' union allselect 'learning' union allselect 'leather' union allselect 'left' union allselect 'leg' union allselect 'let' union allselect 'letter' union allselect 'level' union allselect 'library' union allselect 'lift' union allselect 'light' union allselect 'like' union allselect 'limit' union allselect 'line' union allselect 'linen' union allselect 'lip' union allselect 'liquid' union allselect 'list' union allselect 'little' union allselect 'living' union allselect 'lock' union allselect 'long' union allselect 'look' union allselect 'loose' union allselect 'loss' union allselect 'loud' union allselect 'love' union allselect 'low' union allselect 'machine' union allselect 'make' union allselect 'male' union allselect 'man' union allselect 'manager' union allselect 'map' union allselect 'mark' union allselect 'market' union allselect 'married' union allselect 'mass' union allselect 'match' union allselect 'material' union allselect 'may' union allselect 'meal' union allselect 'measure' union allselect 'meat' union allselect 'medical' union allselect 'meeting' union allselect 'memory' union allselect 'metal' union allselect 'middle' union allselect 'military' union allselect 'milk' union allselect 'mind' union allselect 'mine' union allselect 'minute' union allselect 'mist' union allselect 'mixed' union allselect 'money' union allselect 'monkey' union allselect 'month' union allselect 'moon' union allselect 'morning' union allselect 'mother' union allselect 'motion' union allselect 'mountain' union allselect 'mouth' union allselect 'move' union allselect 'much' union allselect 'muscle' union allselect 'music' union allselect 'nail' union allselect 'name' union allselect 'narrow' union allselect 'nation' union allselect 'natural' union allselect 'near' union allselect 'necessary' union allselect 'neck' union allselect 'need' union allselect 'needle' union allselect 'nerve' union allselect 'net' union allselect 'new' union allselect 'news' union allselect 'nice' union allselect 'night' union allselect 'no' union allselect 'noise' union allselect 'normal' union allselect 'north' union allselect 'nose' union allselect 'not' union allselect 'note' union allselect 'now' union allselect 'number' union allselect 'nut' union allselect 'observation' union allselect 'of' union allselect 'off' union allselect 'offer' union allselect 'office' union allselect 'oil' union allselect 'old' union allselect 'on' union allselect 'only' union allselect 'open' union allselect 'operation' union allselect 'opinion' union allselect 'opposite' union allselect 'or' union allselect 'orange' union allselect 'order' union allselect 'organization' union allselect 'ornament' union allselect 'other' union allselect 'out' union allselect 'oven' union allselect 'over' union allselect 'owner' union allselect 'page' union allselect 'pain' union allselect 'paint' union allselect 'paper' union allselect 'parallel' union allselect 'parcel' union allselect 'part' union allselect 'past' union allselect 'paste' union allselect 'payment' union allselect 'peace' union allselect 'pen' union allselect 'pencil' union allselect 'person' union allselect 'physical' union allselect 'picture' union allselect 'pig' union allselect 'pin' union allselect 'pipe' union allselect 'place' union allselect 'plane' union allselect 'plant' union allselect 'plate' union allselect 'play' union allselect 'please' union allselect 'pleasure' union allselect 'plough' union allselect 'pocket' union allselect 'point' union allselect 'poison' union allselect 'polish' union allselect 'political' union allselect 'poor' union allselect 'porter' union allselect 'position' union allselect 'possible' union allselect 'pot' union allselect 'potato' union allselect 'powder' union allselect 'power' union allselect 'present' union allselect 'price' union allselect 'print' union allselect 'prison' union allselect 'private' union allselect 'probable' union allselect 'process' union allselect 'produce' union allselect 'profit' union allselect 'property' union allselect 'prose' union allselect 'protest' union allselect 'public' union allselect 'pull' union allselect 'pump' union allselect 'punishment' union allselect 'purpose' union allselect 'push' union allselect 'put' union allselect 'quality' union allselect 'question' union allselect 'quick' union allselect 'quiet' union allselect 'quite' union allselect 'rail' union allselect 'rain' union allselect 'range' union allselect 'rat' union allselect 'rate' union allselect 'ray' union allselect 'reaction' union allselect 'reading' union allselect 'ready' union allselect 'reason' union allselect 'receipt' union allselect 'record' union allselect 'red' union allselect 'regret' union allselect 'regular' union allselect 'relation' union allselect 'religion' union allselect 'representative' union allselect 'request' union allselect 'respect' union allselect 'responsible' union allselect 'rest' union allselect 'reward' union allselect 'rhythm' union allselect 'rice' union allselect 'right' union allselect 'ring' union allselect 'river' union allselect 'road' union allselect 'rod' union allselect 'roll' union allselect 'roof' union allselect 'room' union allselect 'root' union allselect 'rough' union allselect 'round' union allselect 'rub' union allselect 'rule' union allselect 'run' union allselect 'sad' union allselect 'safe' union allselect 'sail' union allselect 'salt' union allselect 'same' union allselect 'sand' union allselect 'say' union allselect 'scale' union allselect 'school' union allselect 'science' union allselect 'scissors' union allselect 'screw' union allselect 'sea' union allselect 'seat' union allselect 'second' union allselect 'secret' union allselect 'secretary' union allselect 'see' union allselect 'seed' union allselect 'seem' union allselect 'selection' union allselect 'self' union allselect 'send' union allselect 'sense' union allselect 'separate' union allselect 'serious' union allselect 'servant' union allselect 'sex' union allselect 'shade' union allselect 'shake' union allselect 'shame' union allselect 'sharp' union allselect 'sheep' union allselect 'shelf' union allselect 'ship' union allselect 'shirt' union allselect 'shock' union allselect 'shoe' union allselect 'short' union allselect 'shut' union allselect 'side' union allselect 'sign' union allselect 'silk' union allselect 'silver' union allselect 'simple' union allselect 'sister' union allselect 'size' union allselect 'skin' union allselect 'skirt' union allselect 'sky' union allselect 'sleep' union allselect 'slip' union allselect 'slope' union allselect 'slow' union allselect 'small' union allselect 'smash' union allselect 'smell' union allselect 'smile' union allselect 'smoke' union allselect 'smooth' union allselect 'snake' union allselect 'sneeze' union allselect 'snow' union allselect 'so' union allselect 'soap' union allselect 'society' union allselect 'sock' union allselect 'soft' union allselect 'solid' union allselect 'some' union allselect 'son' union allselect 'song' union allselect 'sort' union allselect 'sound' union allselect 'soup' union allselect 'south' union allselect 'space' union allselect 'spade' union allselect 'special' union allselect 'sponge' union allselect 'spoon' union allselect 'spring' union allselect 'square' union allselect 'stage' union allselect 'stamp' union allselect 'star' union allselect 'start' union allselect 'statement' union allselect 'station' union allselect 'steam' union allselect 'steel' union allselect 'stem' union allselect 'step' union allselect 'stick' union allselect 'sticky' union allselect 'stiff' union allselect 'still' union allselect 'stitch' union allselect 'stocking' union allselect 'stomach' union allselect 'stone' union allselect 'stop' union allselect 'store' union allselect 'story' union allselect 'straight' union allselect 'strange' union allselect 'street' union allselect 'stretch' union allselect 'strong' union allselect 'structure' union allselect 'substance' union allselect 'such' union allselect 'sudden' union allselect 'sugar' union allselect 'suggestion' union allselect 'summer' union allselect 'sun' union allselect 'support' union allselect 'surprise' union allselect 'sweet' union allselect 'swim' union allselect 'system' union allselect 'table' union allselect 'tail' union allselect 'take' union allselect 'talk' union allselect 'tall' union allselect 'taste' union allselect 'tax' union allselect 'teaching' union allselect 'tendency' union allselect 'test' union allselect 'than' union allselect 'that' union allselect 'the' union allselect 'then' union allselect 'theory' union allselect 'there' union allselect 'thick' union allselect 'thin' union allselect 'thing' union allselect 'this' union allselect 'thought' union allselect 'thread' union allselect 'throat' union allselect 'through' union allselect 'thumb' union allselect 'thunder' union allselect 'ticket' union allselect 'tight' union allselect 'till' union allselect 'time' union allselect 'tin' union allselect 'tired' union allselect 'to' union allselect 'toe' union allselect 'together' union allselect 'tomorrow' union allselect 'tongue' union allselect 'tooth' union allselect 'top' union allselect 'touch' union allselect 'town' union allselect 'trade' union allselect 'train' union allselect 'transport' union allselect 'tray' union allselect 'tree' union allselect 'trick' union allselect 'trouble' union allselect 'trousers' union allselect 'TRUE' union allselect 'turn' union allselect 'twist' union allselect 'umbrella' union allselect 'under' union allselect 'unit' union allselect 'up' union allselect 'use' union allselect 'value' union allselect 'verse' union allselect 'very' union allselect 'vessel' union allselect 'view' union allselect 'violent' union allselect 'voice' union allselect 'waiting' union allselect 'walk' union allselect 'wall' union allselect 'war' union allselect 'warm' union allselect 'wash' union allselect 'waste' union allselect 'watch' union allselect 'water' union allselect 'wave' union allselect 'wax' union allselect 'way' union allselect 'weather' union allselect 'week' union allselect 'weight' union allselect 'well' union allselect 'west' union allselect 'wet' union allselect 'wheel' union allselect 'when' union allselect 'where' union allselect 'while' union allselect 'whip' union allselect 'whistle' union allselect 'white' union allselect 'who' union allselect 'why' union allselect 'wide' union allselect 'will' union allselect 'wind' union allselect 'window' union allselect 'wine' union allselect 'wing' union allselect 'winter' union allselect 'wire' union allselect 'wise' union allselect 'with' union allselect 'woman' union allselect 'wood' union allselect 'wool' union allselect 'word' union allselect 'work' union allselect 'worm' union allselect 'wound' union allselect 'writing' union allselect 'wrong' union allselect 'year' union allselect 'yellow' union allselect 'yes' union allselect 'yesterday' union allselect 'you' union allselect 'young' union allselect 'win' union all select 'fox'set nocount off step 2: here is the actual solution:--the input:declare @number varchar(20);declare @MaxNumericDigits int;set @MaxNumericDigits = 1; -- only allow 1 digit numbers to be returnedset @number = '642-394-6369'-- our numbers lookup table:create table Numbers(Number int, Letter char(1))insert into Numbersselect 2,'a' union select 2,'b' union select 2,'c' unionselect 3,'d' union select 3,'e' union select 3,'f' unionselect 4,'g' union select 4,'h' union select 4,'i' unionselect 5,'j' union select 5,'k' union select 5,'l' unionselect 6,'m' union select 6,'n' union select 6,'o' unionselect 7,'p' union select 7,'q' union select 7,'r' union select 7,'s' unionselect 8,'t' union select 8,'u' union select 8,'v' unionselect 9,'w' union select 9,'x' union select 9,'y' union select 9,'z' declare @i int;declare @c int;-- our "work" table:create table tmp(Position int, Digit int, CurrWord varchar(20), Result varchar(100), Numbers int)set @number = replace(@number,'-','')-- get things started:insert into tmp select 0,0, '','',0-- and off we go:set @i= 1;while (@i <= len(@number))begin set @c = convert(int, substring(@number,@i,1)) -- add new letters for words that are forming: insert into tmp (Position, Digit, CurrWord, Result, Numbers) select distinct @i, @c, case when CurrWord + Letter = Word then '' else CurrWord + Letter end, Result + Letter, 0 from Numbers N inner join tmp on tmp.Position = (@i-1) inner join Words W on W.Word LIKE CurrWord + Letter + '%' where N.Number = @c -- Add numbers as well, but only if a word is previously complete: insert into tmp (Position, Digit, CurrWord, Result, Numbers) select distinct @i, @c, '', Result + convert(char(1), @c), tmp.Numbers + 1 from tmp where tmp.Position = (@i-1) and (tmp.Numbers < @MaxNumericDigits) and (exists (select 1 from Words where Words.Word = CurrWord) or CurrWord = '') set @i = @i + 1end-- and the answerselect Resultfrom tmpwhere (Position=len(@number)) and (exists (select 1 from Words where Words.Word = CurrWord) or CurrWord = '')order by Resultgodrop table Numbersdrop table tmp--drop table Words -- uncomment this out to clean it all up when you are done - Jeff |
 |
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-05-09 : 11:14:17
|
The INSERT is exactly the same as Jeff's so I cut a bunch out, to save post space.You will have to copy the INSERT from his post, to get the dictionary data.The CREATE TABLE is a bit different so You will have to use this one.At first I had some problems when a word could appear several times in the phonenumber.Actually i stumbled on this solution while trying to solve that problem.(by using STUFF iof REPLACE)-- We need a helper function to work out the number for a phrase-- e.g. "about" = "22688"create function dbo.fnGetNr(@word varchar(15))returns varchar(15)asbegin declare @nr varchar(15); set @nr = '' declare @i tinyint; set @i = 1 while @i <= len(@word) begin set @nr = @nr + case when substring(@word,@i,1) in('a','b','c') then '2' when substring(@word,@i,1) in('d','e','f') then '3' when substring(@word,@i,1) in('g','h','i') then '4' when substring(@word,@i,1) in('j','k','l') then '5' when substring(@word,@i,1) in('m','n','o') then '6' when substring(@word,@i,1) in('p','q','r','s') then '7' when substring(@word,@i,1) in('t','u','v') then '8' when substring(@word,@i,1) in('w','x','y','z') then '9' end set @i = @i + 1 end return @nrendgo-- This is the same table as Jeff's, but with a nbr column that contains the word's equiv phonenbrcreate table Words(Word varchar(15) primary key, nbr varchar(15) null)set nocount oninsert into Words (Word)select 'a' union allselect 'able' union all..-- ... and abunch that You will have to cut from Jeff's post..select 'win' union allselect 'fox'-- Update the helper column in Words using the helper functionupdate Words set nbr = dbo.fnGetNr(Word)alter table Words alter column nbr varchar(15) not null -- feels much better nowset nocount offgo/* START WORKING */-- create working tablecreate table #phrases(phrase varchar(15))declare @phonenumber varchar(15)set @phonenumber = '6423946369'-- set @phonenumber = '2583268853' --("bluebottle")truncate table #phrases -- clear tableEDIT: Sorry, had references to old dictionary tableinsert #phrases(phrase) values(@phonenumber) -- a number is part of itselfwhile @@rowcount > 0 insert #phrases(phrase) select distinct stuff(phrase,charindex(nbr,phrase),len(Word),Word) from Words join #phrases on charindex(nbr,phrase) > 0 where stuff(phrase,charindex(nbr,phrase),len(Word),Word) not in(select phrase from #phrases)-- the resultselect phrase from #phrases order by 1-- cleanupdrop table #phrasesgo/* END WORKING */drop table Wordsgodrop function dbo.fnGetNrgo rockmoose |
 |
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-05-09 : 11:50:10
|
Compared to Jeff's solution I find only 172 answers, (Jeff 184)These are missing:It's something with that 'I' occurring in pos 2 + 6, maybe I can fix it...Result ------------------------------ 64239i636964239i6do964239i6fox64239iof6964a39i636964a39i6do964a39i6fox64a39iof6964be9i636964be9i6do964be9i6fox64be9iof69PS.Note to self:Don't post challenges when You have work to do rockmoose |
 |
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-05-10 : 09:58:48
|
The set-based solution:declare @phonenumber varchar(15)set @phonenumber = '6423946369'--Select word From wordsDeclare @pattern varchar(1000)Select @pattern = Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(@phoneNumber,'2','[abc ]'),'3','[def ]'),'4','[ghi ]'),'5','[jkl ]'),'6','[mno ]'),'7','[pqrs ]'),'8','[tuv ]'),'9','[wxyz ]')--Select @patternDrop Table #tempWordsSelect phone = @phonenumber, pattern = @pattern, word, pos00 = case when len(word)<=10 and left(replicate(' ',0)+word+replicate(' ',10),10) like @pattern then 1 else 0 end, pos01 = case when len(word)<=9 and left(replicate(' ',1)+word+replicate(' ',10),10) like @pattern then 1 else 0 end, pos02 = case when len(word)<=8 and left(replicate(' ',2)+word+replicate(' ',10),10) like @pattern then 1 else 0 end, pos03 = case when len(word)<=7 and left(replicate(' ',3)+word+replicate(' ',10),10) like @pattern then 1 else 0 end, pos04 = case when len(word)<=6 and left(replicate(' ',4)+word+replicate(' ',10),10) like @pattern then 1 else 0 end, pos05 = case when len(word)<=5 and left(replicate(' ',5)+word+replicate(' ',10),10) like @pattern then 1 else 0 end, pos06 = case when len(word)<=4 and left(replicate(' ',6)+word+replicate(' ',10),10) like @pattern then 1 else 0 end, pos07 = case when len(word)<=3 and left(replicate(' ',7)+word+replicate(' ',10),10) like @pattern then 1 else 0 end, pos08 = case when len(word)<=2 and left(replicate(' ',8)+word+replicate(' ',10),10) like @pattern then 1 else 0 end, pos09 = case when len(word)<=1 and left(replicate(' ',9)+word+replicate(' ',10),10) like @pattern then 1 else 0 endInto #tempWordsFrom wordsWhere (left(replicate(' ',0)+word+replicate(' ',10),10) like @pattern and len(word)<=10)or (left(replicate(' ',1)+word+replicate(' ',10),10) like @pattern and len(word)<=9)or (left(replicate(' ',2)+word+replicate(' ',10),10) like @pattern and len(word)<=8)or (left(replicate(' ',3)+word+replicate(' ',10),10) like @pattern and len(word)<=7)or (left(replicate(' ',4)+word+replicate(' ',10),10) like @pattern and len(word)<=6)or (left(replicate(' ',5)+word+replicate(' ',10),10) like @pattern and len(word)<=5)or (left(replicate(' ',6)+word+replicate(' ',10),10) like @pattern and len(word)<=4)or (left(replicate(' ',7)+word+replicate(' ',10),10) like @pattern and len(word)<=3)or (left(replicate(' ',8)+word+replicate(' ',10),10) like @pattern and len(word)<=2)or (left(replicate(' ',9)+word+replicate(' ',10),10) like @pattern and len(word)<=1)Drop Table #wordPositionsSelect * Into #wordPositionsFrom ( Select phone, pattern, word, startPos = 0, endPos = len(word)-1 From #tempWords Where pos00=1 Union Select phone, pattern, word, startPos = 1, endPos = len(word)-0 From #tempWords Where pos01=1 Union Select phone, pattern, word, startPos = 2, endPos = len(word)+1 From #tempWords Where pos02=1 Union Select phone, pattern, word, startPos = 3, endPos = len(word)+2 From #tempWords Where pos03=1 Union Select phone, pattern, word, startPos = 4, endPos = len(word)+3 From #tempWords Where pos04=1 Union Select phone, pattern, word, startPos = 5, endPos = len(word)+4 From #tempWords Where pos05=1 Union Select phone, pattern, word, startPos = 6, endPos = len(word)+5 From #tempWords Where pos06=1 Union Select phone, pattern, word, startPos = 7, endPos = len(word)+6 From #tempWords Where pos07=1 Union Select phone, pattern, word, startPos = 8, endPos = len(word)+7 From #tempWords Where pos08=1 Union Select phone, pattern, word, startPos = 9, endPos = len(word)+8 From #tempWords Where pos09=1 ) A Declare @wordCnt intSet @wordCnt = 1Drop Table #finalSetSelect phonePhrase=stuff(replicate(' ',10),a.startPos+1,a.endPos-a.startPos+1,a.word), wordCnt = @wordCntInto #finalSetFrom #wordPositions aWhile exists(Select * From #finalSet a Inner Join #wordPositions b on substring(a.phonePhrase,b.startPos+1,b.endPos-b.startPos+1)=Replicate(' ',b.endPos-b.startPos+1) Where a.wordCnt = @wordCnt)Begin Insert Into #finalSet Select phonephrase = stuff(phonePhrase,b.startPos+1,b.endPos-b.startPos+1,b.word), wordCnt = @wordCnt+1 From #finalSet a Inner Join #wordPositions b on substring(a.phonePhrase,b.startPos+1,b.endPos-b.startPos+1)=Replicate(' ',b.endPos-b.startPos+1) Where a.wordCnt = @wordCnt Set @wordCnt = @wordCnt+1Endwhile exists(Select * From #finalSet Where phonePhrase like '% %')Begin Update #finalSet Set phonephrase = stuff(phonephrase,charindex(' ',phonephrase),1,substring(@phonenumber,charindex(' ',phonephrase),1)) From #finalSet Where phonePhrase like '% %'EndEDIT: makes for better viewing Select * From (Select distinct * From #finalSet) AOrder By len(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(phonephrase,'0',''),'1',''),'2',''),'3',''),'4',''),'5',''),'6',''),'7',''),'8',''),'9','')) Desc finds 183 answers... maybe someone can find the 184th for me Corey Secret Service Agent: Mr. President, you're urinating on me.President Lyndon Johnson: I know I am. It's my prerogative. |
 |
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-05-10 : 10:12:13
|
#184: 6423946369 Glad to see You Corey!rockmoose |
 |
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2005-05-10 : 10:17:01
|
Why do you not consider the other solutions set-based?- Jeff |
 |
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-05-10 : 10:25:23
|
Well I re-read you solution jeff and I don't think I read in depth enough the first time. I would agree that yours is set-based.Rock (sorry ) I'm still not sure I think yours counts as set based. ultimately through use of your function you loop through each letter of each word in the dictionary. Depending on the size of the dictionary that could become a problem. Granted that you only do this once and save the result as part of the table, it still seems awkward to me. I would personally vote for Jeff's solution now that I have re-read it. Corey Secret Service Agent: Mr. President, you're urinating on me.President Lyndon Johnson: I know I am. It's my prerogative. |
 |
|
|
|
|
|
|