-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringParser.swift
117 lines (82 loc) · 3.49 KB
/
StringParser.swift
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//
// EEStringParser.swift
// galexy
//
// Created by Tony Thomas on 02/05/16.
// Copyright © 2016 Tony Thomas. All rights reserved.
//
import Foundation
/**
Enumeration for different regular expressions, that can appear in input
*/
enum InputTypePatten : String{
//glob is I
case TypeMap = "^*([a-z]+) *is *([I|V|X|L|C|D|M])$*"
//glob glob Silver is 34 Credits
case TypeCredit = "((?:[a-z]*+ )+)([A-Z]\\w+) *is *(\\d+) *([A-Z]\\w+)$*"
//how much is pish tegj glob glob ?
case TypeCountMuch = "^*how much is ((?:\\w+[^0-9] )+)\\?$*"
//how many Credits is glob prok Iron ?
case TypeCountMany = "^*how many *([a-zA-Z]\\w+) *is *((?:\\w*+ )+)([A-Z]\\w+) \\?$*"
}
/**
String parser class which have type methods to parse test input
*/
class StringParser{
/*
Get the equivalent roman number.This conversion checks the conditions
and throws an error if anything fail
- input : input string can be anly thing like tegj is L
- returns : an optional tuple with the type of query and the query
*/
class func parseInput(input : String)->(InputTypePatten, String)?{
let inputPatterns = [InputTypePatten.TypeMap , InputTypePatten.TypeCredit, InputTypePatten.TypeCountMuch, InputTypePatten.TypeCountMany]
//Check the input string against each pattern in the 'InputType' and returns the first find
for inputPattern in inputPatterns{
let extractedInput = findInText(input, pattenOfInterest: inputPattern.rawValue)?.first
if extractedInput != nil{
return (inputPattern, extractedInput!)
}
}
Log.print("Unresolved input : \(input)")
return nil;
}
/*
The regular expression checker
- text : input string can be anly thing like tegj is L
- pattern : regular expression pattern to find
*/
class func findInText(text :String, pattenOfInterest pattern:String) ->([String]?){
do{
let regex = try NSRegularExpression(pattern: pattern, options: [.CaseInsensitive])
let matches = regex.matchesInString(text, options: [], range: NSMakeRange(0, text.characters.count))
guard matches.count > 0 else{
return nil
}
let nsString = text as NSString
let itemList = matches.map { nsString.substringWithRange($0.range)}
return itemList
}
catch{
Log.print("Error initialising regular expression")
}
return nil
}
/*
Checks and remove some patterns from a text
- text : input string can be anly thing like tegj is L
- pattern : regular expression pattern to remove
*/
class func removePatternFromText(text :String, pattenOfInterest pattern:String) ->String?{
do{
let regex = try NSRegularExpression(pattern: pattern, options: [.CaseInsensitive])
let modString = regex.stringByReplacingMatchesInString(text, options: [], range:
NSMakeRange(0, text.characters.count), withTemplate: "")
return modString
}
catch{
Log.print("Error initialising regular expression")
}
return nil
}
}