<commit_before>package com.jayantkrish.jklol.ccg.lambda;

import java.util.List;
import java.util.regex.Pattern;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.jayantkrish.jklol.util.CsvParser;

/**
 * Type declaration that determines type information
 * by matching each constant against a list of regular
 * expression rules. 
 * 
 * @author jayantk
 *
 */
public class RegexTypeDeclaration extends AbstractTypeDeclaration {

  private List<Pattern> patterns;
  private List<Type> types;
  
  public RegexTypeDeclaration(List<Pattern> patterns, List<Type> types) {
    this.patterns = ImmutableList.copyOf(patterns);
    this.types = ImmutableList.copyOf(types);
  }
  
  public static RegexTypeDeclaration fromCsv(List<String> lines) {
    CsvParser parser = CsvParser.defaultParser();
    ExpressionParser<Type> typeParser = ExpressionParser.typeParser();
    List<Pattern> patterns = Lists.newArrayList();
    List<Type> types = Lists.newArrayList();
    for (String line : lines) {
      String[] parts = parser.parseLine(line);
      
      patterns.add(Pattern.compile(parts[0]));
      types.add(typeParser.parse(parts[1]));
    }
    return new RegexTypeDeclaration(patterns, types);
  }

  @Override
  public Type getType(String constant) {
    for (int i = 0; i < patterns.size(); i++) {
      if (patterns.get(i).matcher(constant).matches()) {
        return types.get(i);
      }<commit_msg>parsing type declarations from string arrays<commit_after>package com.jayantkrish.jklol.ccg.lambda;

import java.util.List;
import java.util.regex.Pattern;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.jayantkrish.jklol.util.CsvParser;

/**
 * Type declaration that determines type information
 * by matching each constant against a list of regular
 * expression rules. 
 * 
 * @author jayantk
 *
 */
public class RegexTypeDeclaration extends AbstractTypeDeclaration {

  private List<Pattern> patterns;
  private List<Type> types;
  
  public RegexTypeDeclaration(List<Pattern> patterns, List<Type> types) {
    this.patterns = ImmutableList.copyOf(patterns);
    this.types = ImmutableList.copyOf(types);
  }
  
  public static RegexTypeDeclaration fromCsv(List<String> lines) {
    CsvParser parser = CsvParser.defaultParser();
    ExpressionParser<Type> typeParser = ExpressionParser.typeParser();
    List<Pattern> patterns = Lists.newArrayList();
    List<Type> types = Lists.newArrayList();
    for (String line : lines) {
      String[] parts = parser.parseLine(line);
      
      patterns.add(Pattern.compile(parts[0]));
      types.add(typeParser.parse(parts[1]));
    }
    return new RegexTypeDeclaration(patterns, types);
  }
  
  public static RegexTypeDeclaration fromStringArrays(String[][] lines) {
    ExpressionParser<Type> typeParser = ExpressionParser.typeParser();
    List<Pattern> patterns = Lists.newArrayList();
    List<Type> types = Lists.newArrayList();
    for (String[] parts : lines) {
      patterns.add(Pattern.compile(parts[0]));
      types.add(typeParser.parse(parts[1]));
    }
    return new RegexTypeDeclaration(patterns, types);
  }

  @Override
  public Type getType(String constant) {
    for (int i = 0; i < patterns.size(); i++) {
      if (patterns.get(i).matcher(constant).matches()) {
        return types.get(i);
      }
{'fraction_non_alphanumeric': 0.11768184506209343, 'fraction_numerical': 0.002365464222353637, 'mean_word_length': 4.094879518072289, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 22, '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': '5904756', 'n_tokens_mistral': 1020, 'n_tokens_neox': 1005, 'n_words': 223}