一套连接到强化批评实验室的工具
Augmented-Criticism-Lab-Toolkit的Python项目详细描述
增强的批评实验室工具包和连接器
这套工具是为与增强批评实验室的API接口而设计的, https://acriticismlab.org。工具包可以与pip一起安装:
pip install Augmented-Criticism-Lab-Toolkit
使用接头
连接器用于通过api从数据库中提取数据。下面是一些示例:
fromconnectors.poemimportPoem# To get a list of all poems:all_poems=Poem().all()# To get a specific poem by database id:single_poem=Poem().by_id(1)fromconnectors.bookimportBook# To get a list of all books:all_books=Book().all()# To get a specific book by database id:single_book=Book().by_id(1)
包含的连接符是book、poem和section。每个连接器的工作原理相同。
使用工具
基于api的工具:
fromtools.apiimportTools# Lemmatize text:lemmas=Tools().lemmatize("text to lemmatize")# Part of speech tags:tags=Tools().pos_tag("text to tag")# Frequency distribution:freqdist=Tools().frequency_distribution("text to get distribution for")# Topic model:model=Tools().topic_model("text to model")
注意:主题模型运行大约需要一分钟。
基于python的工具:
押韵方案分析器:
fromtools.rhymeimportRhymefromtools.rhymeimportclassify_sonnet# Initialize a Rhyme object with the text you want to analyze.# The text must be separated into lines, you can define a delimiter# the default is '\\n'. This returns a list of rhyme pairs:# ['A','B','B','A','C','D','D','C','E','F','E','F','G,'G']rhyme=Rhyme("text\n broken\n into lines",delimiter='\n').find_rhyme_scheme()# To classify the rhyme scheme (only works for sonnets) run:# Returns a tuple such that (each number represents a probability# the sonnet of the type listed):#(Petrarchan 1, Petrarchan 2, Petrarchan 3, Shakespearean, Spenserian)sonnet_type=classify_sonnet(rhyme)
音节计数器:
fromtools.syllableimportSyllableCounter# Initialize a counter:syllable_counter=SyllableCounter()# Run a line of poetry through the counter:syllable_count_for_line=syllable_counter.count_syllables_by_line("line of text")
也可以直接从acl数据库对诗歌运行音节计数器:
fromtools.syllableimportSyllableCounterfromconnectors.poemimportPoem# Initialize a counter:syllable_counter=SyllableCounter()# Get a poem:poem=Poem().by_id(1)# Get counts for the poem:counts=syllable_counter.count_syllables_poem(poem)
扩展离群值:(即没有给出正确音节计数的单词)
创建一个csv文件,格式为:
WORD, NUMBER_OF_SYLLABES
# Example
apple, 2
orange, 2
然后加载csv文件:
fromtools.syllableimportSyllableCounter# Initialize a SyllableCounter.syllable_counter=SyllableCounter()# Load the custom outliers file into the counter.syllable_counter.load_custom_outliers('PATH_TO_FILE')