Word Scrambler in Smalltalk

Um meme na Internet alega que uma pesquisa descobriu que conseguimos ler com alguma desenvoltura uma frase em que as letras internas das palavras tenha sido embaralhadas.  Vários sites na Internet oferecem oportunidades para experimentar isto. Um deles é o Scrambled Words. No site The Word Scramble Problem procura-se voltar ao texto original (em inglês) a partir do texto embaralhado. Logo abaixo publico um código em Smalltalk para embaralhar frases (um problema bem mais fácil).

word-scramble

Em outros dois artigos, Cambridge Word Scramble Study: It’s Fake Already! e Psycholinguistic evidence on scrambled letters in reading são expostas as razões para o fato de conseguirmos ler a maior parte das palavras. Palavras pequenas e o contexto ajudam bastante.

Object subclass: #MWSWord
	instanceVariableNames: 'content punctuation'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'MiddleWordScrambler-Core'!
!MWSWord commentStamp: 'FranciscoAryMartins 8/8/2015 14:46' prior: 0!
Minhas instâncias  armazenam palavras separadas em duas partes: uma parte sem a pontuação e a outra para a pontuação no final. 

Trabalho em conjunto com a classe  MWSMiddleWordScrambler.

O serviço mais relevante é fornecido para a classe MWSMiddleWordScrambler através do método #scramble. 

Public API and Key Messages
- hasPunctuation: aString   
- scramble 
- sort  
- sorted

Exemplo:
MWSWord newFrom: 'abcdefgh!!') scrambled
 
Instance Variables
	content:		<String>
	punctuation:		<Character>
!


!MWSWord methodsFor: 'testing' stamp: 'FranciscoAryMartins 8/8/2015 09:55'!
hasPunctuation: aString 
	| punctuationChars |
	punctuationChars := #($. $, $!! $? $; $: $" $' $- $( $) $`).
	^ punctuationChars includes: aString last ! !


!MWSWord methodsFor: 'shuffling' stamp: 'FranciscoAryMartins 8/8/2015 10:04'!
scrambled
	^ self copy scramble! !

!MWSWord methodsFor: 'shuffling' stamp: 'FranciscoAryMartins 8/8/2015 16:03'!
scramble
	content size > 3 ifTrue: [
		content := content first asString
		, (content allButFirst allButLast shuffle)
		, content last asString ] ! !


!MWSWord methodsFor: 'sorting' stamp: 'FranciscoAryMartins 8/8/2015 12:09'!
sort
	content sort ! !

!MWSWord methodsFor: 'sorting' stamp: 'FranciscoAryMartins 8/8/2015 12:10'!
sorted
	^ self copy sort! !


!MWSWord methodsFor: 'printing' stamp: 'FranciscoAryMartins 8/8/2015 12:05'!
printString
	^ content, punctuation asString! !


!MWSWord methodsFor: 'initialization' stamp: 'FranciscoAryMartins 8/8/2015 09:55'!
intializeFrom: aString 
	(self hasPunctuation: aString) 
		ifFalse: [content := aString. punctuation := String empty ]
		ifTrue:  [ content := aString allButLast. punctuation := aString last ]! !

"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!

MWSWord class
	instanceVariableNames: ''!

!MWSWord class methodsFor: 'as yet unclassified' stamp: 'FranciscoAryMartins 8/8/2015 09:48'!
newFrom: aString
	^ MWSWord new intializeFrom: aString! !


Object subclass: #MWSWordCollection
	instanceVariableNames: 'collection'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'MiddleWordScrambler-Core'!
!MWSWordCollection commentStamp: 'FranciscoAryMartins 8/8/2015 14:46' prior: 0!
Minhas instâncias  armazenam uma coleção de palavras separadas encapsuladas em instâncias de MWSWord. 

Trabalho em conjunto com a classe  MWSMiddleWordScrambler.

O serviço mais relevante é fornecido para a classe MWSMiddleWordScrambler através do método #scramble. 

Public API and Key Messages
- scramble 
- scrambled

Exemplo:
(MWSWordCollection newFrom: 'Ivo viu a uva?') scrambled
 
 Instance Variables
	collection:		<Array>
!


!MWSWordCollection methodsFor: 'initialization' stamp: 'FranciscoAryMartins 8/8/2015 09:43'!
initializeCollectionFrom: aPhrase 
	collection := aPhrase substrings collect: [ :word | self createWordFrom: word ]! !


!MWSWordCollection methodsFor: 'printing' stamp: 'FranciscoAryMartins 8/8/2015 12:23'!
printString
	^ ((collection collect: [ :word | word asString ]) inject: (WriteStream on: String empty) into: [ :stream :word | 
		stream isEmpty ifTrue: [ stream nextPutAll: word. stream ] ifFalse: [stream nextPutAll: ' '. stream nextPutAll: word. stream ] ]) contents! !


!MWSWordCollection methodsFor: 'instance creation' stamp: 'FranciscoAryMartins 8/8/2015 09:47'!
createWordFrom: aString 
	^ MWSWord newFrom: aString! !


