Previous topic

music21.test.treeYield

Next topic

music21.theoryAnalysis.theoryAnalyzer

Table Of Contents

Table Of Contents

This Page

music21.text

Utility routines for processing text in scores and other musical objects.

Functions

music21.text.assembleLyrics(streamIn, lineNumber=1)

Concatenate text from a stream. The Stream is automatically flattened.

The lineNumber parameter determines which line of text is assembled.

>>> s = stream.Stream()
>>> n1 = note.Note()
>>> n1.lyric = "Hi"
>>> n2 = note.Note()
>>> n2.lyric = "there"
>>> s.append(n1)
>>> s.append(n2)
>>> text.assembleLyrics(s)
'Hi there'
music21.text.postpendArticle(src, language=None)

Given a text string, if an article is found in a leading position, place it at the end with a comma.

>>> text.postpendArticle('The Ale is Dear')
'Ale is Dear, The'
>>> text.postpendArticle('The Ale is Dear', 'en')
'Ale is Dear, The'
>>> text.postpendArticle('The Ale is Dear', 'it') 
'The Ale is Dear'
>>> text.postpendArticle('Il Combattimento di Tancredi e Clorinda', 'it') 
'Combattimento di Tancredi e Clorinda, Il'
music21.text.prependArticle(src, language=None)

Given a text string, if an article is found in a trailing position with a comma, place the article in front and remove the comma.

>>> text.prependArticle('Ale is Dear, The')
'The Ale is Dear'
>>> text.prependArticle('Ale is Dear, The', 'en')
'The Ale is Dear'
>>> text.prependArticle('Ale is Dear, The', 'it')
'Ale is Dear, The'
>>> text.prependArticle('Combattimento di Tancredi e Clorinda, Il', 'it') 
'Il Combattimento di Tancredi e Clorinda'

TextBox

class music21.text.TextBox(content=None, x=500, y=500)

A TextBox is arbitrary text that might be positioned anywhere on a page, independent of notes or staffs. A page attribute specifies what page this text is found on; positionVertical and positionHorizontal position the text from the bottom left corner in units of tenths.

This object is similar to the TextExpression object, but does not have as many position parameters, enclosure attributes, and the ability to convert to RepeatExpressions and TempoTexts.

>>> from music21 import text, stream
>>> y = 1000 # set a fixed vertical distance
>>> s = stream.Stream()
>>> # specify character, x position, y position
>>> tb = text.TextBox('m', 250, y)
>>> tb.size = 40
>>> tb.alignVertical = 'bottom'
>>> s.append(tb)
>>> tb = text.TextBox('u', 300, y)
>>> tb.size = 60
>>> tb.alignVertical = 'bottom'
>>> s.append(tb)
>>> tb = text.TextBox('s', 550, y)
>>> tb.size = 120
>>> tb.alignVertical = 'bottom'
>>> s.append(tb)        
>>> tb = text.TextBox('ic', 700, y)
>>> tb.alignVertical = 'bottom'
>>> tb.size = 20
>>> tb.style = 'italic'
>>> s.append(tb)
>>> tb = text.TextBox('21', 850, y)
>>> tb.alignVertical = 'bottom'
>>> tb.size = 80
>>> tb.weight = 'bold'
>>> tb.style = 'italic'
>>> s.append(tb)
>>> s.show()
../_images/textBoxes-01.png

TextBox bases

TextBox read-only properties

Read-only properties inherited from Music21Object:

TextBox read/write properties

TextBox.alignHorizontal

Get or set the horicontal align.

>>> te = text.TextBox('testing')
>>> te.alignHorizontal = 'right'
>>> te.alignHorizontal
'right'
TextBox.alignVertical

Get or set the vertical align. Valid values are top, middle, bottom, and baseline

>>> te = text.TextBox('testing')
>>> te.alignVertical = 'top'
>>> te.alignVertical
'top'
TextBox.content

Get or set the content.

>>> te = text.TextBox('testing')
>>> te.content
'testing'
>>> te.justify = 'center'
>>> te.justify
'center'
TextBox.page

Get or set the page number. The first page (page 1) is the default.

>>> te = text.TextBox('testing')
>>> te.content
'testing'
>>> te.page
1
TextBox.positionHorizontal

Get or set the vertical position.

>>> te = text.TextBox('testing')
>>> te.positionHorizontal = 200
>>> te.positionHorizontal
200
TextBox.positionVertical

Get or set the vertical position.

>>> te = text.TextBox('testing')
>>> te.positionVertical = 1000
>>> te.positionVertical
1000

Read/write properties inherited from Music21Object:

Read/write properties inherited from TextFormat:

TextBox methods

Methods inherited from Music21Object:

TextBox instance variables

Instance variables inherited from Music21Object:

