0% found this document useful (0 votes)
4 views2 pages

Program 7

The document provides two Julia programs: the first calculates the frequency of each letter in a given line of text using a dictionary, while the second extracts unique words from a file, ignoring case and punctuation, using a set. Both programs include user input prompts and error handling for file operations. The first program outputs letter frequencies, and the second lists unique words along with their total count.

Uploaded by

dejongmax21
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Program 7

The document provides two Julia programs: the first calculates the frequency of each letter in a given line of text using a dictionary, while the second extracts unique words from a file, ignoring case and punctuation, using a set. Both programs include user input prompts and error handling for file operations. The first program outputs letter frequencies, and the second lists unique words along with their total count.

Uploaded by

dejongmax21
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

JULIA

7a. Given a line of text as input, develop a Julia program to determine the frequency with which
each letter of the alphabet is used (make use of dictionary)
# Read input line
println("Enter a line of text:")
text = readline()

# Initialize dictionary
freq = Dict{Char, Int}()

# Process each character


for ch in lowercase(text)
if isletter(ch)
if haskey(freq, ch)
freq[ch] += 1
else
freq[ch] = 1
end
end
end

# Display results
println("\nLetter Frequencies:")
for key in sort(collect(keys(freq)))
println("$key => $(freq[key])")
end

7b. Develop a Julia program to fetch words from a file with arbitrary punctuation and keep
track of all the different words found (make use of set and ignore the case of the letters: e.g. to
and To are treated as the same word).
# Function to extract unique words from a file
function unique_words(filename)
# Create an empty Set
words_set = Set{String}()

# Open and read file


open(filename, "r") do file
for line in eachline(file)

# Convert to lowercase (ignore case)


line = lowercase(line)

# Replace punctuation with space


line = replace(line, r"[^a-z]" => " ")

# Split into words


words = split(line)
# Add words to set
for word in words
push!(words_set, word)
end
end
end

return words_set
end

# Main program
println("Enter file name:")
filename = readline()
try
result = unique_words(filename)

println("\nUnique words found:")


for word in sort(collect(result))
println(word)
end

println("\nTotal unique words: ", length(result))

catch e
println("Error: File not found or cannot be opened!")
end

You might also like