Skip to content

Commit

Permalink
[foliaupgrade] convert undefined sets
Browse files Browse the repository at this point in the history
  • Loading branch information
proycon committed Mar 27, 2019
1 parent 58a72f5 commit 462f983
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion folia
30 changes: 29 additions & 1 deletion foliatools/foliaupgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,30 @@ def main():
success = process(*args.files, **args.__dict__)
sys.exit(0 if success else 1)

def convert_set(annotationtype, annotationset):
if annotationset == "undefined":
if annotationtype == folia.AnnotationType.TEXT:
return folia.DEFAULT_TEXT_SET
elif annotationtype == folia.AnnotationType.PHON:
return folia.DEFAULT_PHON_SET
else:
return None
else:
return annotationset

def convert_undefined_sets(doc):
exempt = set()
for element in doc.items():
if isinstance(element, folia.AbstractElement):
if element.set == "undefined" and element.cls:
exempt.add(element.ANNOTATIONTYPE)
for element in doc.items():
if isinstance(element, folia.AbstractElement):
if element.set == "undefined" and element.ANNOTATIONTYPE not in exempt:
element.set = None
doc.annotations = [ (annotationtype, convert_set(annotationtype, annotationset) ) for annotationtype, annotationset in doc.annotations ]


def annotators2processors(doc, mainprocessor):
"""Convert FoLiA v1 style annotators to v2 style processors (limited)"""
for element in doc.items():
Expand Down Expand Up @@ -66,8 +90,12 @@ def process(*files, **kwargs):
doc = validate(file,schema=None, stricttextvalidation=True,autodeclare=True,output=False, warn=False,processor=mainprocessor,traceback=True,**kwargs)
if doc is not False:
print("Upgrading " + doc.filename,file=sys.stderr)
doc.version = folia.FOLIAVERSION #upgrading involves more than just bumping the number, but that is handled implicitly already by the library when reading the document
#bump the version number
doc.version = folia.FOLIAVERSION
#convert old style annotators to new style processors
annotators2processors(doc, mainprocessor)
#convert undefined sets
convert_undefined_sets(doc)
doc.save(doc.filename + ".upgraded")
if not validate(file + ".upgraded",schema=None,stricttextvalidation=True,autodeclare=False,traceback=True,**kwargs):
print("Upgrade failed",file=sys.stderr)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,5 @@ def read(fname):
},
#include_package_data=True,
package_data = {'foliatools': ['*.xsl']},
install_requires=['folia >= 2.0.1', 'lxml >= 2.2','docutils', 'pyyaml']
install_requires=['folia >= 2.0.5', 'lxml >= 2.2','docutils', 'pyyaml']
)

0 comments on commit 462f983

Please sign in to comment.