forked from LeonieWeissweiler/CISTEM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cistem.swift
146 lines (114 loc) · 4.26 KB
/
Cistem.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//
// cistem.swift
// cistem
//
// Created by Hendrik Noeller on 15.09.17.
// Copyright © 2017 de.HendrikNoeller. All rights reserved.
//
import Foundation
extension String {
func index(_ index:Int) -> String.Index{
if (index < 0) {
return self.index(self.endIndex, offsetBy: index)
} else {
return self.index(self.startIndex, offsetBy: index)
}
}
}
struct Cistem {
static let doubleCharacterRegex = try! NSRegularExpression(pattern: "(.)\\1")
static let doubleCharacterReverseRegex = try! NSRegularExpression(pattern: "(.)\\*")
private static func isUppercase(_ word: String) -> Bool {
let first = String(word[word.startIndex])
let firstUpper = first.uppercased()
return first == firstUpper
}
static func stem(_ word: String, caseInsensitive: Bool = false) -> String {
if (word.isEmpty) {
return ""
}
let uppercase = isUppercase(word)
var result = word
result = result.lowercased()
result = result.replacingOccurrences(of: "ü", with: "u")
result = result.replacingOccurrences(of: "ö", with: "o")
result = result.replacingOccurrences(of: "ä", with: "a")
result = result.replacingOccurrences(of: "ß", with: "ss")
if (result.characters.count >= 6 && result.hasPrefix("ge")) {
result = result[result.index(2)..<result.endIndex]
}
result = result.replacingOccurrences(of: "sch", with: "$")
result = result.replacingOccurrences(of: "ei", with: "%")
result = result.replacingOccurrences(of: "ie", with: "&")
result = doubleCharacterRegex.stringByReplacingMatches(in: result, options: [], range: NSMakeRange(0, result.characters.count), withTemplate: "$1*")
while (result.characters.count > 3) {
if (result.characters.count > 5) {
if (result.hasSuffix("em") || result.hasSuffix("er") || result.hasSuffix("nd")){
result = result[result.startIndex..<result.index(-2)]
continue
}
}
if (!uppercase || caseInsensitive) {
if (result.hasSuffix("t")) {
result = result[result.startIndex..<result.index(-1)]
continue
}
}
if (result.hasSuffix("e") || result.hasSuffix("s") || result.hasSuffix("n")) {
result = result[result.startIndex..<result.index(-1)]
continue
}
break
}
result = doubleCharacterReverseRegex.stringByReplacingMatches(in: result, options: [], range: NSMakeRange(0, result.characters.count), withTemplate: "$1$1")
result = result.replacingOccurrences(of: "$", with: "sch")
result = result.replacingOccurrences(of: "%", with: "ei")
result = result.replacingOccurrences(of: "&", with: "ie")
return result
}
static func segment(_ word: String, caseInsensitive: Bool = false) -> [String] {
if (word.isEmpty) {
return ["", ""]
}
let uppercase = isUppercase(word)
var restLength = 0
var result = word
result = result.lowercased()
let original = result
result = result.replacingOccurrences(of: "sch", with: "$")
result = result.replacingOccurrences(of: "ei", with: "%")
result = result.replacingOccurrences(of: "ie", with: "&")
result = doubleCharacterRegex.stringByReplacingMatches(in: result, options: [], range: NSMakeRange(0, result.characters.count), withTemplate: "$1*")
while (result.characters.count > 3) {
if (result.characters.count > 5) {
if (result.hasSuffix("em") || result.hasSuffix("er") || result.hasSuffix("nd")){
restLength += 2
result = result[result.startIndex..<result.index(-2)]
continue
}
}
if (!uppercase || caseInsensitive) {
if (result.hasSuffix("t")) {
restLength += 1
result = result[result.startIndex..<result.index(-1)]
continue
}
}
if (result.hasSuffix("e") || result.hasSuffix("s") || result.hasSuffix("n")) {
restLength += 1
result = result[result.startIndex..<result.index(-1)]
continue
}
break
}
result = doubleCharacterReverseRegex.stringByReplacingMatches(in: result, options: [], range: NSMakeRange(0, result.characters.count), withTemplate: "$1$1")
result = result.replacingOccurrences(of: "$", with: "sch")
result = result.replacingOccurrences(of: "%", with: "ei")
result = result.replacingOccurrences(of: "&", with: "ie")
var rest = ""
if (restLength > 0) {
rest = String(original[original.index(-restLength)..<original.endIndex])
}
return [result, rest]
}
}