Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

FindAirportByCodeAction.cs 2.7 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  1. namespace LuisActions.Samples
  2. {
  3. using System;
  4. using System.ComponentModel.DataAnnotations;
  5. using System.IO;
  6. using System.Reflection;
  7. using System.Threading.Tasks;
  8. using System.Xml.Linq;
  9. using System.Xml.XPath;
  10. using Ionic.Zip;
  11. using Microsoft.Cognitive.LUIS.ActionBinding;
  12. using Models;
  13. [Serializable]
  14. [LuisActionBinding("FindAirportByCode", FriendlyName = "Find Airport by Code")]
  15. public class FindAirportByCodeAction : BaseLuisAction
  16. {
  17. [Required(ErrorMessage = "Please provide airport CODE")]
  18. [Location(ErrorMessage = "Please provide a valid CODE")]
  19. public string Code { get; set; }
  20. public override Task<object> FulfillAsync()
  21. {
  22. using (var stream = UnZipCatalog())
  23. {
  24. var result = default(AirportInfo);
  25. var msg = "Airport CODE not found, please try another query!";
  26. var geoCatalog = XElement.Load(stream);
  27. var airport = geoCatalog.XPathSelectElement($"//Airport[@Id=\"{this.Code.ToUpperInvariant()}\"]");
  28. if (airport != null)
  29. {
  30. var city = airport.Parent.Parent;
  31. var country = city.Parent.Parent;
  32. result = new AirportInfo
  33. {
  34. City = city.Attribute("Name").Value,
  35. Code = this.Code.ToUpperInvariant(),
  36. Country = country.Attribute("Name").Value,
  37. Location = airport.Attribute("Location").Value,
  38. Name = airport.Attribute("Name").Value
  39. };
  40. msg = $"{this.Code.ToUpperInvariant()} corresponds to \"{airport.Attribute("Name").Value}\" which is located in {city.Attribute("Name").Value}, {country.Attribute("Name").Value} [{airport.Attribute("Location").Value}]";
  41. }
  42. return Task.FromResult((object)result);
  43. }
  44. }
  45. private static MemoryStream UnZipCatalog()
  46. {
  47. // embedded resource got from http://partners.api.skyscanner.net//apiservices//geo//v1.0?apikey=prtl6749387986743898559646983194
  48. var assembly = Assembly.GetExecutingAssembly();
  49. var resourceName = "LuisActions.Samples.Assets.airportCatalog.zip";
  50. using (Stream stream = assembly.GetManifestResourceStream(resourceName))
  51. {
  52. MemoryStream data = new MemoryStream();
  53. using (ZipFile zip = ZipFile.Read(stream))
  54. {
  55. zip["airportCatalog.xml"].Extract(data);
  56. }
  57. data.Seek(0, SeekOrigin.Begin);
  58. return data;
  59. }
  60. }
  61. }
  62. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...