Neighbors

Neighbor calculation functions for lexlib.

lexlib.neighbors.check_neighbors(a, b, sep=None)[source]

Determine whether two words are neighbors. Returns True if they are neighbors and False if they are not.

sep – String used to separate phonemes (if the words are phonological forms). To separate into individual characters, set to None (default).

lexlib.neighbors.get_neighbor_dict(words, **kwargs)[source]

Compare each word in a list of words to each word in a corpus word list (or in the same list if corpus is not given), and return a dict where each target word is a key, and its value is a list of its neighbors. (If you are looking for a function to get neighbor pairs, see get_neighbor_pairs()).

keyword arguments:

corpus – List of all the words to get the neighbors from. If empty, defaults to words.

sep – String used to separate phonemes (if the words are phonological forms). To separate into individual characters, set to None (default).

debug – If True, it prints the current word and the words being compared to it to the console. Defaults to False.

lexlib.neighbors.get_neighbor_pairs(words, **kwargs)[source]

Compare each word in a list of words to each word in a corpus word list (or in the same list if corpus is not given), and return a list of (word, neighbor) pairs. (If you are looking for a function to get lists of all the neighbors for specific words, see get_neighbor_pairs()).

keyword arguments:

corpus – List of all the words to get the neighbors from. If omitted, defaults to words.

sep – String used to separate phonemes (if the words are phonological forms). To separate into individual characters, set to None (default).

debug – If True, it logs the current word and the words being compared to it to the console. Defaults to False.

lexlib.neighbors.get_neighbor_positions(neighbor_pairs, sep=None)[source]

Given a list of (word1, word2) neighbor_pairs, return a list of (word1, word2, position) triples, where position is the position in the words where the neighbor relationship is formed. Note that this can only be calculated for pairs of substitution neighbors. If the words differ in length, position will be -1.

Example:

>>> neighbor_pairs = [("cat", "cap"), ("cat", "cut"), ("cat", "cast")]
>>> get_neighbor_positions(neighbor_pairs)
[("cat", "cap", 3), ("cat", "cut", 2), ("cat", "cast", -1)]
lexlib.neighbors.get_neighbor_types(neighbor_dict, sep=None)[source]

Given a neighbor_dict (where a key is a “target” word and its value is a list of all of its neighbors), return a list of (word1, word2, relationship) triples, where relationship is one of “deletion,” “addition,” “substitution,” or “unknown”.