Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,6 +1,8 @@
[[wp:Mad Libs|Mad Libs]] is a phrasal template word game where one player prompts another for a list of words to substitute for blanks in a story, usually with funny results.
Write a program to create a Mad Libs like story. The program should read a multiline story from the input. The story will be terminated with a blank line. Then, find each replacement to be made within the story, ask the user for a word to replace it with, and make all the replacements. Stop when there are none left and print the final story.
Write a program to create a Mad Libs like story.
The program should read a multiline story from the input. The story will be terminated with a blank line.
Then, find each replacement to be made within the story, ask the user for a word to replace it with, and make all the replacements. Stop when there are none left and print the final story.
The input should be in the form:
<pre>

View file

@ -0,0 +1,128 @@
# Mad Libs style story generation #
# gets the story template from the file f. The template terminates with #
# a blank line #
# The user is then promoted for the replacements for the <word> markers #
# and the story is printed with the substitutions made #
PROC story = ( REF FILE f )VOID:
BEGIN
# a linked list of strings, used to hold the story template #
MODE STRINGLIST = STRUCT( STRING text, REF STRINGLIST next );
# a linked list of pairs of strings, used to hold the replacements #
MODE REPLACEMENTLIST = STRUCT( STRING word
, STRING replacement
, REF REPLACEMENTLIST next
);
# NIL reference for marking the end of a STRINGLIST #
REF STRINGLIST nil stringlist = NIL;
# NIL reference for marking the end of a REPLACEMENTLIST #
REF REPLACEMENTLIST nil replacementlist = NIL;
# returns "text" with trailing spaces removed #
OP RTRIM = ( STRING text )STRING:
BEGIN
INT trim pos := UPB text;
FOR text pos FROM UPB text BY -1 TO LWB text WHILE text[ text pos ] = " "
DO
trim pos := text pos - 1
OD;
text[ LWB text : trim pos ]
END; # RTRIM #
# looks for word in the dictionary. If it is found, replacement is #
# set to its replacement and TRUE is returned. If word not present, #
# FALSE is returned - uses recursion #
PROC find replacement = ( STRING word
, REF STRING replacement
, REF REPLACEMENTLIST dictionary
)BOOL:
IF dictionary IS nil replacementlist
THEN
FALSE
ELIF word OF dictionary = word
THEN
replacement := replacement OF dictionary;
TRUE
ELSE
find replacement( word, replacement, next OF dictionary )
FI; # find replacement #
# read the story template #
# the result has a dummy element so "next OF next" is always valid #
REF STRINGLIST story := HEAP STRINGLIST := ( "dummy", nil stringlist );
REF REF STRINGLIST next := story;
# read the story template, terminates with a blank line #
WHILE
STRING text;
get( f, ( text, newline ) );
text := RTRIM text;
text /= ""
DO
# add the line to the end of the list #
next := ( next OF next ) := HEAP STRINGLIST := ( text, nil stringlist )
OD;
# find the <word> markers in the story and replace them with the #
# user's chosen text - we ignore the dummy element at the start #
REF REPLACEMENTLIST dictionary := nil replacementlist;
REF STRINGLIST line := story;
WHILE line := next OF line;
line ISNT nil stringlist
DO
# have a line of text - replace all the <word> markers in it #
STRING word, replacement;
INT start pos, end pos;
WHILE char in string( "<", start pos, text OF line )
AND char in string( ">", end pos, text OF line )
DO
# have a marker, get it from the line #
word := ( text OF line )[ start pos : end pos ];
# get its replacement #
IF NOT find replacement( word, replacement, dictionary )
THEN
# we don't already have a replacement for word #
# get one from the user and add it to the dictionary #
print( ( "What should replace ", word, "? " ) );
read( ( replacement, newline ) );
dictionary := HEAP REPLACEMENTLIST := ( word, replacement, dictionary )
FI;
# replace <word> with the replacement #
text OF line := ( text OF line )[ : start pos - 1 ]
+ replacement
+ ( text OF line )[ end pos + 1 : ]
OD
OD;
# print the story, ignoring the dummy element at the start #
line := story;
WHILE line := next OF line;
line ISNT nil stringlist
DO
print( ( text OF line, newline ) )
OD
END; # story #
main:(
# we read the template from stand in (the keyboard unless it's been #
# redirected) we could prompt the user for a template file name, #
# open it and read that instead... #
print( ( "Please Enter the story template terminated by a blank line", newline ) );
story( stand in )
)

View file

@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace madLibs {
class Program {
static void Main(string[] args) {
string name, sex, addThis, thing;
bool isMale = false;
Console.Write("Enter a name: ");
name = Console.ReadLine();
while(isMale == false) {
Console.Write("Is that a male or female name? [m/f] ");
sex = Console.ReadLine().ToLower().ToCharArray()[0].ToString();
if(sex == "m") { isMale = true; } else if(sex == "f") { break; }
}
if (isMale){ addThis = "He "; }else{ addThis = "She "; }
Console.Write("Enter a thing: ");
thing = Console.ReadLine();
Console.WriteLine(Environment.NewLine + String.Format(("{0} went for a walk in the park. " + addThis +
"found a {1}. {0} decided to take it home."), name, thing));
Console.ReadKey();
}
}
}

