Ejemplo n.º 1
0
        // ------------------------------------------------------------------------------------
        // Name:   AddFeature
        // Goal:   Add the feature [strFname] with value [strFvalue] to the node
        //         Result:
        //           <fs type='[strFStype]'>
        //             <f name='[strFname] value='[strFvalue]' />
        //           </fs>
        // History:
        // 26-05-2010  ERK Created
        // ------------------------------------------------------------------------------------
        public bool AddFeature(ref XmlDocument pdxThis, ref XmlNode ndxThis, string strFStype, string strFname, string strFvalue)
        {
            XmlNode ndxChild = null; // Child node of type <fs>

            try {
                // Validate
                if (pdxThis == null)
                {
                    return(false);
                }
                // Add this inside <fs type='NP'> to <f name='NPtype' value='...'/>
                ndxChild = oXmlTools.SetXmlNodeChild(pdxThis, ref ndxThis, "fs", "type;" + strFStype, "type", strFStype);
                // Validate
                if (ndxChild == null)
                {
                    // Show error
                    errHandle.Status("Could not add an appropriate <fs type='" + strFStype + "'> child");
                    // Return failure
                    return(false);
                }
                // Add or replace the <f> child
                if (oXmlTools.SetXmlNodeChild(pdxThis, ref ndxChild, "f", "name;" + strFname, "value", strFvalue) == null)
                {
                    // Show error
                    errHandle.Status("Could not add an appropriate <f name='" + strFname + "'> child");
                    // Return failure
                    return(false);
                }
                // Return success
                return(true);
            } catch (Exception ex) {
                // Show error
                errHandle.DoError("modAdapt/AddFeature", ex);
                // Return failure
                return(false);
            }
        }
Ejemplo n.º 2
0
        // ================================ METHODS ===================================================

        /* -------------------------------------------------------------------------------------
        * Name:        repairOneFolia
        * Goal:        Check and repair one folia file
        * History:
        * 21/mar/2016 ERK Created
        *  ------------------------------------------------------------------------------------- */
        public bool repairOneFolia(String sFileFoliaGz)
        {
            try {
                // Get the unzipped file name
                String sFileFolia = sFileFoliaGz.Replace(".gz", "");
                // Unzip the file
                if (!General.DecompressFile(sFileFoliaGz, sFileFolia))
                {
                    errHandle.DoError("cmbConv/repairOneFolia", "Could not decompress"); return(false);
                }
                // Check for empty begintime/endtime
                String[] arLine = File.ReadAllLines(sFileFolia);

                /*
                 * if (sFileFoliaGz.Contains("S-O_00214801")) {
                 * int iStop = 2;
                 * } */
                bool bChanged = false; int iChanges = 0;
                for (int i = 0; i < arLine.Length; i++)
                {
                    String sLine = arLine[i];
                    // Check the syntax of the begintime and the endtime
                    if (adaptTime(ref sLine, sLine.IndexOf("begintime=")))
                    {
                        bChanged = true;
                    }
                    if (adaptTime(ref sLine, sLine.IndexOf("endtime=")))
                    {
                        bChanged = true;
                    }

                    /*
                     * if (sLine.Contains("begintime=\"\"")) {
                     * sLine = sLine.Replace("begintime=\"\"", "begintime=\"00:00:00.000\"");
                     * bChanged = true;
                     * }
                     * if (sLine.Contains("endtime=\"\"")) {
                     * sLine = sLine.Replace("endtime=\"\"", "endtime=\"00:00:00.000\"");
                     * bChanged = true;
                     * } */
                    if (bChanged)
                    {
                        arLine[i] = sLine; iChanges++; bChanged = false;
                    }
                }
                // Only save results if something changed
                if (iChanges > 0)
                {
                    File.WriteAllLines(sFileFolia, arLine);
                    // Show repair log
                    errHandle.Status("Repaired file: [" + sFileFoliaGz + "] (" + iChanges + " repairs)");
                    // And compress into .gz
                    if (!General.CompressFile(sFileFolia, sFileFoliaGz))
                    {
                        errHandle.DoError("cmbConv/repairOneFolia", "Could not compress"); return(false);
                    }
                }
                else
                {
                    // Show repair log
                    errHandle.Status("Unchanged: [" + sFileFoliaGz + "]");
                }
                // Remove the unzipped file again
                File.Delete(sFileFolia);

                // Return positively
                return(true);
            } catch (Exception ex) {
                errHandle.DoError("cmdConv/repairOneFolia", ex);
                return(false);
            }
        }
