-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBCAuthorAffiliations.m
273 lines (173 loc) · 6.08 KB
/
BCAuthorAffiliations.m
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
//
// BCAuthorAffiliations.m
//
//
// Created by Tom Houpt on 15/10/13.
//
//
#import "BCAuthorAffiliations.h"
#import "BCAuthor.h"
@interface AffNode : NSObject
@property (copy) NSString *token;
@property AffNode *parent;
@property NSMutableArray *children;
@property NSMutableArray *authors;
@property NSInteger leafNumber;
-(NSInteger)numberOfLeaves;
-(NSString *)stringWithFootNotes:(BOOL)showFootNotes;
-(NSInteger)footnoteForAuthor:(BCAuthor *)author;
@end
@interface BCAuthorAffiliations (private)
-(void)constructTree;
@end
//---------------------------------------------------------------------
//---------------------------------------------------------------------
@implementation AffNode
-(id)init; {
self = [super init];
if (self) {
self.children = [NSMutableArray array];
self.authors = [NSMutableArray array];
self.token = nil;
self.parent = nil;
self.leafNumber = -1;
}
return self;
}
-(NSInteger)numberOfLeaves; {
if (0 == [self.children count]) { return 1; }
NSInteger sumLeaves = 0;
for (AffNode *child in self.children) {
sumLeaves+= [child numberOfLeaves];
}
return sumLeaves;
}
-(NSString *)stringWithFootNotes:(BOOL)showFootNotes; {
NSMutableString *s = [NSMutableString string];
if (nil != self.token) {
if (showFootNotes) {
if (self.leafNumber != -1) {
if (self.leafNumber > 1 ) {
[s appendString:@" and "];
}
[s appendString:[NSString stringWithFormat:@"^%ld^", self.leafNumber]];
}
}
[s appendString:self.token];
[s appendString:@","];
}
for (NSInteger i = [self.children count] - 1; i >= 0; i--) {
// if (1 < [self.children count]
// && ([self.children count] - 1) == i
// && self.parent.parent == nil) {
// [s appendString:@"and "];
// }
[s appendString:[[self.children objectAtIndex:i] stringWithFootNotes:showFootNotes]];
}
return s;
}
-(NSInteger)footnoteForAuthor:(BCAuthor *)author; {
if ([self.authors containsObject:author]) {
return self.leafNumber;
}
for (AffNode *child in self.children) {
NSInteger authorleaf = [child footnoteForAuthor:author];
if (-1 != authorleaf) { return authorleaf; }
}
return -1;
}
@end
//---------------------------------------------------------------------
//---------------------------------------------------------------------
@implementation BCAuthorAffiliations
-(id)initWithAuthors:(NSArray *)a; {
self = [super init];
if (self) {
self.authors = [NSArray arrayWithArray:a];
}
return self;
}
-(void)constructTree; {
self.affroot = [[AffNode alloc] init];
self.currentLeafNumber = 1;
for (BCAuthor *author in self.authors) {
NSArray *tokens = [[author affiliation] componentsSeparatedByString:@","];
AffNode *currentNode = self.affroot;
for (NSString *token in [tokens reverseObjectEnumerator]) {
BOOL hasToken = NO;
for (AffNode *child in [currentNode children]) {
if ([child.token isEqualToString:token]){
hasToken = YES;
currentNode = child;
break;
}
}
if (!hasToken) {
AffNode *newNode = [[AffNode alloc] init];
newNode.token = token;
newNode.parent = currentNode;
[currentNode.children addObject:newNode];
currentNode = newNode;
}
}
[currentNode.authors addObject:author];
if (1 == [currentNode.authors count]) {
currentNode.leafNumber = self.currentLeafNumber;
self.currentLeafNumber++;
}
}
}
-(NSString *)affiliationsString; {
if (nil == self.affroot) { [self constructTree]; }
NSString *backwardsString;
if ([self.affroot numberOfLeaves] > 1) {
backwardsString = [self.affroot stringWithFootNotes:YES];
}
else {
backwardsString = [self.affroot stringWithFootNotes:NO];
}
NSArray *backwardsTokenArray = [backwardsString componentsSeparatedByString:@","];
NSMutableArray *tokenArray = [NSMutableArray array];
for (NSString *token in [backwardsTokenArray reverseObjectEnumerator]) {
if (0 < [token length]){ [tokenArray addObject:token]; }
}
NSMutableString *affString = [NSMutableString string];
for (NSInteger i = 0; i < [tokenArray count]; i++) {
if (0 != i) {
if (![[tokenArray objectAtIndex:i] hasPrefix:@"and"]) {
[affString appendString:@","];
}
[affString appendString:@" "];
}
[affString appendString:[tokenArray objectAtIndex:i]];
}
return affString;
}
-(NSString *)authorsWithFootNotes; {
if (nil == self.affroot) { [self constructTree]; }
NSMutableString *authorString = [NSMutableString string];
for (NSInteger i = 0; i < [self.authors count]; i++) {
BCAuthor *author = [self.authors objectAtIndex:i];
if (i != 0 ){
if ( i == [self.authors count] - 1) {
[authorString appendString:@" and "];
}
else {
[authorString appendString:@", "];
}
}
NSInteger footnote = [self footnoteForAuthor:author];
if (-1 == footnote) {
[authorString appendString:[author fullName]];
}
else {
[authorString appendString:[NSString stringWithFormat:@"%@^%ld^",[author fullName],footnote]];
}
}
return authorString;
}
-(NSInteger)footnoteForAuthor:(BCAuthor *)author; {
if ([self.affroot numberOfLeaves] <= 1) { return -1; }
return [self.affroot footnoteForAuthor:author];
}
@end