!MWSWordCollection methodsFor: 'shuffling' stamp: 'FranciscoAryMartins 8/8/2015 10:36'!
scrambled
"	^ collection collect: [ :word | word scrambled ]"
	^ self copy scramble! !

!MWSWordCollection methodsFor: 'shuffling' stamp: 'FranciscoAryMartins 8/8/2015 10:38'!
scramble
	collection do: [ :word | word scramble ]! !

"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!

MWSWordCollection class
	instanceVariableNames: ''!

!MWSWordCollection class methodsFor: 'as yet unclassified' stamp: 'FranciscoAryMartins 8/8/2015 09:43'!
newFrom: aPhrase
	^ self new initializeCollectionFrom: aPhrase! !


Object subclass: #MWSMiddleWordScrambler
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'MiddleWordScrambler-Core'!
!MWSMiddleWordScrambler commentStamp: 'FranciscoAryMartins 8/8/2015 14:43' prior: 0!
Minhas instâncias podem embaralhar o "miolo" das palavras em uma frase.

Public API and Key Messages
- scramble: aPhrase 

Exemplo:
|text|
text := 'O ornitorrinco é um mamífero semiaquático natural da Austrália e Tasmânia.'.
MWSMiddleWordScrambler new scramble: text
!


!MWSMiddleWordScrambler methodsFor: 'private' stamp: 'FranciscoAryMartins 8/8/2015 09:45'!
createWordCollectionFrom: aPhrase 
	^ MWSWordCollection newFrom: aPhrase ! !


!MWSMiddleWordScrambler methodsFor: 'shuffling' stamp: 'FranciscoAryMartins 8/8/2015 14:30'!
scramble: aPhrase 
	^ (self createWordCollectionFrom: aPhrase) scramble asString ! !
TestCase subclass: #MWSWordTest
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'MiddleWordScrambler-Tests'!
!MWSWordTest commentStamp: '<historical>' prior: 0!
A MWSWordTest is a test class for testing the behavior of MWSWord!


!MWSWordTest methodsFor: 'tests-as yet unclassified' stamp: 'FranciscoAryMartins 8/8/2015 12:07'!
testScramble
	self assert: (MWSWord newFrom: 'a') scrambled asString = 'a'.
	self assert: (MWSWord newFrom: 'ab') scrambled asString = 'ab'.
	self assert: (MWSWord newFrom: 'abc') scrambled asString = 'abc'.
	self assert: (MWSWord newFrom: 'abcd') scrambled asString sorted = 'abcd'.
	self assert: (MWSWord newFrom: 'abcdefgh') scrambled asString sorted = 'abcdefgh'.
	self assert: (MWSWord newFrom: 'a!!') scrambled asString = 'a!!'.
	self assert: (MWSWord newFrom: 'ab!!') scrambled asString = 'ab!!'.
	self assert: (MWSWord newFrom: 'abc!!') scrambled asString = 'abc!!'.
	self assert: (MWSWord newFrom: 'abcd!!') scrambled sorted asString = 'abcd!!'.
	self assert: (MWSWord newFrom: 'abcdefgh!!') scrambled sorted asString = 'abcdefgh!!'  ! !


TestCase subclass: #MWSMiddleWordsScramblerTest
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'MiddleWordScrambler-Tests'!
!MWSMiddleWordsScramblerTest commentStamp: '<historical>' prior: 0!
A MWSMiddleWordsScramblerTest is a test class for testing the behavior of MWSMiddleWordsScrambler!


!MWSMiddleWordsScramblerTest methodsFor: 'tests-as yet unclassified' stamp: 'FranciscoAryMartins 8/8/2015 14:43'!
testScramble
	|text|
	text := 'Eu também te amo!!'.
	self assert: (MWSMiddleWordScrambler new scramble: text) sorted = text sorted! !
PackageManifest subclass: #ManifestMiddleWordScrambler
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'MiddleWordScrambler'!
!ManifestMiddleWordScrambler commentStamp: '<historical>' prior: 0!
I store metadata for this package. These meta data are used by other tools such as the SmalllintManifestChecker and the critics Browser!


!ManifestMiddleWordScrambler methodsFor: 'code-critics' stamp: 'FranciscoAryMartins 8/8/2015 14:55'!
ruleLiteralArrayCharactersRuleV1FalsePositive
^ #(#(#(#RGMethodDefinition #(#MWSWord #hasPunctuation: #false)) #'2015-08-08T14:55:26.194495-03:00') )! !
Anúncios

Deixe um comentário

Preencha os seus dados abaixo ou clique em um ícone para log in:

Logotipo do WordPress.com

Você está comentando utilizando sua conta WordPress.com. Sair / Alterar )

Imagem do Twitter

Você está comentando utilizando sua conta Twitter. Sair / Alterar )

Foto do Facebook

Você está comentando utilizando sua conta Facebook. Sair / Alterar )

Foto do Google+

Você está comentando utilizando sua conta Google+. Sair / Alterar )

Conectando a %s