View file

@ -0,0 +1,49 @@
import System.IO
import System.Environment
import qualified Data.Map as M
getLines :: IO [String]
getLines = getLines' [] >>= return . reverse
where
getLines' xs = do
line <- getLine
case line of
[] -> return xs
_ -> getLines' $ line:xs
prompt :: String -> IO String
prompt p = putStr p >> hFlush stdout >> getLine
getKeyword :: String -> Maybe String
getKeyword ('<':xs) = getKeyword' xs []
where
getKeyword' [] _ = Nothing
getKeyword' (x:'>':_) acc = Just $ '<' : (reverse $ '>':x:acc)
getKeyword' (x:xs) acc = getKeyword' xs $ x:acc
getKeyword _ = Nothing
parseText :: String -> M.Map String String -> IO String
parseText [] _ = return []
parseText line@(l:lx) keywords = do
case getKeyword line of
Nothing -> parseText lx keywords >>= return . (l:)
Just keyword -> do
let rest = drop (length keyword) line
case M.lookup keyword keywords of
Nothing -> do
newword <- prompt $ "Enter a word for " ++ keyword ++ ": "
rest' <- parseText rest $ M.insert keyword newword keywords
return $ newword ++ rest'
Just knownword -> do
rest' <- parseText rest keywords
return $ knownword ++ rest'
main :: IO ()
main = do
args <- getArgs
nlines <- case args of
[] -> getLines >>= return . unlines
arg:_ -> readFile arg
nlines' <- parseText nlines M.empty
putStrLn ""
putStrLn nlines'

View file

@ -0,0 +1,14 @@
filename = InputString["Enter the filename of the story template: "];
text = Import[filename];
listofblanks = StringCases[text, RegularExpression["<[^>]+>"]] // Union;
listofanswers = {};
Do[
answer = InputString["Enter a/an: " <> listofblanks[[i]]];
AppendTo[listofanswers, answer];
, {i, 1, Length[listofblanks]}
]
Do[
text = StringReplace[text, listofblanks[[i]] -> listofanswers[[i]]]
, {i, 1, Length[listofblanks]}
]
text

View file

@ -0,0 +1,7 @@
t: {<name> went for a walk in the park. <he or she> found a <noun>. <name> decided to take it home.}
view layout [a: area wrap t btn "Done" [x: a/text unview]]
w: copy []
parse x [any [to "<" copy brackets thru ">" (append w brackets)] to end]
w: unique w
foreach i w [replace/all x i ask join i ": "]
alert x

View file

@ -1,23 +1,23 @@
/*REXX program to prompt user for template substitutions within a story.*/
@.=; !.=0; #=0; @= /*assign some defaults. */
/*REXX program prompts user for a template substitutions within a story.*/
@.=; !.=0; #=0; @= /*assign some defaults. */
parse arg iFID . /*allow use to specify input file*/
if iFID=='' then iFID="MAD_LIBS.TXT" /*Not specified? Then use default*/
if iFID=='' then iFID="MAD_LIBS.TXT" /*Not specified? Use a default.*/
do recs=1 while lines(iFID)\==0 /*read the input file 'til done. */
@.recs=linein(iFID); @=@ @.recs /*read a record, append it to @ */
do recs=1 while lines(iFID)\==0 /*read the input file 'til done. */
@.recs=linein(iFID); @=@ @.recs /*read a record, append it to @ */
if @.recs='' then leave /*Read a blank line? We're done.*/
end /*recs*/
recs=recs-1 /*adjust for E─O─F or blank line.*/
do forever /*look for templates in the text.*/
do forever /*look for templates in the text.*/
parse var @ '<' ? '>' @ /*scan for <ααα> stuff in text.*/
if ?='' then leave /*if no ααα, then we're done. */
if !.? then iterate /*already asked? Keep scanning.*/
!.?=1 /*mark this ααα as "found". */
do forever /*prompt user for a replacement. */
do forever /*prompt user for a replacement. */
say ' please enter a word or phrase to replace: ' ?
parse pull ans; if ans\='' then leave
parse pull ans; if ans\='' then leave
end /*forever*/
#=#+1 /*bump the template counter. */
old.# = '<'?">"; new.# = ans /*assign "old" name & "new" name.*/

View file

@ -1,13 +1,13 @@
puts "Enter a story, terminated by an empty line:"
story = ""
until (line = STDIN.gets).chomp.empty?
until (line = gets).chomp.empty?
story << line
end
story.scan(/(?<=[<]).+?(?=[>])/).uniq.each do |var|
print "Enter a value for '#{var}': "
story.gsub!(/<#{var}>/, STDIN.gets.chomp)
story.gsub!(/<#{var}>/, gets.chomp)
end
puts ""
puts
puts story

View file

@ -0,0 +1,14 @@
object MadLibs extends App{
val input = "<name> went for a walk in the park. <he or she>\nfound a <noun>. <name> decided to take it home."
println(input)
println
val todo = "(<[^>]+>)".r
val replacements = todo.findAllIn(input).toSet.map{found: String =>
found -> readLine(s"Enter a $found ")
}.toMap
val output = todo.replaceAllIn(input, found => replacements(found.matched))
println
println(output)
}