Skip to content
quadrismegistus edited this page Apr 22, 2011 · 18 revisions

Front-end

How to initiate prosodic

cd prosodic
python prosodic.py

Back-end

How to initiate prosodic

cd prosodic
python
import prosodic as p

How to open a text

import prosodic as p
t = p.Text('/absolute/path/here.txt')    # open path from root
t = p.Text('relative/path/here.txt')     # open path relative to directory from which Prosodic initiated
t = p.Text('direct text/with lines//and even/new stanzas')     # create texts on the fly

How to change the language

import prosodic as p
p.lang='fi'                                      # method 1: change the lang attribute of the prosodic module
t = p.Text('corpora/a_finnish.txt')    
t = p.Text('corpora/an_english.txt',lang='en')   # method 2: set the lang='xx' flag on a Text
w = p.Word('transparency',lang='en')             #           ... or word

How to change other configuration options interactively

import prosodic as p
print p.config                  # shows the current state of the 'config' dictionary
p.config['print_to_screen']=0   # turn off printing

How to get help on any object x

x.less()                # shows just attributes
x.more()                # shows attributes and un-inherited methods
x.dir()                 # shows attributes and all methods callable from this object
help(x)                 # brings you to Python's interactive help screen showing x's documentation

How to access the children of any object x

method 1: by returning a list of a specific entity

x.texts()                         # returns a list of Texts
x.stanzas()                       # returns a list of Stanzas
x.lines()                         # returns a list of Lines
x.words()                         # returns a list of Words
x.syllables()                     # returns a list of Syllables
x.syllableBodies()                # returns a list of SyllableBodies
x.onsets()                        # returns a list of Onsets
x.rimes()                         # returns a list of Rimes
x.nuclei()                        # returns a list of Nuclei
x.codae()                         # returns a list of Codae
x.phonemes()                      # returns a list of Phonemes
x.ents('Word')                    # returns a list of Words
x.ents('Word',flattenList=False)  # returns a list of (list of [differently stressed, weighted, etc] Words)s
x.ents('Word',False)              # returns a list of (list of [differently stressed, weighted, etc] Words)s

method 2: using x.children and x.descendants()

import prosodic as p
t = p.Text('Q: if i am a text/to what/do I give birth?')
t.children       # A: stanzas
t.descendants()  # A: stanzas
                 #   the only difference is .children could be a list of lists [if the .children of a Line]
                 #   -- eg, a line's .children could be: [ ['in.to, in.'to], [the], [woods] ]
                 #   BUT .descendants() always make sure the list is 'flat' (ie, not made of lists)

[Python] How to iterate over lists

# directly
for w in t.words():
  if len(w.syllables())>1:
    print w

# indirectly
new_list = [w for w in t.words() if len(w.syllables())>1]
print new_list

How to access the features of any object x

w=p.Word('vocalize')                 # (create word)
s1=w.syllables()[0]                  # (get first syllable)
s1.feature()                         # return dictionary of all features
s1.feature('prom.stress')            # return value of this feature if found

w.feature('voice')                   # did not find feature here; returns nothing
w.feature('voice',searchforit=True)  # returns: [(v, True), (k, False), (l, True), (z, True)]
w.feature('voice',True)              # same as above
w.feature('+voice',True)             # returns: [v, l, z]
w.feature('-voice',True)             # returns: [k]

How to write function-based queries

def is_allCV(word):   # returns True if each of word's syllables has shape 'CV'
  sylls=word.syllables()
  shapes=[syll.getShape() for syll in sylls]
  for shape in shapes:
    if shape!="CV":
      return False
  return True           # this point reached only if not yet returned False b/c (shape!=CV)

t=p.Text('mieleni minun tekevi')
cv_words = [ w for w in t.words() if is_allCV(w) ]
print cv_words          # returns: [<Word.tekevi> ['te.ke.vi]]