<commit_before>package ch.elexis.core.services;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.apache.commons.lang3.StringUtils;
import org.osgi.service.component.annotations.Component;
import org.slf4j.LoggerFactory;

import ch.elexis.core.constants.StringConstants;
import ch.elexis.core.model.IArticle;
import ch.elexis.core.model.IArticleDefaultSignature;
import ch.elexis.core.model.IPatient;
import ch.elexis.core.model.IPrescription;
import ch.elexis.core.model.IRecipe;
import ch.elexis.core.model.Identifiable;
import ch.elexis.core.model.builder.IRecipeBuilder;
import ch.elexis.core.model.prescription.Constants;
import ch.elexis.core.model.prescription.EntryType;
import ch.elexis.core.model.prescription.Methods;
import ch.elexis.core.services.IQuery.COMPARATOR;
import ch.elexis.core.services.holder.ContextServiceHolder;
import ch.elexis.core.services.holder.CoreModelServiceHolder;
import ch.elexis.core.services.holder.StoreToStringServiceHolder;

@Component
public class MedicationService implements IMedicationService {
	
	@Override
	public float getDailyDosageAsFloat(IPrescription prescription){
		float total = 0f;
		List<Float> res = getDosageAsFloats(prescription);
		for (int j = 0; j < res.size(); j++) {
			total += res.get(j);
		}
		return total;
	}
	
	@Override
	public List<Float> getDosageAsFloats(IPrescription prescription){
		ArrayList<Float> list = new ArrayList<>();
		ArrayList<Float> sub_list = new ArrayList<>();
		float num = 0;
		String dosis = prescription.getDosageInstruction();
		if (dosis != null) {
			// Match stuff like '1/2', '7/8', '~1,2'
			// System.out.println(dosis.matches(special_num_at_start));
			if (dosis.matches(special_num_at_start)) {
				list.add(getNum(dosis.replace("~", "")));
			} else if (dosis.matches("[0-9½¼]+([xX][0-9]+(/[0-9]+)?|)")) { //$NON-NLS-1$
				String[] dose = dosis.split("[xX]"); //$NON-NLS-1$
				float count = getNum(dose[0]);
				if (dose.length > 1)
					num = getNum(dose[1]) * count;
				else
					num = getNum(dose[0]);
				list.add(num);
			} else {
				sub_list = getDosageAsFloats(dosis, "-");
				if (StringUtils.countMatches(dosis, "-") > 1 && sub_list.size() > 0) {
					return sub_list;
				}
				sub_list = getDosageAsFloats(dosis, "/");
				if (StringUtils.countMatches(dosis, "/") > 1 && sub_list.size() > 0) {
					return sub_list;
				}
				if (dosis.indexOf('-') != -1 || dosis.indexOf('/') != -1) {
					String[] dos = dosis.split("[- /]"); //$NON-NLS-1$
					if (dos.length > 2) {
						for (String d : dos) {
							boolean hasDigit = d.matches("^[~/.]*[½¼0-9].*");
							if (d.indexOf(' ') != -1)
								list.add(getNum(d.substring(0, d.indexOf(' '))));
							else if (d.length() > 0 && hasDigit)
								list.add(getNum(d));
							if (list.size() >= 4)
								return list;
						}
					} else if (dos.length > 1) {
						list.add(getNum(dos[1]));
					} else {
						// nothing to add
					}
				}
			}
		}
		return list;
	}
	
	private ArrayList<Float> getDosageAsFloats(String dosis, String trennzeichen){
		ArrayList<Float> list = new ArrayList<Float>();
		if (dosis.indexOf('-') != -1 || dosis.indexOf('/') != -1) {
			String[] dos = dosis.split(trennzeichen);
			if (dos.length > 2) {
				for (String d : dos) {
					boolean hasDigit = d.matches("^[~/.]*[½¼0-9].*");
					if (d.indexOf(' ') != -1)
						list.add(getNum(d.substring(0, d.indexOf(' '))));
					else if (d.length() > 0 && hasDigit)
						list.add(getNum(d));
					else if (d.length() == 0)
						list.add(0.0f);
					if (list.size() >= 4)
						return list;
				}
			} else if (dos.length > 1) {
				list.add(getNum(dos[1]));
			} else {
				// nothing to add
			}
		}
		return list;
	}
	
	private static final String special_num_at_start = "^(~|)[0-9]/[0-9][ a-zA-Z]*$";
	
