text: const algorithmia = require('algorithmia')
  const apiKey = require('../credentials/algorithmia.json').apiKey
  const sentenceBoundaryDetection = require('sbd')
  const watsonApiKey = require('../credentials/watson-nlu.json').apikey
  const watsonUrl = require('../credentials/watson-nlu.json').url
  
  var NaturalLanguageUnderstandingV1 = require('ibm-watson/natural-language-understanding/v1.js');
  const { IamAuthenticator } = require('ibm-watson/auth');
  
  var nlu = new NaturalLanguageUnderstandingV1({
      // note: if unspecified here, credentials are pulled from environment properties:
      // NATURAL_LANGUAGE_UNDERSTANDING_USERNAME &  NATURAL_LANGUAGE_UNDERSTANDING_PASSWORD
      // username: '<username>'.
      // password: '<password>',
      version: '2019-07-12',
      authenticator: new IamAuthenticator({
          apikey: watsonApiKey,
      }),
      url: watsonUrl
  });
  
  const state = require('./state.js')
  
  async function robot(){
      const content = state.load();
      
      await fetchContentFromWikipedia(content);
      sanitizeContent(content)
      breakContentIntoSetences(content)
      limitMaximumSentences(content)
      await fetchKeywordsOfAllSentences(content)
      
      state.save(content)
      
      
      async function fetchContentFromWikipedia(apiKey){
          
          const algorithmiaAuthenticated = algorithmia('sim2V/WOmvFrwCAikFAhzipIgpb1');
          const wikipediaAlgorithm = algorithmiaAuthenticated.algo('web/WikipediaParser/0.1.2');
          const wikipediaResponse = await wikipediaAlgorithm.pipe(content.searchTerm);
          const wikipediaContent = wikipediaResponse.get();
          
          content.sourceContentOriginal = wikipediaContent.content
      }
      
      function sanitizeContent(content){
          const withoutBlankLinesAndMarkdown = removeBlankLinesAndMarkdown(content.sourceContentOriginal);
          const withoutDatesInParentheses = removeDatesInParentheses(withoutBlankLinesAndMarkdown)
          
          content.sourceContentSanitized = withoutDatesInParentheses
          
          function removeBlankLinesAndMarkdown(text){
              const allLines = text.split('\n');
              
              const withoutBlankLinesAndMarkdown = allLines.filter((line) => {
                  if(line.trim().length === 0 || line.trim().startsWith('=')){
                      return false
                  }
                  return true
              })
              
              return withoutBlankLinesAndMarkdown.join(' ')
          }
          
          function removeDatesInParentheses(text){
              return text.replace(/\((?:\([^()]*\)|[^()])*\)/gm, '').replace(/  /g, ' ')
          }
      }
      
      function breakContentIntoSetences(content){
          content.sentences = [];
          const sentences = sentenceBoundaryDetection.sentences(content.sourceContentSanitized)
          sentences.forEach((sentence) => {
              content.sentences.push({
                  text: sentence,
                  keywords: [],
                  images: []
              })
          })
      }
      
      function limitMaximumSentences(content){
          content.sentences = content.sentences.slice(0, content.maximumSentences)
      }
      
      async function fetchKeywordsOfAllSentences(content){
          for(const sentence of content.sentences){
              sentence.keywords = await fetchWatsonAndReturnKeywords(sentence.text)
          }
      }
      
      async function fetchWatsonAndReturnKeywords(sentence){
          return new Promise((resolve, reject) => {
              nlu.analyze({
                  text: sentence,
                  features: {
                      keywords: {}
                  }
              }, (error, response) => {
                  if(error){
                      throw error
                  }
                  const keywords = response.result.keywords.map((keyword) => {
                      return keyword.text
                  })
                  
                  resolve(keywords)
              })
          })
      }
      
  }
  
  module.exports = robot