TextFormat

class music21.text.TextFormat

An object for defining text formatting. This object can be multiple-inherited by objects that need storage and i/o of text settings.

See music21.expressions.TextExpression for an example.

TextFormat read/write properties

TextFormat.justify

Get or set the justification.

>>> tf = text.TextFormat()
>>> tf.justify = 'center'
>>> tf.justify
'center'
TextFormat.letterSpacing

Get or set the letter spacing.

>>> tf = text.TextFormat()
>>> tf.letterSpacing = 20
>>> tf.letterSpacing
20.0
>>> tf.letterSpacing = 'normal'
TextFormat.size

Get or set the size.

>>> tf = text.TextFormat()
>>> tf.size = 20
>>> tf.size
20.0
TextFormat.style

Get or set the style, as normal, italic, bold, and bolditalic.

>>> tf = text.TextFormat()
>>> tf.style = 'bold'
>>> tf.style
'bold'
TextFormat.weight

Get or set the weight, as normal, or bold.

>>> tf = text.TextFormat()
>>> tf.weight = 'bold'
>>> tf.weight
'bold'

LanguageDetector

class music21.text.LanguageDetector(text=None)

Attempts to detect language on the basis of trigrams

uses code from http://code.activestate.com/recipes/326576-language-detection-using-character-trigrams/ unknown author. No license given.

See Trigram docs below...

LanguageDetector methods

LanguageDetector.mostLikelyLanguage(excerpt)

returns the code of the most likely language for a passage, works on unicode or ascii. current languages: en, fr, de, it, cn

>>> ld = text.LanguageDetector()
>>> ld.mostLikelyLanguage("Hello there, how are you doing today? I haven't seen you in a while.")
'en'
>>> ld.mostLikelyLanguage("Ciao come stai? Sono molto lento oggi, ma non so perche.")
'it'
LanguageDetector.mostLikelyLanguageNumeric(excerpt=None)

returns a number representing the most likely language for a passage or 0 if there is no text.

Useful for feature extraction.

The codes are the index of the language name in LanguageDetector.languageCodes + 1

>>> ld = text.LanguageDetector()
>>> for i in range(0, len(ld.languageCodes)):
...    print(str(i+1) + " " +  ld.languageCodes[i])
1 en
2 fr
3 it
4 de
5 cn
>>> numLang = ld.mostLikelyLanguageNumeric("Hello there, how are you doing today? I haven't seen you in a while.")
>>> numLang
1
>>> ld.languageCodes[numLang - 1]
'en'
LanguageDetector.readExcerpts()

Trigram

class music21.text.Trigram(excerptList=None)

See LanguageDector above. From http://code.activestate.com/recipes/326576-language-detection-using-character-trigrams/

The frequency of three character sequences is calculated. When treated as a vector, this information can be compared to other trigrams, and the difference between them seen as an angle. The cosine of this angle varies between 1 for complete similarity, and 0 for utter difference. Since letter combinations are characteristic to a language, this can be used to determine the language of a body of text. For example:

>>> reference_en = Trigram('/path/to/reference/text/english')
>>> reference_de = Trigram('/path/to/reference/text/german')
>>> unknown = Trigram('url://pointing/to/unknown/text')
>>> unknown.similarity(reference_de)
#_DOCS_SHOW 0.4
>>> unknown.similarity(reference_en)
#_DOCS_SHOW 0.95

would indicate the unknown text is almost cetrtainly English. As syntax sugar, the minus sign is overloaded to return the difference between texts, so the above objects would give you:

#_DOCS_SHOW >>> unknown - reference_de #_DOCS_SHOW 0.6 #_DOCS_SHOW >>> reference_en - unknown # order doesn’t matter. #_DOCS_SHOW 0.05

As it stands, the Trigram ignores character set information, which means you can only accurately compare within a single encoding (iso-8859-1 in the examples). A more complete implementation might convert to unicode first.

As an extra bonus, there is a method to make up nonsense words in the style of the Trigram’s text.

#_DOCS_SHOW >>> reference_en.makeWords(30) #_DOCS_SHOW My withillonquiver and ald, by now wittlectionsurper, may sequia, tory, I ad my notter. Marriusbabilly She lady for rachalle spen hat knong al elf

Trigram read-only properties

Trigram.length

Trigram methods

Trigram.likely(k)

Returns a character likely to follow the given string two character string, or a space if nothing is found.

Trigram.makeWords(count)

returns a string of made-up words based on the known text.

Trigram.measure()

calculates the scalar length of the trigram vector and stores it in self.length.

Trigram.parseExcerpt(excerpt)
Trigram.similarity(other)

returns a number between 0 and 1 indicating similarity between two trigrams. 1 means an identical ratio of trigrams; 0 means no trigrams in common.