Skip to content

Commit 3c9f236

Browse files
committed
emailnameparser: service behaves as before in version 4.0, also for version 4.1. there is a new emailnameparser2 with a slightly different return value. it only exists in 4.1. the new service was added to not break the existing api. users are encouraged to switch, the older one is set deprecated.
1 parent 2b3aa7b commit 3c9f236

20 files changed

+1159
-8
lines changed

src/main/java/org/nameapi/client/services/email/emailnameparser/EmailAddressParsingResultType.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@
77
public enum EmailAddressParsingResultType {
88

99
/**
10-
* The email address belongs to a department (eg [email protected]) or is
11-
* technical (eg [email protected]).
10+
* The email address belongs to a department, such as [email protected].
1211
*/
13-
FUNCTIONAL,
12+
DEPARTMENT,
13+
14+
/**
15+
* It is a technical email address for the domain, such as [email protected].
16+
*
17+
*/
18+
TECHNICAL,
1419

1520
/**
1621
* The email address contains a person's initials such as [email protected].

src/main/java/org/nameapi/client/services/email/emailnameparser/EmailNameParserCommand.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@
3131
* </p>
3232
*
3333
* <p>Names are not formatted to correct case, they are left the way they appeared in the input.</p>
34+
*
35+
* @deprecated use the EmailNameParser2Command
3436
*/
37+
@Deprecated
3538
public class EmailNameParserCommand
3639
extends NameApiBaseCommand<SoapEmailNameParser, String, EmailNameParserResult>
3740
{

src/main/java/org/nameapi/client/services/email/emailnameparser/EmailNameParserMatchImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
import java.util.List;
88

99
/**
10+
* Visible only because emailnameparser2 needs to instantiate it. Don't use this, use the interface!
1011
*/
1112
@Immutable
12-
final class EmailNameParserMatchImpl implements EmailNameParserMatch {
13+
public final class EmailNameParserMatchImpl implements EmailNameParserMatch {
1314

1415
@NotNull
1516
private final List<NameFromEmailAddress> givenNames;
@@ -18,7 +19,7 @@ final class EmailNameParserMatchImpl implements EmailNameParserMatch {
1819

1920
private final double confidence;
2021

21-
EmailNameParserMatchImpl(@NotNull List<NameFromEmailAddress> givenNames,
22+
public EmailNameParserMatchImpl(@NotNull List<NameFromEmailAddress> givenNames,
2223
@NotNull List<NameFromEmailAddress> surnames,
2324
double confidence) {
2425
this.givenNames = Collections.unmodifiableList(givenNames);

src/main/java/org/nameapi/client/services/email/emailnameparser/NameFromEmailAddressImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@
55
import org.jetbrains.annotations.NotNull;
66

77
/**
8+
* Visible only because emailnameparser2 needs to instantiate it. Don't use this, use the interface!
89
*/
910
@Immutable
10-
class NameFromEmailAddressImpl implements NameFromEmailAddress {
11+
public class NameFromEmailAddressImpl implements NameFromEmailAddress {
1112

1213
@NotNull
1314
private String name;
1415
@NotNull
1516
private EmailAddressNameType nameType;
1617

17-
NameFromEmailAddressImpl(@NotNull String name, @NotNull EmailAddressNameType nameType) {
18+
public NameFromEmailAddressImpl(@NotNull String name, @NotNull EmailAddressNameType nameType) {
1819
this.name = name;
1920
this.nameType = nameType;
2021
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
package org.nameapi.client.services.email.emailnameparser2;
3+
4+
/**
5+
* Belongs to {@link org.nameapi.client.services.email.emailnameparser2.EmailNameParserResult}, tells what was detected.
6+
*
7+
* Difference to the EmailAddressParsingResultType:
8+
* The enum uses FUNCTIONAL instead of DEPARTMENT and TECHNICAL because the two can hardly be told apart.
9+
*/
10+
public enum EmailAddressParsingResultType2 {
11+
12+
/**
13+
* The email address belongs to a department (eg [email protected]) or is
14+
* technical (eg [email protected]).
15+
*/
16+
FUNCTIONAL,
17+
18+
/**
19+
* The email address contains a person's initials such as [email protected].
20+
* <p>Note that this answer is a guess, the 2 letters could also have another meaning
21+
* such as a short given name or surname, or something completely different.</p>
22+
*/
23+
INITIALS,
24+
25+
/**
26+
* The email address contains a person's name such as [email protected].
27+
*/
28+
PERSON_NAME,
29+
30+
/**
31+
* There is no name in the address, for example [email protected]
32+
* <p>The address may be personal or non-personal, can't say (as in UNKNOWN)
33+
* but it is clear that no name can be found in it.</p>
34+
*/
35+
NOT_A_NAME,
36+
37+
/**
38+
* The email address could not be classified and hence the service failed to extract a name.
39+
*/
40+
UNKNOWN;
41+
42+
public boolean isUnknown() {
43+
return this == UNKNOWN;
44+
}
45+
46+
47+
/**
48+
* Developer: Call this before doing a switch on an enum value.
49+
*/
50+
public static void assertSize(int size) {
51+
assert values().length == size : "Update the code calling this with "+size+"!";
52+
}
53+
54+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package org.nameapi.client.services.email.emailnameparser2;
2+
3+
import com.google.common.base.Optional;
4+
import com.optimaize.command4j.ExecutionContext;
5+
import org.jetbrains.annotations.NotNull;
6+
import org.nameapi.client.services.NameApiBaseCommand;
7+
import org.nameapi.client.services.email.emailnameparser.EmailNameParserMatch;
8+
import org.nameapi.client.services.email.emailnameparser.EmailNameParserMatchImpl;
9+
import org.nameapi.client.services.email.emailnameparser.NameFromEmailAddress;
10+
import org.nameapi.client.services.email.emailnameparser.NameFromEmailAddressImpl;
11+
import org.nameapi.client.services.email.emailnameparser2.wsdl.*;
12+
13+
import java.net.URL;
14+
import java.util.ArrayList;
15+
import java.util.List;
16+
import java.util.concurrent.Callable;
17+
18+
/**
19+
* Analyzes the email address and attempts to extract a person's name.
20+
*
21+
* <p>Example: [email protected] => [john, doe]</p>
22+
*
23+
* <p>The service attempts to detect several kinds of syntax, such as:
24+
* <ul>
25+
* <li>[email protected]</li>
26+
* <li>[email protected]</li>
27+
* <li>[email protected]</li>
28+
* <li>[email protected]</li>
29+
* <li>[email protected]</li>
30+
* <li>[email protected]</li>
31+
* <li>[email protected]</li>
32+
* <li>[email protected]</li>
33+
* <li>[email protected]</li>
34+
* </ul>
35+
* </p>
36+
*
37+
* <p>Names are not formatted to correct case, they are left the way they appeared in the input.</p>
38+
*
39+
* <p>Difference to the older EmailNameParserCommand:
40+
* The enum EmailAddressParsingResultType2 uses FUNCTIONAL instead of DEPARTMENT and TECHNICAL because the two can hardly be told apart.
41+
* </p>
42+
*/
43+
public class EmailNameParser2Command
44+
extends NameApiBaseCommand<SoapEmailNameParser2, String, EmailNameParserResult>
45+
{
46+
47+
private static final String servicePath = "/email/emailnameparser2";
48+
49+
public EmailNameParser2Command() {
50+
super(SoapEmailNameParser2.class);
51+
}
52+
53+
@Override @NotNull
54+
public EmailNameParserResult call(@NotNull Optional<String> arg, @NotNull ExecutionContext ec) throws Exception {
55+
SoapEmailNameParserResult2 soapResult = getPort(ec).parse(getContext(ec), arg.get());
56+
return convert(soapResult);
57+
}
58+
59+
@NotNull
60+
private EmailNameParserResult convert(@NotNull SoapEmailNameParserResult2 soapResult) {
61+
List<EmailNameParserMatch> matches = new ArrayList<>();
62+
for (SoapEmailNameParserMatch soapMatch : soapResult.getMatches()) {
63+
matches.add( convert(soapMatch) );
64+
}
65+
return new EmailNameParserResultImpl(soapResult.getResultType(), matches);
66+
}
67+
@NotNull
68+
private EmailNameParserMatch convert(@NotNull SoapEmailNameParserMatch soapMatch) {
69+
return new EmailNameParserMatchImpl(
70+
convert(soapMatch.getGivenNames()),
71+
convert(soapMatch.getSurnames()),
72+
soapMatch.getConfidence()
73+
);
74+
}
75+
@NotNull
76+
private List<NameFromEmailAddress> convert(@NotNull List<SoapNameFromEmailAddress> list) {
77+
List<NameFromEmailAddress> ret = new ArrayList<>();
78+
for (SoapNameFromEmailAddress soapName : list) {
79+
ret.add(convert(soapName));
80+
}
81+
return ret;
82+
}
83+
@NotNull
84+
private NameFromEmailAddress convert(@NotNull SoapNameFromEmailAddress soapName) {
85+
return new NameFromEmailAddressImpl(soapName.getName(), soapName.getNameType());
86+
}
87+
88+
@NotNull @Override
89+
protected Callable<SoapEmailNameParser2> createPort(@NotNull final ExecutionContext ec) {
90+
return new Callable<SoapEmailNameParser2>() {
91+
@Override
92+
public SoapEmailNameParser2 call() throws Exception {
93+
URL url = makeUrl(ec, servicePath);
94+
return new SoapEmailNameParser2Service(url).getSoapEmailNameParser2Port();
95+
}
96+
};
97+
}
98+
99+
}
100+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.nameapi.client.services.email.emailnameparser2;
2+
3+
import com.google.common.base.Optional;
4+
import org.jetbrains.annotations.NotNull;
5+
import org.nameapi.client.services.email.emailnameparser.EmailAddressParsingResultType;
6+
import org.nameapi.client.services.email.emailnameparser.EmailNameParserMatch;
7+
8+
import java.util.List;
9+
10+
/**
11+
*/
12+
public interface EmailNameParserResult {
13+
14+
@NotNull
15+
EmailAddressParsingResultType2 getResultType();
16+
17+
@NotNull
18+
Optional<EmailNameParserMatch> getBestNameMatch();
19+
20+
/**
21+
* The first entry (if any) is the best, they are ordered by likeliness in descending order.
22+
*/
23+
@NotNull
24+
List<EmailNameParserMatch> getNameMatches();
25+
26+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.nameapi.client.services.email.emailnameparser2;
2+
3+
import com.google.common.base.Optional;
4+
import crema.annotation.Immutable;
5+
import org.jetbrains.annotations.NotNull;
6+
import org.nameapi.client.services.email.emailnameparser.EmailAddressParsingResultType;
7+
import org.nameapi.client.services.email.emailnameparser.EmailNameParserMatch;
8+
9+
import java.util.Collections;
10+
import java.util.List;
11+
12+
/**
13+
*/
14+
@Immutable
15+
class EmailNameParserResultImpl implements EmailNameParserResult {
16+
17+
@NotNull
18+
private final EmailAddressParsingResultType2 resultType;
19+
@NotNull
20+
private final List<EmailNameParserMatch> matches;
21+
22+
EmailNameParserResultImpl(@NotNull EmailAddressParsingResultType2 resultType, @NotNull List<EmailNameParserMatch> matches) {
23+
this.resultType = resultType;
24+
this.matches = Collections.unmodifiableList(matches);
25+
}
26+
27+
28+
@Override @NotNull
29+
public EmailAddressParsingResultType2 getResultType() {
30+
return resultType;
31+
}
32+
33+
@NotNull @Override
34+
public Optional<EmailNameParserMatch> getBestNameMatch() {
35+
if (matches.isEmpty()) return Optional.absent();
36+
return Optional.of(matches.get(0));
37+
}
38+
39+
@NotNull @Override
40+
public List<EmailNameParserMatch> getNameMatches() {
41+
return matches;
42+
}
43+
44+
@Override @NotNull
45+
public String toString() {
46+
return getResultType() + ": "+matches;
47+
}
48+
49+
}

0 commit comments

Comments
 (0)