NGordNet
Explore how words were used over time and what they mean: time series and n-gram maps built on dictionaries, WordNet as a directed graph of meanings, hyponym queries with traversals, popularity rankings, and a SwiftUI app with Swift Charts.
This is CS61B’s Project 4, in Swift. You will build the back end of a tool that answers two kinds of question about words: how often was a word used, year by year? and which words are more specific kinds of a word? The first uses maps of time series; the second uses a graph of meanings and the traversals from Unit 6. A SwiftUI app (Lesson 42) charts and queries the results.
Getting started
Work in the ngordnet folder of the
swift-projects repository:
cd swift-projects/ngordnet
swift test
Sources/NGordNetCore/Data/holds the sample data:words.tsvandtotals.csv(counts) andsynsets.csvandhyponyms.csv(meanings).SampleData.swift, provided, loads them.TimeSeries.swift,NGramMap.swift,WordNet.swift, andNGordNet.swiftare yours.NGordNetApp, provided, has a History tab that charts words and a Hyponyms tab that queries WordNet. It needs macOS 27:swift run -c release NGordNetApp.
The tests use small data written inline, so each task can be checked on examples you can work out by hand.
Part A: word histories
Task 1: TimeSeries
A TimeSeries maps years to values; it wraps a dictionary, data. Write:
init(copying:from:through:), a copy with only the years in a range (empty if the range is backwards).years, in increasing order, andvalues, in the same order.plus(_:), the year-by-year sum, where a year missing from one series counts as 0 there.dividedBy(_:), the year-by-year quotient for the years of this series. Every such year must be in the divisor; check with a precondition.
CS61B builds its time series on Java’s TreeMap, a red-black tree, which keeps
years sorted. A Swift Dictionary does not, so years sorts on demand: fine for a
few hundred years, and Lesson 40’s tree would be the choice for millions of keys.
Task 2: NGramMap
NGramMap reads two files, given as strings. The word counts have one line per
word and year, word⇥year⇥count⇥books, separated by tabs; the totals have one line
per year, year,count,pages,books. Store a TimeSeries per word and one for the
totals, then write:
countHistory(_:from:through:), a word’s counts, as a copy, so a caller who changes it does not change the map. (With value types, a copy is what you get anyway; Lesson 33 explains why it costs nothing until someone writes.)totalCountHistory(from:through:).weightHistory(_:from:through:): the count divided by the total count, the word’s relative frequency, which makes years with more books comparable with years with fewer.summedWeightHistory(_:from:through:), the sum of several words’ weights.
After Part A, the app’s History tab works: try bird, dog, computer.
Part B: meanings
WordNet groups words into synsets, sets of synonyms that share one
meaning, and links each synset to its hyponyms, the more specific meanings: a
sparrow is a kind of bird, a bird a kind of animal. The synsets file has lines
id,words,definition, with the words separated by spaces; definitions may contain
commas. The hyponyms file has lines id,hyponym,hyponym,…, and a synset can appear
on several lines.
A word can belong to several synsets, and that is the interesting part. In
the sample data, swift is both a bird and a programming language, and jump is
both a sudden change and a leap into the air.
Task 3: Hyponyms of a word
Parse both files into a directed graph of synset ids (Lesson 44), plus maps
from ids to words and from words to ids. Then write hyponyms(of:): every word in
every synset containing the word, and in every synset reachable from those along
hyponym edges, including the word itself, sorted and without duplicates. Any
traversal from Lesson 43 works; mark synsets as you visit them, since the graph is a
DAG in which a synset can be reached along several paths.
Task 4: Several words
Write hyponyms(of words:): the words that are hyponyms of every word in the
list. In the sample data, the hyponyms common to bird and language are just
swift.
Part C: popularity
Task 5: The k most used
Write NGordNet.hyponyms(_:from:through:k:). With k = 0, it returns all common
hyponyms, as in Task 4. With k > 0, it keeps only the k most used between the two
years, counting each word’s total uses over those years, leaving out words
with no uses at all, and breaking ties alphabetically. The result is always in
alphabetical order.
Try device from 1900 to 1949 in the app: the computer words do not appear,
because in the sample data they are unused before 1950.
Task 6: Your own tests
Write tests on the bundled sample data in MyTests.swift, using
SampleData.ngramMap() and SampleData.wordNet(). Test properties rather than
exact numbers: every hyponym of animal is a hyponym of entity; a word’s summed
weight on its own equals its weight history; asking for k larger than the number
of hyponyms returns every used one.