COS 226 Programming Assignment 6

Language modeling

Design and implement functions that build a language model from a piece of input text, then use the model to generate random output text.

As a warmup, implement a main (driver) program that takes an integer k from the command line (which we shall refer to as the order of the model), then reads a text string from standard input and calls a symbol-table function for each consecutive (k+1)-character substring in the text. For example, when k is 2 and the input string is abracadabra, your driver program should call the symbol table function for the keys abr bra rac aca cad ada dab abr bra. Next, implement a symbol-table implementation that converts keys to array indices and uses them to keep a count of the number of occurrences of each key in the text. Include a routine output that you call as the last statement of your driver that prints out the number of distinct keys and the key that appears most often in the text. You will not use these functions directly in your solution, but they will give you the basic program organization that you need. The performance-critical part of the warmup has to do with space. Your driver program should not store the whole text string. Indeed, you only need to keep track of the most recent k characters in the string. If you wish, you may assume that k is sufficiently small that k characters fit in a long, and use shifting and masking to keep track of the keys. Your brute-force symbol-table implementation will not work for large k, anyway: for example, three 7-bit characters imply a 21-bit index, or 2 million entries.

Your next task is to build a language model, using the same driver program, but developing a symbol-table implementation that uses space proportional to the number of distinct keys. Furthermore, your data structure must be able to, given a k-character key, efficiently access the frequency of occurrence of each of the (k+1)-character keys with that prefix. This second operation is at the crux of the matter, as you will need it to generate random strings in accordance with the model.

Finally, change output to produce a random output string, as follows. Start with the first k characters in the text. Then generate a new random character, but according to the probabilities provided by the model. For example, if the text has 100 occurrences of th, with 50 occurrences of the, 25 occurrences of thi, 20 occurrences of tha, and 5 occurrences of tho, you should produce an e with probability 1/2, an i with probability 1/4, an a with probability 1/5, and an o with probability 1/20. After you generate a character, then you move over one character position, always using the last k characters generated to determine the probabilities for the next. For example, if your program had chosen i in the example above, then it would next have to look up the number of occurrences of hi and generate the next character according to probabilities determined by that sequence. Continue the process until you have output the same number of characters as appeared in the text.

After you have done the warmup, be sure to carefully design your data structure and associated operations before plunging into the implementation. As usual, there are a few details left unspecified here. There are several different effective approaches that you could use. Whichever you choose, you need to take particular care to be sure that all the pieces work properly as you develop and debug your solution to this problem. You probably will want to have a utility routine to dump the contents of your data structure in an easy-to-read format, and to start with tiny input texts that you understand to be sure that your program is building the data structure that you think it is building.

Once you get the program working, you will find that the random text with low-order models starts to sound more and more like the original text as you increase the order, as illustrated in following example:

Example 1 input: news item

Microsoft said Tuesday the company would comply with a preliminary ruling by Federal District Court Judge Ronald H. Whyte that Microsoft is no longer able to use the Java Compatibility Logo on its packaging and websites for Internet Explorer and Software Developers Kit for Java.

"We remain confident that once all the facts are presented in the larger case, the court will find Microsoft to be in full compliance with its contract with Sun," stated Tom Burt, Associate General Counsel for Microsoft Corporation. "We are disappointed with this decision, but we will immediately comply with the Court's order."

Microsoft has been in the forefront of helping developers use the Java programming language to write cutting-edge applications. The company has committed significant resources so that Java developers have the option of taking advantage of Windows features when writing software using the Java language. Providing the best tools and programming options will continue to be Microsoft's goal.

"We will continue to listen to our customers and provide them the tools they need to write great software using the Java language," added Tod Nielsen, General Manager for Microsoft's Developer Relations Group/Platform Marketing.

Example 1 output: random news item, using input as an order 7 model

Microsoft said Tuesday the court will find Microsoft's goal.

"We will continue to listen to our customers and programming option of taking advantage of Windows features when writing software using the J ava Compatibility Logo on its packaging and websites for Internet Explorer and Software using the best tools a nd programming language. Providing the Java language. Providing the Java programming language to write great software Developers Kit for Java.

"We remain confident that once all the facts are presented in the forefront of h elping developers have the option of taking advantage of Windows features when writing software Developers use the Java Compatibility Logo on its packaging and websites for Internet Explorer and Software using the best tools a nd provide them the tools they need to write cutting-edge applications. The company would comply with this decision, but we will immediately comply with this decision, but we will immediately comply with a preliminary ruling by Federal District Court Judge Ronald H. Whyte that Microsoft is no longer able to use the Java language," added Tod Nielsen, General Manager for Microsoft's goal.

Some longer examples are attached at the end of this document, and there are many more apparent pearls of wisdom in the full texts on the web. As you can see, there are limitless opportunities for amusement here. Try your model on some of your own text, or find something interesting on the net. You may wish to write small filters to clean up white space for pure text, but be careful, as the special characters sometimes drive the model, as illustrated in the Shakespeare example.

Submit your code as usual, along with one or two of the most amusing language-modeling examples that you come up with. Be sure to describe your data structure and algorithms in detail, and describe the most important performance characteristics of your program in the README file. Part of your grade will depend on your understanding of the resources consumed by your program for huge problems.



Due: 11:59PM Sunday, April 4.

Example 2 input: Speech to class of 2001, excerpts [link to full text]

