Laynetworks  
Web laynetworks.com Google
Home | Site Map | Tell a friends
Management Tutorials
Download
Tutorials
History
Computer Science
Networking
OS - Linux and Unix
Source Code
Script & Languages
Protocols
Glossary
IGNOU
Quiz
About Us
Contact Us
Feedback
 
Sign up for our Email Newsletter
 
Get Paid for Your Tech Turorials / Tips

 
Home > Computer Science > TMA 2001
 
CS 01 CS 02 CS 03 CS 04 CS 05 CS 06 CS 07 CS 08 CS 09 CS 10 CS 11 CS 12 CS 13 CS 14 CS 15 CS 16 CS 17
Page : 1 2 3 4 5 6 7 8 9 10 11 12
TMA 2001
 
Following is the example of a program that accepts an arbitrarily long text as input and produces a list of words and their frequency of occurrence as output.

(The program is in ICON** V. 9.0 programming language)

** = Icon is a very high level general-purpose programming language with extensive features for processing strings (text) and data structures. Icon is an imperative, procedural language with a syntax that is reminiscent of C and Pascal, but with semantics at a much higher level.

 

global uses, lineno, width

 

procedure main(args)

width := 15 # width of word field

uses := table()

lineno := 0

every tabulate(words()) # tabulate all citations

output() # print the citations

end

 

# Add line number to citations for word

#

procedure tabulate(word)

/uses[word] := set()

insert(uses[word], lineno)

return

end

# Generate words

#

procedure words()

while line := read() do {

lineno +:= 1

write(right(lineno, 6), " ", line)

map(line) ? while tab(upto(&letters)) do {

s := tab(many(&letters))

if *s >= 3 then suspend s# skip short words

}

}

end

TOP

# Print the results

#

procedure output()

write() # blank line

uses := sort(uses, 3) # sort citations

while word := get(uses) do {

line :=""

numbers := sort(get(uses))

while line ||:= get(numbers) || ","

write(left(word, width), line[1:-2])

}

end

The program reads a line, writes it out with an identifying line number, and processes every word in the line. Words less than three characters long are considered to be "noise" and are discarded. A table, uses, is keyed by the words. Every key has a corresponding set of line numbers. The first time a word is encountered, a new set is created for it. The line number is inserted in any event. Since a value can be in a set only once, duplicate line numbers are suppressed automatically.

After all the input has been read, the table of words is sorted by key. Each corresponding set of line numbers is sorted and the line numbers are appended to the line to be written.

For example, if the input file is

On the Future!-how it tells

Of the rapture that impells

To the swinging and the ringing

Of the bells, bells, bells-

Of the bells, bells, bells, bells,

Bells, bells, bells-

To the rhyming and the chiming of the bells!

the output is

1 On the Future!-how it tells

2 Of the rapture that impells

3 To the swinging and the ringing

4 Of the bells, bells, bells-

5 Of the bells, bells, bells, bells,

6 Bells, bells, bells-

7 To the rhyming and the chiming of the bells!

 

and 3, 7

bells 4, 5, 6, 7

chiming 7

future 1

how 1

impells 2

rapture 2

rhyming 7

ringing 3

swinging 3

tells 1

that 2

the 1, 2, 3, 4, 5, 7

TOP
 
Page : 1 2 3 4 5 6 7 8 9 10 11 12
   
Donation | Useful links | Link to Laynetworks.com | Legal
Copyright © 2000-2010 Lay Networks All rights reserved.