Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package apoc.text;
import static apoc.util.Util.quote;
import static java.lang.Math.toIntExact;
import static java.util.Arrays.asList;
import apoc.util.Util;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.Normalizer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.text.similarity.HammingDistance;
import org.apache.commons.text.similarity.JaroWinklerDistance;
import org.apache.commons.text.similarity.LevenshteinDistance;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.Transaction;
import org.neo4j.kernel.api.QueryLanguage;
import org.neo4j.kernel.api.procedure.QueryLanguageScope;
import org.neo4j.procedure.Context;
import org.neo4j.procedure.Description;
import org.neo4j.procedure.Name;
import org.neo4j.procedure.UserFunction;
/**
* @author mh
* @since 05.05.16
*/
public class Strings {
private static final HammingDistance hammingDistance = new HammingDistance();
private static final JaroWinklerDistance jaroWinklerDistance = new JaroWinklerDistance();
private static final LevenshteinDistance levenshteinDistance = new LevenshteinDistance();
@Context
public Transaction tx;
@UserFunction("apoc.text.indexOf")
@Description("Returns the first occurrence of the lookup `STRING` in the given `STRING`, or -1 if not found.")
public Long indexOf(
final @Name(value = "text", description = "The string to search for the lookup string in.") String text,
final @Name(value = "lookup", description = "The lookup string to search for in the given string.") String
lookup,
final @Name(value = "from", defaultValue = "0", description = "The index at which to start the search.")
long from,
@Name(value = "to", defaultValue = "-1", description = "The index at which to stop the search.") long to) {
if (text == null) return null;
if (lookup == null) return -1L;
if (to == -1L || to > text.length()) return (long) text.indexOf(lookup, (int) from);
if (to <= from) return -1L;
return (long) text.substring(0, (int) to).indexOf(lookup, (int) from);
}
@UserFunction("apoc.text.indexesOf")
@Description("Returns all occurrences of the lookup `STRING` in the given `STRING`, or an empty list if not found.")
public List indexesOf(
final @Name(value = "text", description = "The string to search for the lookup string in.") String text,
final @Name(value = "lookup", description = "The lookup string to search for in the given string.") String
lookup,
final @Name(value = "from", defaultValue = "0", description = "The index at which to start the search.")
long from,
@Name(value = "to", defaultValue = "-1", description = "The index at which to stop the search.") long to) {
if (text == null) return null;
if (lookup == null) return Collections.emptyList();
if (to == -1L) to = text.length();
List result = new ArrayList<>();
int idx = (int) from - 1;
while (true) {
idx = text.indexOf(lookup, idx + 1);
if (idx == -1 || idx >= to) {
return result;
} else {
result.add((long) idx);
}
}
}
@UserFunction("apoc.text.replace")
@Description("Finds and replaces all matches found by the given regular expression with the given replacement.")
public String replace(
final @Name(value = "text", description = "The string to be modified.") String text,
final @Name(
value = "regex",
description = "The regular expression pattern to replace in the original string.") String
regex,
final @Name(value = "replacement", description = "The value to be inserted in the original string.") String
replacement) {
return regreplace(text, regex, replacement);
}
@UserFunction("apoc.text.byteCount")
@Description("Returns the size of the given `STRING` in bytes.")
public long byteCount(
final @Name(value = "text", description = "The string to get the size of in bytes.") String text,
@Name(value = "charset", defaultValue = "UTF-8", description = "The name of a supported charset.")
String charset)
throws UnsupportedEncodingException {
return text.getBytes(charset).length;
}
@UserFunction("apoc.text.bytes")
@Description("Returns the given `STRING` as bytes.")
public List bytes(
final @Name(value = "text", description = "The string to get the bytes from.") String text,
@Name(value = "charset", defaultValue = "UTF-8", description = "The name of a supported charset.")
String charset)
throws UnsupportedEncodingException {
byte[] bytes = text.getBytes(charset);
List result = new ArrayList<>(bytes.length);
for (byte b : bytes) {
result.add((long) b & 0xFFL);
}
return result;
}
@UserFunction(name = "apoc.text.regreplace", deprecatedBy = "apoc.text.replace")
@Deprecated
@QueryLanguageScope(scope = {QueryLanguage.CYPHER_5})
@Description("Finds and replaces all matches found by the given regular expression with the given replacement.")
public String regreplace(
final @Name(value = "text", description = "The string to be modified.") String text,
final @Name(
value = "regex",
description = "The regular expression pattern to replace in the original string.") String
regex,
final @Name(value = "replacement", description = "The value to be inserted in the original string.") String
replacement) {
if (text == null || regex == null || replacement == null) {
return null;
}
return text.replaceAll(regex, replacement);
}
@UserFunction("apoc.text.split")
@Description("Splits the given `STRING` using a given regular expression as a separator.")
public List split(
final @Name(value = "text", description = "The string to split.") String text,
final @Name(value = "regex", description = "The delimiting regular expression.") String regex,
final @Name(
value = "limit",
defaultValue = "0",
description =
"The number of times the regex pattern is applied; if set to 0, it will be applied as many times as possible.")
Long limit) {
if (text == null || regex == null || limit == null) {
return null;
}
String[] resultArray = text.split(regex, limit.intValue());
return new ArrayList<>(asList(resultArray));
}
@UserFunction("apoc.text.regexGroups")
@Description("Returns all groups matching the given regular expression in the given text.")
public List> regexGroups(
final @Name(value = "text", description = "The text to extract matches from.") String text,
final @Name(value = "regex", description = "The regex pattern to match.") String regex) {
if (text == null || regex == null) {
return Collections.EMPTY_LIST;
} else {
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(text);
List> result = new ArrayList<>();
while (matcher.find()) {
List matchResult = new ArrayList<>();
for (int i = 0; i <= matcher.groupCount(); i++) {
matchResult.add(matcher.group(i));
}
result.add(matchResult);
}
return result;
}
}
@UserFunction("apoc.text.regexGroupsByName")
@Description("Returns all groups with their group name matching the given regular expression in the given text.")
public List