Welcome to Princeton. This may be your first Princeton lecture, but it's not a typical one. For one thing it's the only time you'll be in a class of size more than 1000! Also, lectures usually involve slides or vugraphs, or at least a blackboard. When Hal told me this lecture would be in this room and that no audio-visual aids would be possible, I realized the challenge: we've all been on vacation all summer, and now we have to deal in ideas, face-to-face. No slides. No movies. No organist. Not even any Internet access. Well, at least the experience ties in with the topic of this lecture, as you'll see.

Many of you have probably not done much academic work since you opened that thick envelope from Fred Hargadon. Right? The purpose of this lecture is to set your minds in motion, because you'll need them in gear at full speed when classes start on Thursday.

The topic that I've chosen for this purpose is the prospect of having all knowledge online, and its implications. To start, I need to test some basic assumptions that I've made in preparing this talk: how many of you have never used a computer? how many of you use electronic mail? how many of you have ever used the Internet? how many use it regularly? how many run companies that produce Web pages? OK. Well, it looks as though I don't have to describe the basic features of the net to most of you. I'm not going to assume much, anyway.

You can find a link to a web page for this lecture on my home page. If you've never been on the net, take this opportunity to get a friend to show it to you. Also, after you've had a chance to discuss this talk in your residential colleges tonight, if you'd like to send me e-mail with your reaction to it, please feel free to do so. I'll collect the mail that I get and put it on the web page.

SUMMARY OF BUSH ARTICLE

I'd like to begin with a brief summary of the article "As We May Think", which was written by Vannevar Bush in 1945. The article was written at the end of World War II. Science played a significant role in the outcome of the war, and Bush wonders where scientists will turn their attention next.

Example 2 output: random speech, using input as an order 7 model, excerpts [link to full text]

Welcome to life before you were born.

At a universities, where the Joneses were invented, actually think about effectively few people expected to revolution, that it might be like?

Before therefore replace some teachers instantly being invented, actually by John von Neumann, right help are on the lecture would need to think that of technical device called "associative instruction of interconnections in the audience somewhere! I was at Xerox PARC. I visited there were to postulate that the university, we try to absorb new ideas to others all around in millions of dollars on things were 10 years, a small amount of information, and he mentioned that both of these thing that a physical libraries and museums at Harvard, so I'm not much about 5 years, it's fair to save every keystroke typed in that comprise though the web, the information, after you've never been on that limb.

SUMMARY OF BUSH ARTICLE

No argument with it? I'd be far out on a limb if I said that enhance our understand the people expect to have a clear how much about IBM in the functionality of today, but the article "As We May Think", which are available at the enterprise upon which you agree with Noam. Again, let me begins by noting them to help solve scientists in the amount of information, the number of problems.

Still, Bush did hit the nail on the verge of breaking down. Why? First, he says, there are now found in T-shirts and sandals, drinking personal attention next.

I'd like to say that you missed the connection be linked together in different world different than access it by typing in a short time with exponential colleges tonight.



Example 3 input: As you Like It, excerpts [link to full text]

	[Enter DUKE SENIOR, AMIENS, and two or three Lords,
	like foresters]

DUKE SENIOR	Now, my co-mates and brothers in exile,
	Hath not old custom made this life more sweet
	Than that of painted pomp? Are not these woods
	More free from peril than the envious court?
	Here feel we but the penalty of Adam,
	The seasons' difference, as the icy fang
	And churlish chiding of the winter's wind,
	Which, when it bites and blows upon my body,
	Even till I shrink with cold, I smile and say
	'This is no flattery: these are counsellors
	That feelingly persuade me what I am.'
	Sweet are the uses of adversity,
	Which, like the toad, ugly and venomous,
	Wears yet a precious jewel in his head;
	And this our life exempt from public haunt
	Finds tongues in trees, books in the running brooks,
	Sermons in stones and good in every thing.
	I would not change it.

AMIENS	Happy is your grace,
	That can translate the stubbornness of fortune
	Into so quiet and so sweet a style.

DUKE SENIOR	Come, shall we go and kill us venison?
	And yet it irks me the poor dappled fools,
	Being native burghers of this desert city,
	Should in their own confines with forked heads
	Have their round haunches gored.

Example 3 output: random Shakespeare, using order 6 model, excerpts [link to full text]

DUKE SENIOR	Now, my co-mates and thus bolden'd, man, how now, monsieur Jaques,
	Unclaim'd of his absence, as the holly!
	Though in the slightest for the fashion of his absence, as the only wear.

TOUCHSTONE	I care not for meed!
	This I must woo yours: your request than your father: the time,
	That ever love I broke
	my sword upon some kind of men
	Then, heigh-ho! sing, heigh-ho! sing, heigh-ho! sing, heigh-ho! unto the needless stream;
	'Poor deer,' quoth he,
	'Call me not so keen,
	Because thou the creeping hours of the sun,
	As man's feasts and women merely players:
	Thus we may rest ourselves and neglect the cottage, pasture?

	[Exit]

	[Enter DUKE FREDERICK	Can in his time in my heartily,
	And have me go with your fortune
	In all this fruit
	Till than bear
	the arm's end: I will through
	Cleanse the uses of the way to look you.
	Know you not, master,
	Sighing like upon a stone another down his bravery is not so with his effigies with my food:
	To speak my mind, and inquisition
	And unregarded age in corners throat,
	He will come hither:
	He dies that hath engender'd:
	And you to
	the bed untreasured of the brutish sting it.