123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- package examples.extractor;
- import examples.utils.SimilarityUtils;
- import java.time.Instant;
- import java.time.LocalDate;
- import java.time.LocalDateTime;
- import java.time.ZoneId;
- import java.time.format.DateTimeFormatter;
- import java.time.temporal.ChronoUnit;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- public class ExtractorUtils {
- public static Double division(String s1, String s2, Map<String, String> maps) {
- double rate = 0.0;
- if (maps.containsKey(s1) && maps.containsKey(s2)) {
- Double d1 = Double.valueOf(maps.get(s1));
- if (isDoubleEqualToZero(d1)) {
- return rate;
- }
- Double d2 = Double.valueOf(maps.get(s2));
- rate = d2 / d1;
- }
- return rate;
- }
- public static Double divisionDouble(Double d1, Double d2) {
- double rate = 0.0;
- if (isDoubleEqualToZero(d1)) {
- return rate;
- }
- rate = d2 / d1;
- return rate;
- }
- public static boolean isDoubleEqualToZero(double value) {
- final double epsilon = 1e-10;
-
- return Math.abs(value) < epsilon;
- }
- public static double calculateVariance(List<Double> numbers) {
- double average = numbers.stream()
- .mapToDouble(Double::doubleValue)
- .average()
- .orElse(0.0);
- double squaredDiffSum = numbers.stream()
- .mapToDouble(Double::doubleValue)
- .map(x -> Math.pow(x - average, 2))
- .average()
- .orElse(0.0);
- return squaredDiffSum;
- }
- public static double calculateAverage(List<Double> numbers) {
- if (numbers == null || numbers.isEmpty()) {
- return 0.0;
- }
- return numbers.stream()
- .mapToDouble(Number::doubleValue)
- .average()
- .orElse(0.0);
- }
- public static List<Double> calculateDifferences(List<Double> numbers) {
- List<Double> differences = new ArrayList<>();
- for (int i = 0; i < numbers.size() - 1; i++) {
- Double diff = 0.0;
- if (!isDoubleEqualToZero(numbers.get(i))) {
- diff = (numbers.get(i + 1) - numbers.get(i)) / numbers.get(i);
- }
- differences.add(diff);
- }
- return differences;
- }
- public static List<String> generateHourStrings(String timeString, int N) {
- LocalDateTime dateTime = LocalDateTime.parse(timeString, DateTimeFormatter.ofPattern("yyyyMMddHH"));
- List<String> hourStrings = new ArrayList<>();
- for (int i = 0; i < N; i++) {
- hourStrings.add(dateTime.minusHours(i).format(DateTimeFormatter.ofPattern("yyyyMMddHH")));
- }
- return hourStrings;
- }
- public static String subtractHours(String inputDateTime, int hoursToSubtract) {
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHH");
- LocalDateTime dateTime = LocalDateTime.parse(inputDateTime, formatter);
- LocalDateTime subtractedDateTime = dateTime.minusHours(hoursToSubtract);
- return subtractedDateTime.format(formatter);
- }
-
- public static Integer ceilLogRate(Double key) {
- double bucket = Math.ceil(
- Math.pow(key, 0.2) * 100
- );
- if (bucket > 300) {
- bucket = 300;
- }
- if (bucket < 0) {
- bucket = 0;
- }
- return (int) bucket;
- }
-
- public static int bucketCnt(Double key) {
- long bucket = Math.round(Math.log((key * 10 + 1.0)) * 10);
- if (bucket > 300) {
- bucket = 300;
- }
- if (bucket < 0) {
- bucket = 0;
- }
- return (int) bucket;
- }
- public static int findInsertPosition(double[] sortedArray, double target) {
- int low = 0;
- int high = sortedArray.length - 1;
- while (low <= high) {
- int mid = low + (high - low) / 2;
- double midValue = sortedArray[mid];
- if (midValue < target) {
- low = mid + 1;
- } else if (midValue > target) {
- high = mid - 1;
- } else {
-
- while (mid < sortedArray.length - 1 && sortedArray[mid + 1] == target) {
- mid++;
- }
- return mid + 1;
- }
- }
- return low;
- }
- public static Double[] funcC34567ForTagsNew(String tags, String title) {
- String[] tagsList = tags.split(",");
- int d1 = 0;
- List<String> d2 = new ArrayList<>();
- double d3 = 0.0;
- double d4 = 0.0;
- for (String tag : tagsList) {
- if (title.contains(tag)) {
- d1++;
- d2.add(tag);
- }
- double score = SimilarityUtils.word2VecSimilarity(tag, title);
- if (score > d3) {
- d3 = score;
- }
- d4 += score;
- }
- d4 = (tagsList.length > 0) ? d4 / tagsList.length : d4;
-
- Double[] result = {(double) d1, d3, d4};
- return result;
- }
- public static double reciprocal(double num) {
- if (num == 0) {
- return 0;
- }
- return 1.0 / (num + 1);
- }
- public static long getDaysBetween(long timestamp1, long timestamp2) {
- if (timestamp1 == 0 || timestamp2 == 0) {
- return 0;
- }
- Instant instant1 = Instant.ofEpochSecond(timestamp1);
- Instant instant2 = Instant.ofEpochSecond(timestamp2);
- LocalDate date1 = instant1.atZone(ZoneId.systemDefault()).toLocalDate();
- LocalDate date2 = instant2.atZone(ZoneId.systemDefault()).toLocalDate();
- return ChronoUnit.DAYS.between(date1, date2);
- }
- public static int getHourByTimestamp(long timestamp) {
- return LocalDateTime
- .ofInstant(Instant.ofEpochSecond(timestamp), ZoneId.systemDefault())
- .getHour() + 1;
- }
- public static int getDayOfWeekByTimestamp(long timestamp) {
- return LocalDateTime
- .ofInstant(Instant.ofEpochSecond(timestamp), ZoneId.systemDefault())
- .getDayOfWeek()
- .getValue();
- }
- public static void main(String[] args) {
- double[] sortedArray = {1.0, 2.0, 4.0, 4.0, 6.0};
- double target = 0.0;
- System.out.println(findInsertPosition(sortedArray, target));
- }
- }
|