def get_tier(filename, tier_name, verbose=True): """ Get a tier from a file. :param filename: (str) Name of the annotated file. :param tier_name: (str) Name of the tier :param verbose: (bool) Print message :returns: (Tier) """ # Read an annotated file. if verbose: print("Read file: {:s}".format(filename)) trs = trsaio.read(filename) if verbose: print(" ... [ OK ] ") # Get the expected tier if verbose: print("Get tier {:s}".format(tier_name)) tier = trs.Find(tier_name, case_sensitive=False) if tier is None: print("Tier not found.") sys.exit(1) if verbose: print(" ... [ OK ] ") return tier
def printFreqAnn(filename, pattern): """ Print the number of occurrences of a pattern in each tier of a file. """ print("Take a look at file {:s}:".format(filename)) trs = trsaio.read(filename) pattern = pattern.strip() for tier in trs: c = 0 for ann in tier: if ann.GetLabel().GetValue() == pattern: c += 1 print(" Tier {:s}: {:d}".format(tier.GetName(), c))
def printInfo(filename): """ Print information about tiers of a file. """ # Read the file. print("Take a look at file {:s}:".format(filename)) trs = trsaio.read(filename) for tier in trs: # Get the tier type tier_type = "Unknown" if tier.IsPoint() is True: tier_type = "Point" elif tier.IsInterval() is True: tier_type = "Interval" # Print all information print(" * Tier: {:s}".format(tier.GetName())) print(" - Type: {:s}".format(tier_type)) print(" - Number of annotations: {:d}".format(len(tier))) print(" - From time: {:.4f}".format(tier.GetBeginValue())) print(" - To time: {:.4f} ".format(tier.GetEndValue()))
if len(sys.argv) <= 1: sys.argv.append('-h') args = parser.parse_args() # ---------------------------------------------------------------------------- # Load input data mapping = TierMapping(args.m) mapping.set_reverse(False) # from sampa to ipa direction mapping.set_keep_miss(True) # keep unknown entries as given mapping.set_miss_symbol("") # not used! mapping.set_delimiters([]) # longest matching # read content trs_input = aio.read(args.i) # ---------------------------------------------------------------------------- # Convert input file trs = Transcription(name=trs_input.GetName()+"-IPA") for n in args.n.split(','): print(" -> Tier {:s}:".format(n)) tier = trs_input.Find(n, case_sensitive=False) if tier is not None: new_tier = mapping.map_tier(tier) new_tier.SetName(n+"-IPA") new_tier.metadata = tier.metadata trs.Append(new_tier) else:
# The input file name filename = 'F_F_B003-P9-merge.TextGrid' # The list of tiers we want to select tier_names = ['PhonAlign', 'TokensAlign', 'toto'] # The output file name output_filename = 'F_F_B003-P9-selection.TextGrid' # ---------------------------------------------------------------------------- # Main # ---------------------------------------------------------------------------- # Read an annotated file. print("Read the file {:s}".format(filename)) trs = trsaio.read(filename) print(" Number of tiers: {:d}.".format(trs.GetSize())) # Create a new Transcription to add selected tiers. new_trs = Transcription() # Select some tiers, add into the new Transcription for name in tier_names: tier = trs.Find(name, case_sensitive=False) if tier is not None: new_trs.Append(tier) print(" Tier {:s} successfully added.".format(tier.GetName())) else: print(" Error: the file does not contain a tier with name = {:s}".format(name)) # Save the Transcription into a file.
# ---------------------------------------------------------------------------- # Convert input file if not TextGrid filename = args.i fname, fext = os.path.splitext(filename) if fname.endswith("-palign") is False: print("ERROR: MarsaTag plugin requires SPPAS alignment files " "(i.e. with -palign in its name).") sys.exit(1) if fext.lower() != "textgrid": # read content trs_input = aio.read(filename) tier = trs_input.Find("TokensAlign", case_sensitive=False) if tier is None: print("ERROR: A tier with name TokensAlign is required.") sys.exit(1) # write as textgrid trs = Transcription(name="TokensAlign") trs.Append(tier) filename = fname + ".TextGrid" aio.write(filename, trs) # ---------------------------------------------------------------------------- # Get MarsaTag path if len(args.p) == 0:
if len(sys.argv) <= 1: sys.argv.append('-h') args = parser.parse_args() # ---------------------------------------------------------------------------- # Load input data mapping = TierMapping(args.m) mapping.set_reverse(False) # from sampa to praat direction mapping.set_keep_miss(True) # keep unknown entries as given mapping.set_miss_symbol("") # not used! mapping.set_delimiters([]) # longest matching # read content trs_input = aio.read(args.i) # ---------------------------------------------------------------------------- # Convert input file trs = Transcription(name=trs_input.GetName()+"-IPA") for n in args.n.split(','): print(" -> Tier {:s}:".format(n)) tier = trs_input.Find(n, case_sensitive=False) if tier is not None: new_tier = mapping.map_tier(tier) new_tier.SetName(n+"-IPA") trs.Append(new_tier) else: print(" [IGNORED] Wrong tier name.")
:copyright: Copyright (C) 2017 Fixme :summary: Open an annotated file and save it as CSV file. """ import sys import os.path SPPAS_IS_HERE = os.path.join("..", "..") sys.path.append(SPPAS_IS_HERE) import sppas.src.annotationdata.aio as trsaio # ---------------------------------------------------------------------------- # Variables # ---------------------------------------------------------------------------- input_filename = 'F_F_B003-P9-merge.TextGrid' output_filename = input_filename.replace('.TextGrid', '.csv') # ---------------------------------------------------------------------------- # Main # ---------------------------------------------------------------------------- if __name__ == '__main__': # Read an annotated file, put content in a Transcription object. trs = trsaio.read(input_filename) # Save the Transcription object into a file. trsaio.write(output_filename, trs)