-
Notifications
You must be signed in to change notification settings - Fork 43
Recipes
Ryan Heuser edited this page Jul 17, 2015
·
9 revisions
The following is a repository of "recipes": snippets of code which can be swapped, potentially modified, and used in other contexts.
Prosodic has a built-in layer of lexical ambiguity with regard to the stress of (primarily) monosyllabic words in English. A monosyllabic word can be either (a) definitely unstressed, (b) maybe unstressed, or (c) definitely stressed. These ambiguities are currently resolved through the process of metrical parsing.
One can access this ambiguity in Prosodic in the following way:
# import prosodic (in same directory as prosodic-master folder)
import prosodic as p
# create a text object
text = p.Text("a cart he drove had broken down")
# access the words as a *flattened* list [ambiguities are not represented]
words = text.words()
for word in words:
# each word is a word object in Prosodic
print word
""" [this will print:]
a <'eɪ>
cart <'kɑrt>
he <hiː>
drove <'droʊv>
had <hæd>
broken <'broʊ.kən>
down <'daʊn>
"""
# access the text's words, but *preserving the ambiguities*:
words = text.words(flattenList=False)
for word in words:
# each word is now a mini-list of word objects, rather than a word object
print word
""" [this will print:]
[<Word.a> ['eɪ], <Word.a> [ə]]
[<Word.cart> ['kɑrt]]
[<Word.he> [hiː]]
[<Word.drove> ['droʊv]]
[<Word.had> [hæd]]
[<Word.broken> ['broʊ.kən]]
[<Word.down> ['daʊn], <Word.down> [daʊn]]
"""
# To access the stress of a word object, use its attribute ".stress"
word = words[0][0] # the first mini-list of words, the first in the mini-list, is a word object
print word.stress # returns: 'P'
# To generate all possibilities of the word ambiguities, use a function 'product' available on the module 'prosodic'
for unique_word_combo in p.product(*words):
print unique_word_combo
print [word.stress for word in unique_word_combo]
print
"""[this will print 4 combinations, given two ambiguities ("a", or "down"):]
(<Word.a> ['eɪ], <Word.cart> ['kɑrt], <Word.he> [hiː], <Word.drove> ['droʊv], <Word.had> [hæd], <Word.broken> ['broʊ.kən], <Word.down> ['daʊn])
['P', 'P', 'U', 'P', 'U', 'PU', 'P']
(<Word.a> ['eɪ], <Word.cart> ['kɑrt], <Word.he> [hiː], <Word.drove> ['droʊv], <Word.had> [hæd], <Word.broken> ['broʊ.kən], <Word.down> [daʊn])
['P', 'P', 'U', 'P', 'U', 'PU', 'U']
(<Word.a> [ə], <Word.cart> ['kɑrt], <Word.he> [hiː], <Word.drove> ['droʊv], <Word.had> [hæd], <Word.broken> ['broʊ.kən], <Word.down> ['daʊn])
['U', 'P', 'U', 'P', 'U', 'PU', 'P']
(<Word.a> [ə], <Word.cart> ['kɑrt], <Word.he> [hiː], <Word.drove> ['droʊv], <Word.had> [hæd], <Word.broken> ['broʊ.kən], <Word.down> [daʊn])
['U', 'P', 'U', 'P', 'U', 'PU', 'U']
"""
def is_ntV(word):
phonemes = word.phonemes()
if phonemes[-3:-1] != [p.Phoneme("n"), p.Phoneme("t")]:
return False
return phonemes[-1].feature("syll") # return True if vowel, False otherwise