<commit_before>const functions = require('firebase-functions');

// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
// exports.helloWorld = functions.https.onRequest((request, response) => {
//  response.send("Hello from Firebase!");
// });


const admin = require('firebase-admin');



admin.initializeApp(functions.config().firebase);

let scorenames = ["b", "g", "i", "u", "w"];
let scores = {};
  scorenames.forEach((scorename) => {
    scores[scorename] = [];
  });
  
function calculateScores(scores) {
  let returnscores = {};
  for (let scorename of scorenames) {
    let cleanArray = scores[scorename].filter(x => x != '' && x != "-");
    returnscores["score_" + scorename] = cleanArray.length > 0 ? cleanArray.reduce((a, b) => a + b) / cleanArray.length : null;
  }
  return returnscores;
}

exports.createExperience = functions.firestore
  .document('birthexperiences/{documentId}')
  .onCreate((event) => {
   



    console.log("start");

    let db = admin.firestore();
    let birthplaceid = event.data.get('birthplace_id');
    //let birthdate = event.data.get('birth_date');
    console.info(event);
    console.info(birthplaceid);
    let experiences = db.collection('birthexperiences').where('birthplace_id', '==', birthplaceid);
    
    
    experiences.get().then((querySnapshot) => {
      querySnapshot.forEach((doc) => {
        let thisexperience = doc.data();
        for (var property in thisexperience) {
          if (thisexperience.hasOwnProperty(property)) {
            if (scorenames.includes(property.charAt(0)) && property.length < 4) {
              scores[property.charAt(0)].push(thisexperience[property])
            }
          }
        }
      })

      let score = calculateScores(scores);
      score["experiences"] = querySnapshot.size;
      var docRef = db.collection('birthplaces').doc(birthplaceid);
      
      docRef.update(score).then(function () {
        console.info("Document successfully updated!");
      })
        .catch(function (error) {
          console.info("Error updating document: ", error);
        });
    });
    
  });<commit_msg>firestore function for calculate Scores added<commit_after>const functions = require('firebase-functions');

// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
// exports.helloWorld = functions.https.onRequest((request, response) => {
//  response.send("Hello from Firebase!");
// });


const admin = require('firebase-admin');



admin.initializeApp(functions.config().firebase);

let scorenames = ["u", "e", "k", "m", "w"];


function calculateScores(scores) {
  let returnscores = {};

  for (let scorename of scorenames) {

    let cleanArray = scores[scorename].filter(x => x != '' && x != "-");
    returnscores["score_" + scorename] = cleanArray.length > 0 ? cleanArray.reduce((a, b) => a + b) / cleanArray.length : null;
  }
  return returnscores;
}

exports.createExperience = functions.firestore
  .document('birthexperiences/{documentId}')
  .onCreate((event) => {


    let scores = {};
    scorenames.forEach((scorename) => {
      scores[scorename] = [];
    });

    let db = admin.firestore();
    let birthplaceid = event.data.get('birthplace_id');
    let experiences = db.collection('birthexperiences').where('birthplace_id', '==', birthplaceid);


    experiences.get().then((querySnapshot) => {
      querySnapshot.forEach((doc) => {
        let thisexperience = doc.data();
        for (var property in thisexperience) {
          if (thisexperience.hasOwnProperty(property)) {
            if (scorenames.includes(property.charAt(0)) && property.length < 4) {
              scores[property.charAt(0)].push(thisexperience[property])
            }
          }
        }
      })


      let score = calculateScores(scores);
      score["experiences"] = querySnapshot.size;
      var docRef = db.collection('birthplaces').doc(birthplaceid);

      docRef.update(score).then(function () {
        console.info("Document successfully updated!");
      })
        .catch(function (error) {
          console.info("Error updating document: ", error);
        });
    });
    return true;

  });
{'fraction_non_alphanumeric': 0.16186460529397986, 'fraction_numerical': 0.0018739751698289998, 'mean_word_length': 3.133591481122943, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 2, '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': '4506406', 'n_tokens_mistral': 1294, 'n_tokens_neox': 1237, 'n_words': 227}