Ejemplo n.º 3
0
        static List <String> lstCmdi  = null; // List of .cmdi.xml files in the [sCmdi] directory
        // Command-line entry point + argument handling
        static void Main(string[] args)
        {
            String sLanguage = "";      // Which language to take (two-letter code 'en' or 'nl')
            String sFolia    = "";      // Base directory to take (e.g. /vol/tensusers/ekomen)
            String sSubtiel  = "";      // Base directory for the output (e.g: /vol/tensusers/ekomen/subtiel)
            String sCmdi     = "";      // Base directory for CMDI files
            String sAction   = "combi"; // The particular type of action expected. Default "combi"
            bool   bIsDebug  = false;   // Debugging

            try {
                // Check command-line options
                for (int i = 0; i < args.Length; i++)
                {
                    // get this argument
                    String sArg = args[i];
                    if (sArg.StartsWith("-"))
                    {
                        // Check out the arguments
                        switch (sArg.Substring(1))
                        {
                        case "f": // Root directory under which the .folia.xml.gz files are located
                            sFolia = Path.GetFullPath(args[++i]);
                            break;

                        case "c": // Root directory where the CMDI files are located
                            sCmdi = Path.GetFullPath(args[++i]);
                            break;

                        case "o": // Root directory where the output files are going to be stored
                            sSubtiel = Path.GetFullPath(args[++i]);
                            break;

                        case "d": // Debugging
                            bIsDebug = true;
                            break;

                        case "l": // Language (three letter code)
                            sLanguage = args[++i];
                            break;

                        case "r": // INSTEAD of 'combi', perform 'REPAIR' action
                            sAction = "repair";
                            break;
                        }
                    }
                    else
                    {
                        // Throw syntax error and leave
                        SyntaxError("1 - i=" + i + " args=" + args.Length + " argCurrent=[" + sArg + "]"); return;
                    }
                }
                // Check presence of input/output
                if (sFolia == "" || !Directory.Exists(sFolia))
                {
                    SyntaxError("No (valid) base directory for FoLiA input"); return;
                }

                // Initialise the Treebank Xpath functions, which may make use of tb:matches()
                XPathFunctions.conTb.AddNamespace("tb", XPathFunctions.TREEBANK_EXTENSIONS);

                // Create a new instance of the combination class
                cmbConv oConv = new cmbConv(errHandle);

                // Other directories depend on ACTION
                switch (sAction)
                {
                case "repair":
                    // No further directories need be present
                    oConv.output = sFolia;
                    // Process all the directories with this action
                    WalkDirectoryTree(sFolia, "*.folia.xml.gz", sAction, ref oConv);
                    break;

                default:
                    if (sCmdi == "" || !Directory.Exists(sCmdi))
                    {
                        SyntaxError("No (valid) cmdi directory"); return;
                    }
                    if (sSubtiel == "")
                    {
                        SyntaxError("No subtiel directory"); return;
                    }
                    // If the target directory is not there, create it
                    if (!Directory.Exists(sSubtiel))
                    {
                        Directory.CreateDirectory(sSubtiel);
                    }
                    oConv.output = sSubtiel;
                    // find .cmdi.xml files
                    errHandle.Status("Finding .cmdi.xml files...");
                    lstCmdi = Directory.GetFiles(sCmdi, "*.cmdi.xml", SearchOption.AllDirectories).ToList();

                    // Convert the list of files into a dictionary
                    oConv.cmdi(lstCmdi);

                    // Find .folia.xml.gz files
                    errHandle.Status("Finding directories in " + sFolia);
                    // Walk all directories
                    errHandle.Status("Processing .folia.xml.gz files...");
                    // Output the header
                    oConv.doHeaderCsv();
                    // Resursive call for each subdirectory.
                    WalkDirectoryTree(sFolia, "*.folia.xml.gz", sAction, ref oConv);

                    // Save the harvested information to an xml file
                    if (!oConv.harvestSaveXml(sSubtiel + "/harvest.xml"))
                    {
                        errHandle.DoError("Main", "Could not create harvest summary xml file");
                        return;
                    }
                    break;
                }


                // Exit the program
                errHandle.Status("Ready");
            } catch (Exception ex) {
                errHandle.DoError("Main", ex); // Provide standard error message
                throw;
            }
        }
