Utility routines for processing text in scores and other musical objects.
Concatenate text from a stream. The Stream is automatically flattened.
The lineNumber parameter determines which line of text is assembled.
>>> from music21 import *
>>> 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'
Given a text string, if an article is found in a leading position, place it at the end with a comma.
>>> from music21 import *
>>> 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'
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.
>>> from music21 import *
>>> 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'
Inherits from: Music21Object, JSONSerializer, TextFormat
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()
TextBox attributes
- classSortOrder¶
Property which returns an number (int or otherwise) depending on the class of the Music21Object that represents a priority for an object based on its class alone – used as a tie for stream sorting in case two objects have the same offset and priority. Lower numbers are sorted to the left of higher numbers. For instance, Clef, KeySignature, TimeSignature all come (in that order) before Note.
All undefined classes have classSortOrder of 20 – same as note.Note
>>> from music21 import * >>> tc = clef.TrebleClef() >>> tc.classSortOrder 0 >>> ks = key.KeySignature(3) >>> ks.classSortOrder 1New classes can define their own default classSortOrder
>>> class ExampleClass(base.Music21Object): ... classSortOrderDefault = 5 ... >>> ec1 = ExampleClass() >>> ec1.classSortOrder 5Attributes inherited from Music21Object: isSpanner, isStream, isVariant, id, groups, hideObjectOnPrint
TextBox properties
- alignHorizontal¶
Get or set the horicontal align.
>>> from music21 import * >>> te = text.TextBox('testing') >>> te.alignHorizontal = 'right' >>> te.alignHorizontal 'right'
- alignVertical¶
Get or set the vertical align. Valid values are top, middle, bottom, and baseline
>>> from music21 import * >>> te = text.TextBox('testing') >>> te.alignVertical = 'top' >>> te.alignVertical 'top'
- content¶
Get or set the the content.
>>> from music21 import * >>> te = text.TextBox('testing') >>> te.content 'testing' >>> te.justify = 'center' >>> te.justify 'center'
- page¶
Get or set the the page number. The first page (page 1) is the default.
>>> from music21 import * >>> te = text.TextBox('testing') >>> te.content 'testing' >>> te.page 1
- positionHorizontal¶
Get or set the vertical position.
>>> from music21 import * >>> te = text.TextBox('testing') >>> te.positionHorizontal = 200 >>> te.positionHorizontal 200
- positionVertical¶
Get or set the vertical position.
>>> from music21 import * >>> te = text.TextBox('testing') >>> te.positionVertical = 1000 >>> te.positionVertical 1000Properties inherited from Music21Object: activeSite, beat, beatDuration, beatStr, beatStrength, classes, derivationHierarchy, duration, isGrace, measureNumber, offset, priority, seconds
Properties inherited from JSONSerializer: json
Properties inherited from TextFormat: justify, letterSpacing, size, style, weight
TextBox methods
Methods inherited from Music21Object: searchActiveSiteByAttr(), getContextAttr(), setContextAttr(), addContext(), addLocation(), addLocationAndActiveSite(), freezeIds(), getAllContextsByClass(), getCommonSiteIds(), getCommonSites(), getContextByClass(), getOffsetBySite(), getSiteIds(), getSites(), getSpannerSites(), hasContext(), hasSite(), hasSpannerSite(), hasVariantSite(), isClassOrSubclass(), mergeAttributes(), next(), previous(), purgeLocations(), purgeOrphans(), purgeUndeclaredIds(), removeLocationBySite(), removeLocationBySiteId(), setOffsetBySite(), show(), splitAtDurations(), splitAtQuarterLength(), splitByQuarterLengths(), unfreezeIds(), unwrapWeakref(), wrapWeakref(), write()
Methods inherited from JSONSerializer: jsonAttributes(), jsonComponentFactory(), jsonPrint(), jsonRead(), jsonWrite()
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 properties
- justify¶
Get or set the the justification.
>>> from music21 import * >>> tf = TextFormat() >>> tf.justify = 'center' >>> tf.justify 'center'
- letterSpacing¶
Get or set the letter spacing.
>>> from music21 import * >>> tf = TextFormat() >>> tf.letterSpacing = 20 >>> tf.letterSpacing 20.0 >>> tf.letterSpacing = 'normal'
- size¶
Get or set the size.
>>> from music21 import * >>> tf = TextFormat() >>> tf.size = 20 >>> tf.size 20.0
- style¶
Get or set the style, as normal, italic, bold, and bolditalic.
>>> from music21 import * >>> tf = TextFormat() >>> tf.style = 'bold' >>> tf.style 'bold'
- weight¶
Get or set the weight, as normal, or bold.
>>> from music21 import * >>> tf = TextFormat() >>> tf.weight = 'bold' >>> tf.weight 'bold'
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 attributes
Attributes without Documentation: languageCodes, languageLong, text, trigrams
LanguageDetector methods
- 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
>>> from music21 import * >>> ld = text.LanguageDetector() >>> ld.mostLikelyLanguage("Hello there, how are you doing today? I haven't seen you in a while.") 'en'
- 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 = LanguageDetector() >>> for i in range(0, len(ld.languageCodes)): ... print 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'
- readExcerpts()¶
No documentation.
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)
0.4
>>> unknown.similarity(reference_en)
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:
>>> unknown - reference_de
0.6
>>> reference_en - unknown # order doesn't matter.
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.
>>> reference_en.makeWords(30)
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 attributes
Attributes without Documentation: lut
Trigram methods
- likely(k)¶
Returns a character likely to follow the given string two character string, or a space if nothing is found.
- makeWords(count)¶
returns a string of made-up words based on the known text.
- measure()¶
calculates the scalar length of the trigram vector and stores it in self.length.
- parseExcerpt(excerpt)¶
No documentation.
- 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.