Ejemplo n.º 1
0
        private void ReadTheSource(string path)
        {
            if (!File.Exists(path) || new FileInfo(path).Length == 0)
                throw new FileNotFoundException("Source file isn't exists or it's empty!");

            _documents.Clear();
            WriteDebugInfo("Start reading of the file...");
            try
            {
                using (CsvFileReader reader = new CsvFileReader(path, EmptyLineBehavior.Ignore))
                {
                    bool isFirstRow = true;
                    bool hasComplianceScore = false;
                    List<string> row = new List<string>();
                    List<string> services = new List<string>();

                    while (reader.ReadRow(row))
                    {
                        if (isFirstRow)
                        {
                            isFirstRow = false;
                            if (row.Count <= 1)
                                throw new Exception("Source file isn't proper CSV file for SemantAPI.Human application");
                            if (!row[0].Equals("Document ID"))
                                throw new Exception("Source file doesn't have \"Document ID\" column or it's not the first one.");
                            if (row[row.Count - 1] != "Source text")
                                throw new Exception("Source file doesn't have \"Source text\" column or it's not the last one.");

                            for (int index = 1; index < row.Count - 1; index++)
                            {
                                string service = row[index].Split(' ').First();
                                if (!services.Contains(service))
                                    services.Add(service);
                            }

                            hasComplianceScore = row.Any(item => item.Contains("Compliance"));
                            continue;
                        }

                        int col = 1;
                        string polarity = string.Empty;
                        ResultSet result = new ResultSet(row[row.Count - 1]);

                        foreach (string service in services)
                        {
                            result.AddOutput(service, double.Parse(row[col + 1]), row[col]);
                            if (hasComplianceScore)
                                polarity = row[col + 2];

                            col += (hasComplianceScore && service != "MechanicalTurk") ? 3 : 2;
                        }

                        if (hasComplianceScore)
                            result.AddReferencePolarity(polarity, "MechanicalTurk");

                        _documents.Add(row[0], result);
                        Application.DoEvents();
                    }

                    WriteDebugInfo("Source file has been loaded.");
                }
            }
            catch (IOException ex)
            {
                throw new Exception("File is locked or another IO problem happened.", ex);
            }
            catch (Exception ex)
            {
                throw new Exception("File is locked or another IO problem happened.", ex);
            }

            return;
        }
Ejemplo n.º 2
0
        private void ReadTheSource(string path, int cutBy)
        {
            if (!File.Exists(path) || new FileInfo(path).Length == 0)
                throw new FileNotFoundException("Source file isn't exists or it's empty!");

            bool isCSV = path.Contains(".csv");

            _documents.Clear();
            WriteDebugInfo("Start reading of the file...");

            try
            {
                using (CsvFileReader reader = new CsvFileReader(path, EmptyLineBehavior.Ignore))
                {
                    List<string> row = new List<string>();
                    while (reader.ReadRow(row))
                    {
                        if (row == null || row.Count < 1)
                            continue;

                        string text = row[row.Count - 1];
                        if (text.Length > cutBy)
                            text = text.Substring(0, cutBy);

                        _documents.Add(Guid.NewGuid().ToString(), new ResultSet(text));
                        Application.DoEvents();
                    }

                    WriteDebugInfo("Source file has been loaded.");
                }
            }
            catch (IOException ex)
            {
                throw new Exception("File is locked or another IO problem happened.", ex);
            }

            return;
        }