Ejemplo n.º 4
0
        // =================== Local variables ===============================================
        // Command-line entry point + argument handling
        static void Main(string[] args)
        {
            String sDutch    = "";        // Input directory for DUTCH subtitles
            String sEnglish  = "";        // Input directory for ENGLISH subtitles
            String sOutput   = "";        // Output directory
            String sParallel = "";        // File in .xml format that contains the parallel between EN-NL
            bool   bIsDebug  = false;     // Debugging
            bool   bForce    = false;     // Force
            String sAction   = "english"; // Type of action to be taken

            try {
                // Check command-line options
                for (int i = 0; i < args.Length; i++)
                {
                    // get this argument
                    String sArg = args[i];
                    if (sArg.StartsWith("-"))
                    {
                        // Check out the arguments
                        switch (sArg.Substring(1))
                        {
                        case "p": // Xml file containing the parallels between Eng and NL
                            sParallel = args[++i];
                            break;

                        case "n": // Input directory for Dutch: .folia.xml and .cmdi.xml files
                            sDutch = args[++i];
                            break;

                        case "e": // Input directory for English: .folia.xml and .cmdi.xml files
                            sEnglish = args[++i];
                            break;

                        case "o": // Top output directory
                            sOutput = args[++i];
                            break;

                        case "f": // Force
                            bForce = true;
                            break;

                        case "a": // Action
                            sAction = args[++i].ToLower();
                            if (sAction != "english")
                            {
                                SyntaxError("The only action right now is 'english'");
                                return;
                            }
                            break;

                        case "d": // Debugging
                            bIsDebug = true;
                            break;

                        default:
                            // Throw syntax error and leave
                            SyntaxError("Unknown option: [" + sArg + "]");
                            return;
                        }
                    }
                    else
                    {
                        // Throw syntax error and leave
                        SyntaxError("1 - i=" + i + " args=" + args.Length + " argCurrent=[" + sArg + "]"); return;
                    }
                }
                // Check presence of input/output
                if (sDutch == "" || sEnglish == "")
                {
                    SyntaxError("Both dutch and english input must be specified"); return;
                }
                if (sParallel == "")
                {
                    SyntaxError("The XML file containing the Dutch-English parallels must be specified"); return;
                }

                // Check input directory and parallels file
                if (!Directory.Exists(sDutch))
                {
                    errHandle.DoError("Main", "Cannot find Dutch input file(s) in: " + sDutch); return;
                }
                if (!Directory.Exists(sEnglish))
                {
                    errHandle.DoError("Main", "Cannot find English input file(s) in: " + sEnglish); return;
                }
                if (!File.Exists(sParallel))
                {
                    errHandle.DoError("Main", "Cannot find parallel file in: " + sParallel); return;
                }

                // Initialize the main entry point for the conversion
                engConv objConv = new engConv(errHandle);

                // Set directories where input is situated and output should come
                objConv.dutch   = sDutch;
                objConv.english = sEnglish;
                objConv.output  = sOutput;

                // Initialise the Treebank Xpath functions, which may make use of tb:matches()
                opsub.util.XPathFunctions.conTb.AddNamespace("tb", opsub.util.XPathFunctions.TREEBANK_EXTENSIONS);

                // Start reading the parallels XML
                ParReader oParallel = new ParReader(sParallel, errHandle);
                // Walk all the <linkGrp> elements
                XmlReader rdLinkGrp = null;
                while (oParallel.getNextLinkGrp(ref rdLinkGrp))
                {
                    // Process this one
                    if (!objConv.ConvertOneEngToFolia(rdLinkGrp, bForce, bIsDebug))
                    {
                        errHandle.DoError("Main", "Could not convert English");
                        return;
                    }
                }

                /*
                 * XmlNode ndxLinkGrp = oParallel.getNextLinkGrp();
                 * while (ndxLinkGrp!= null) {
                 * // Process this one
                 * if (!objConv.ConvertOneEngToFolia(ndxLinkGrp, bForce, bIsDebug)) { errHandle.DoError("Main", "Could not convert English"); return; }
                 * // Go to the next one
                 * ndxLinkGrp = oParallel.getNextLinkGrp();
                 * }
                 */

                // Exit the program
                errHandle.Status("Ready");
            } catch (Exception ex) {
                errHandle.DoError("Main", ex); // Provide standard error message
                throw;
            }
        }