PageRenderTime 75ms CodeModel.GetById 38ms RepoModel.GetById 0ms app.codeStats 0ms

/src/project/framework/framework/Controller.cs

https://bitbucket.org/realhidden/multimediaeval
C# | 291 lines | 228 code | 29 blank | 34 comment | 15 complexity | f0537ad1611f9d5835cecc6424d2db6b MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Xml.Linq;
  8. namespace framework
  9. {
  10. class Controller
  11. {
  12. public static int UpperLimit = -1;
  13. public static int LowerLimit = -1;
  14. public static string DataDirectory;
  15. public static string OutputDirectory;
  16. public static string Separator = ""+System.IO.Path.DirectorySeparatorChar;
  17. public delegate List<OneImage> Algorithm(OneLocation keyword);
  18. static public Algorithm CurrentAlgorithm;
  19. static public List<OneImage> RunAlgorithm(OneLocation keyword)
  20. {
  21. return CurrentAlgorithm(keyword);
  22. }
  23. static public List<OneImage> DummyAlgorithm(OneLocation keyword)
  24. {
  25. return keyword.Images;
  26. }
  27. static public void WriteRunningResultToFile(List<OneImage> resultList,string locationName,int locationNum)
  28. {
  29. using (StreamWriter writer = new StreamWriter(OutputDirectory + Separator + locationName + ".out", false))
  30. {
  31. //export only 50 lines
  32. int maxLines = 50;
  33. int currLines = 0;
  34. foreach (var result in resultList)
  35. {
  36. writer.WriteLine(locationNum + "\tQ0\t" + result.ID + "\t" + currLines + "\t" + (maxLines-currLines) + "\tignoreit");
  37. currLines++;
  38. if (currLines==maxLines)
  39. break;
  40. }
  41. }
  42. }
  43. /// <summary>
  44. /// Reads the location list.
  45. /// </summary>
  46. /// <returns>The location list.</returns>
  47. /// <param name="LocationListName">Location list name.</param>
  48. /// <param name="LowerNumber">Lower number.</param>
  49. /// <param name="UpperNumber">Upper number.</param>
  50. static public List<OneLocation> ReadLocationList(string LocationListName, int LowerNumber = 0, int UpperNumber = 0)
  51. {
  52. List < OneLocation > keywords = new List<OneLocation>();
  53. var keywordXML = XDocument.Load(LocationListName);
  54. foreach (var topic in keywordXML.Root.Descendants("topic"))
  55. {
  56. double d = -1;
  57. bool b = false;
  58. if (UpperNumber-- == 0)
  59. return keywords;
  60. if (!(LowerNumber-- > 0))
  61. {
  62. OneLocation tmp = new OneLocation();
  63. foreach (var t in topic.Descendants())
  64. {
  65. switch (t.Name.LocalName)
  66. {
  67. case "number":
  68. tmp.Num = int.Parse(t.Value);
  69. break;
  70. case "title":
  71. tmp.Title = t.Value;
  72. break;
  73. case "latitude":
  74. d = -1;
  75. b = Double.TryParse(t.Value, out d);
  76. if (!b)
  77. {
  78. b = Double.TryParse(t.Value.Replace(".", ","), out d);
  79. }
  80. tmp.Latitude = d;
  81. break;
  82. // tmp.Latitude = double.Parse(t.Value);
  83. case "longitude":
  84. d = -1;
  85. b = Double.TryParse(t.Value, out d);
  86. if (!b)
  87. {
  88. b = Double.TryParse(t.Value.Replace(".", ","), out d);
  89. }
  90. tmp.Longitude = d;
  91. // tmp.Longitude = double.Parse(t.Value);
  92. break;
  93. case "wiki":
  94. tmp.Wiki = t.Value;
  95. break;
  96. default:
  97. break;
  98. }
  99. }
  100. keywords.Add(tmp);
  101. }
  102. }
  103. return keywords;
  104. }
  105. /// <summary>
  106. /// Gets a specific line from the csv file, which has the imageid as first item
  107. /// </summary>
  108. /// <returns>The line for a specific image in the csv file, if found/returns>
  109. /// <param name="csvdata">csv file data</param>
  110. /// <param name="imageid">the image id we are searching for</param>
  111. static private List<string> getLineForImageFromCsv(List<string[]> csvdata, long imageid)
  112. {
  113. foreach (var line in csvdata) {
  114. if (long.Parse (line [0]) == imageid)
  115. return new List<string>(line);
  116. }
  117. return new List<string> ();
  118. }
  119. /// <summary>
  120. /// Reads the locations into OneLocation list
  121. /// </summary>
  122. /// <returns>The locations.</returns>
  123. /// <param name="DirectoryPath">Directory path.</param>
  124. /// <param name="Location">Location.</param>
  125. static public List<OneLocation> ReadLocations(string DirectoryPath, List<OneLocation> Location)
  126. {
  127. double d = -1;
  128. bool b = false;
  129. // each LOCATION
  130. foreach (var location in Location)
  131. {
  132. //image list
  133. List<OneImage> results = new List<OneImage>();
  134. //load
  135. var faceCSV=ReadCSVFileIntoArray (DirectoryPath + Controller.Separator + "descvis" + Controller.Separator + "img" + Controller.Separator + location.Title + " FACE.csv");
  136. var cnCSV=ReadCSVFileIntoArray (DirectoryPath + Controller.Separator + "descvis" + Controller.Separator + "img" + Controller.Separator + location.Title + " CN.csv");
  137. var cmCSV=ReadCSVFileIntoArray (DirectoryPath + Controller.Separator + "descvis" + Controller.Separator + "img" + Controller.Separator + location.Title + " CM.csv");
  138. //parse the location xml
  139. var keywordXML = XDocument.Load(DirectoryPath + Controller.Separator + "xml"+ Controller.Separator + location.Title + ".xml" );
  140. foreach (var photo in keywordXML.Root.Descendants())
  141. {
  142. OneImage currImg = new OneImage();
  143. //some retarded xml parser
  144. foreach (var t in photo.Attributes())
  145. {
  146. switch (t.Name.LocalName)
  147. {
  148. case "views":
  149. currImg.Views = int.Parse(t.Value);
  150. break;
  151. case "title":
  152. currImg.Title = t.Value;
  153. break;
  154. case "username":
  155. currImg.Username = t.Value;
  156. break;
  157. case "url_b":
  158. currImg.Url_b = t.Value;
  159. break;
  160. case "tags":
  161. currImg.Tags = t.Value;
  162. break;
  163. case "rank":
  164. currImg.Rank = int.Parse(t.Value);
  165. break;
  166. case "nbComments":
  167. currImg.NbOfComments = int.Parse(t.Value);
  168. break;
  169. case "longitude":
  170. d = -1;
  171. b = Double.TryParse(t.Value, out d);
  172. if (!b)
  173. {
  174. b = Double.TryParse(t.Value.Replace(".", ","), out d);
  175. }
  176. currImg.Longitude = d;
  177. break;
  178. case "license":
  179. currImg.License = int.Parse(t.Value);
  180. break;
  181. case "latitude":
  182. d = -1;
  183. b = Double.TryParse(t.Value, out d);
  184. if (!b)
  185. {
  186. b = Double.TryParse(t.Value.Replace(".", ","), out d);
  187. }
  188. currImg.Latitude = d;
  189. break;
  190. case "id":
  191. currImg.ID = long.Parse(t.Value);
  192. break;
  193. case "description":
  194. currImg.Description = t.Value;
  195. break;
  196. case "date_taken":
  197. currImg.SetTimeFromString(t.Value);
  198. break;
  199. default:
  200. break;
  201. }
  202. }
  203. //parse face
  204. {
  205. var faceLine = getLineForImageFromCsv (faceCSV, currImg.ID);
  206. currImg.ImgFace = double.Parse (faceLine [1]);
  207. }
  208. //parse CN
  209. {
  210. var cnLine = getLineForImageFromCsv (cnCSV, currImg.ID);
  211. double[] dCn = new double[11];
  212. for (int i=0; i<11; i++) {
  213. dCn [i] = double.Parse (cnLine [1 + i]);
  214. }
  215. currImg.ImgCN = dCn;
  216. }
  217. //parse CM
  218. {
  219. var cmLine = getLineForImageFromCsv (cmCSV, currImg.ID);
  220. double[] dCm = new double[9];
  221. for (int i=0; i<9; i++) {
  222. dCm [i] = double.Parse (cmLine [1 + i]);
  223. }
  224. currImg.ImgCM = dCm;
  225. }
  226. results.Add(currImg);
  227. }
  228. location.Images = results;
  229. }
  230. return Location;
  231. }
  232. static public void ProcessLocations(List<OneLocation> locations)
  233. {
  234. // Process the locations one by one
  235. foreach (var loc in locations)
  236. {
  237. var res=RunAlgorithm (loc);
  238. if (res.Count > 0) {
  239. WriteRunningResultToFile (res, loc.Title, loc.Num);
  240. }
  241. }
  242. }
  243. //Keep in mind that values are accessed by the column number, and then the row number like in a regular matrix.
  244. // Baszod. Ideszarsz egy google találatot arról, hogy kell csv-t betölteni, ez gecire nem az,
  245. // mint megírni az adatok betöltését.... Faszom.... (by Zsombor)
  246. static public List<string[]> ReadCSVFileIntoArray(string filePath,string splitcharacter=",")
  247. {
  248. string CSVFileContents = "";
  249. using (StreamReader oStreamReader = new StreamReader(File.OpenRead(filePath)))
  250. {
  251. CSVFileContents = oStreamReader.ReadToEnd();
  252. }
  253. List<string[]> CSVList = new List<string[]>();
  254. string[] sFileLines = CSVFileContents.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  255. foreach (string sFileLine in sFileLines)
  256. {
  257. CSVList.Add(sFileLine.Split(splitcharacter.ToCharArray(), StringSplitOptions.RemoveEmptyEntries));
  258. }
  259. return CSVList;
  260. }
  261. }
  262. }