	private float getNum(String num){
		try {
			String n = num.trim();
			if (n.matches(special_num_at_start)) {
				float value = getNum(n.substring(0, 1)) / getNum(n.substring(2));
				return value;
			} else if (n.equalsIgnoreCase("½"))
				return 0.5F;
			else if (n.equalsIgnoreCase("¼"))
				return 0.25F;
			else if (n.equalsIgnoreCase("1½"))
				return 1.5F;
			
			else if (n.indexOf('/') != -1) {<commit_msg>minor changes<commit_after>package ch.elexis.core.services;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.apache.commons.lang3.StringUtils;
import org.osgi.service.component.annotations.Component;
import org.slf4j.LoggerFactory;

import ch.elexis.core.constants.StringConstants;
import ch.elexis.core.model.IArticle;
import ch.elexis.core.model.IArticleDefaultSignature;
import ch.elexis.core.model.IPatient;
import ch.elexis.core.model.IPrescription;
import ch.elexis.core.model.IRecipe;
import ch.elexis.core.model.Identifiable;
import ch.elexis.core.model.builder.IRecipeBuilder;
import ch.elexis.core.model.prescription.Constants;
import ch.elexis.core.model.prescription.EntryType;
import ch.elexis.core.model.prescription.Methods;
import ch.elexis.core.services.IQuery.COMPARATOR;
import ch.elexis.core.services.holder.ContextServiceHolder;
import ch.elexis.core.services.holder.CoreModelServiceHolder;
import ch.elexis.core.services.holder.StoreToStringServiceHolder;

@Component
public class MedicationService implements IMedicationService {
	
	@Override
	public float getDailyDosageAsFloat(IPrescription prescription){
		float total = 0f;
		List<Float> res = getDosageAsFloats(prescription);
		for (int j = 0; j < res.size(); j++) {
			total += res.get(j);
		}
		return total;
	}
	
	@Override
	public List<Float> getDosageAsFloats(IPrescription prescription){
		ArrayList<Float> list = new ArrayList<>();
		ArrayList<Float> sub_list = new ArrayList<>();
		float num = 0;
		String dosis = prescription.getDosageInstruction();
		if (dosis != null) {
			// Match stuff like '1/2', '7/8', '~1,2'
			// System.out.println(dosis.matches(special_num_at_start));
			if (dosis.matches(special_num_at_start)) {
				list.add(getNum(dosis.replace("~", "")));
			} else if (dosis.matches("[0-9½¼]+([xX][0-9]+(/[0-9]+)?|)")) { //$NON-NLS-1$
				String[] dose = dosis.split("[xX]"); //$NON-NLS-1$
				float count = getNum(dose[0]);
				if (dose.length > 1)
					num = getNum(dose[1]) * count;
				else
					num = getNum(dose[0]);
				list.add(num);
			} else {
				sub_list = getDosageAsFloats(dosis, "-");
				if (StringUtils.countMatches(dosis, "-") > 1 && sub_list.size() > 0) {
					return sub_list;
				}
				sub_list = getDosageAsFloats(dosis, "/");
				if (StringUtils.countMatches(dosis, "/") > 1 && sub_list.size() > 0) {
					return sub_list;
				}
				if (dosis.indexOf('-') != -1 || dosis.indexOf('/') != -1) {
					String[] dos = dosis.split("[- /]"); //$NON-NLS-1$
					if (dos.length > 2) {
						for (String d : dos) {
							boolean hasDigit = d.matches("^[~/.]*[½¼0-9].*");
							if (d.indexOf(' ') != -1)
								list.add(getNum(d.substring(0, d.indexOf(' '))));
							else if (d.length() > 0 && hasDigit)
								list.add(getNum(d));
							if (list.size() >= 4)
								return list;
						}
					} else if (dos.length > 1) {
						list.add(getNum(dos[1]));
					} else {
						// nothing to add
					}
				}
			}
		}
		return list;
	}
	
	private ArrayList<Float> getDosageAsFloats(String dosis, String trennzeichen){
		ArrayList<Float> list = new ArrayList<Float>();
		if (dosis.indexOf('-') != -1 || dosis.indexOf('/') != -1) {
			String[] dos = dosis.split(trennzeichen);
			if (dos.length > 2) {
				for (String d : dos) {
					boolean hasDigit = d.matches("^[~/.]*[½¼0-9].*");
					if (d.indexOf(' ') != -1)
						list.add(getNum(d.substring(0, d.indexOf(' '))));
					else if (d.length() > 0 && hasDigit)
						list.add(getNum(d));
					else if (d.length() == 0)
						list.add(0.0f);
					if (list.size() >= 4)
						return list;
				}
			} else if (dos.length > 1) {
				list.add(getNum(dos[1]));
			} else {
				// nothing to add
			}
		}
		return list;
	}
	
	private static final String special_num_at_start = "^(~|)[0-9]/[0-9][ a-zA-Z]*$";
	
	private float getNum(String num){
		try {
			String n = num.trim();
			if (n.matches(special_num_at_start)) {
				float value = getNum(n.substring(0, 1)) / getNum(n.substring(2));
				return value;
			} else if (n.equalsIgnoreCase("½")) {
				return 0.5F;
			}else if (n.equalsIgnoreCase("¼")) {
				return 0.25F;
			}else if (n.equalsIgnoreCase("1½")) {
				return 1.5F;
			
			} else if (n.indexOf('/') != -1) {
{'fraction_non_alphanumeric': 0.17790834312573442, 'fraction_numerical': 0.016216216216216217, 'mean_word_length': 3.285498489425982, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6003430', 'n_tokens_mistral': 3745, 'n_tokens_neox': 3155, 'n_words': 591}