Python中的UIMA CAS处理库
dkpro-cassis的Python项目详细描述
dkprocas is(发音:[ka.sis])是 Python。当前支持的功能有:
- 从/到xmi反序列化/序列化uima cas
- 从/到XML反序列化/序列化类型系统
- 选择注释,选择覆盖的注释,添加 注释
- 类型继承
- 沙发支架
一些功能仍在开发中,例如
- 功能编码为XML元素(目前只有XML属性工作)
- 正确的类型检查
- XML/XMI模式验证
- 从字符串到在 键入系统
- 参考、数组和列表功能
安装
要使用pip从主分支安装包,只需运行
pip install git+https://github.com/dkpro/dkpro-cassis
用法
示例cas xmi和types系统文件可以在tests\test_files
下找到。
加载cas
可以通过读取文件或
使用load_cas_from_xmi
的字符串。
fromcassisimport*withopen('typesystem.xml','rb')asf:typesystem=load_typesystem(f)withopen('cas.xml','rb')asf:cas=load_cas_from_xmi(f,typesystem=typesystem)
添加注释
给定具有类型cassis.Token
的类型系统,该类型系统具有id
和
pos
功能,可以在以下位置添加批注:
fromcassisimport*withopen('typesystem.xml','rb')asf:typesystem=load_typesystem(f)withopen('cas.xml','rb')asf:cas=load_cas_from_xmi(f,typesystem=typesystem)Token=typesystem.get_type('cassis.Token')tokens=[Token(begin=0,end=3,id='0',pos='NNP'),Token(begin=4,end=10,id='1',pos='VBD'),Token(begin=11,end=14,id='2',pos='IN'),Token(begin=15,end=18,id='3',pos='DT'),Token(begin=19,end=24,id='4',pos='NN'),Token(begin=25,end=26,id='5',pos='.'),]fortokenintokens:cas.add_annotation(token)
选择注释
fromcassisimport*withopen('typesystem.xml','rb')asf:typesystem=load_typesystem(f)withopen('cas.xml','rb')asf:cas=load_cas_from_xmi(f,typesystem=typesystem)forsentenceincas.select('cassis.Sentence'):fortokenincas.select_covered('cassis.Token',sentence):print(cas.get_covered_text(token))# Annotation values can be accessed as propertiesprint('Token: begin={0}, end={1}, id={2}, pos={3}'.format(token.begin,token.end,token.id,token.pos))
创建类型并添加功能
fromcassisimport*typesystem=TypeSystem()parent_type=typesystem.create_type(name='example.ParentType')typesystem.add_feature(type_=parent_type,name='parentFeature',rangeTypeName='String')child_type=typesystem.create_type(name='example.ChildType',supertypeName=parent_type.name)typesystem.add_feature(type_=child_type,name='childFeature',rangeTypeName='Integer')annotation=child_type(parentFeature='parent',childFeature='child')
添加新功能时,将传播这些更改。例如, 将要素添加到父类型可使其对子类型可用。 因此,不需要冻结类型系统以保持一致性。
沙发支架
sofa表示在uima管道中处理的某种形式的非结构化工件。例如,它包含文档文本。目前,可以创建新沙发。这在创建新视图时自动完成。沙发的基本特性可以读写:
cas=Cas()cas.sofa_string="Joe waited for the train . The train was late ."cas.sofa_mime="text/plain"print(cas.sofa_string)print(cas.sofa_mime)
管理视图
cas的视图包含特征结构和注释的子集。一个视图正好对应一个沙发。它还可用于查询和更改有关沙发的信息,例如文档文本。添加到一个视图的批注在另一个视图中不可见。可以创建和更改视图视图。视图具有与Cas
相同的方法和属性。
fromcassisimport*withopen('typesystem.xml','rb')asf:typesystem=load_typesystem(f)Token=typesystem.get_type('cassis.Token')# This creates automatically the view `_InitialView`cas=Cas()cas.sofa_string="I like cheese ."cas.add_annotations([Token(begin=0,end=1),Token(begin=2,end=6),Token(begin=7,end=13),Token(begin=14,end=15)])print([cas.get_covered_text(x)forxincas.select_all()])# Create a new view and work on it.view=cas.create_view('testView')view.sofa_string="I like blackcurrant ."view.add_annotations([Token(begin=0,end=1),Token(begin=2,end=6),Token(begin=7,end=19),Token(begin=20,end=21)])print([view.get_covered_text(x)forxinview.select_all()])
开发
所需的依赖项由pip管理。虚拟环境 包含开发和生产所需的所有包 由创建和激活
virtualenv venv --python=python3 --no-site-packages sourve venv/bin/activate pip install -e ".[test, dev, doc]"
通过调用
make test
或在清洁的环境中,通过
tox