/// <summary> /// Devuelve una lista de extractos con las referencias y los fragmentos en las que se /// encuentra el texto de búsqueda pero sin que sea en la forma de ninguna de la exclusiones. /// </summary> /// <param name="searchText">Texto de búsqueda.</param> /// <param name="excluded">Un array de textos que contienen el texto de búsqueda.</param> /// <returns>Lista de extractos.</returns> public List<Excerpt> Search(string searchText, string[] excluded) { List<Excerpt> result = new List<Excerpt>(); if (GetParLength() == 0) return result; for (int p = 0; p < GetParLength(); p++) { Par par = GetPar(p); string refer = par.GetID(); string content = par.GetContentCleaned(); //Buscar recursivamente List<Position> found = CommonTasks.IndexesOf(content, searchText); List<Position> found2 = new List<Position>(); if (found.Count > 0) { if (excluded.Length > 0) { List<Position> foundExcluded = new List<Position>(); for (int i = 0; i < excluded.Length; i++) { List<Position> fExcluded = CommonTasks.IndexesOf(content, excluded[i]); for (int j = 0; j < fExcluded.Count; j++) { Position pos = new Position(); pos.Start = fExcluded[j].Start; pos.End = fExcluded[j].End; foundExcluded.Add(pos); } } for (int i = 0; i < found.Count; i++) { bool toExclude = false; for (int j = 0; j < foundExcluded.Count; j++) { if (found[i].Start >= foundExcluded[j].Start && found[i].End <= foundExcluded[j].End) { toExclude = true; } } if (!toExclude) { found2.Add(found[i]); } } } else { for (int i = 0; i < found.Count; i++) { found2.Add(found[i]); } } //Obtener la posición mínima y máxima int min = content.Length - 1, max = 0; for (int i = 0; i < found2.Count; i++) { if (found2[i].Start < min) min = found2[i].Start; if (found2[i].End > max) max = found2[i].End; } Excerpt excerpt = GetExcerpt(content, refer, min, max); result.Add(excerpt); } } return result; }
private Excerpt GetExcerpt(string content, string refer, int start, int end) { Excerpt excerpt = new Excerpt(); excerpt.Reference = refer; Position pos = new Position(); pos.Start = start - 45; if (pos.Start < 0) pos.Start = 0; pos.End = end + 45; if (pos.End > content.Length - 1) pos.End = content.Length - 1; excerpt.Position = pos; excerpt.Extract = content.Substring(pos.Start, pos.End - pos.Start); return excerpt; }