2017-02-08 7 views
0

저는 몇 가지 핵심 단어에 대해 질문에 답하는 봇을 만들고 있습니다. Deni Spasovski가 개발 한 아주 쉬운 코드입니다. 그러나, 내 웹 사이트는 스페인어로되어 있으며 핵심 단어의 일부로 악센트 부호 &을 사용할 수 있어야합니다. 예를 들어 :자바 스크립트에서 악센트 부호 및 ñ 사용 하시겠습니까?

{ "keys": ["cómo", "estás"], "value": 0 } 

는하지만, 내 노력에도 불구하고, 나는 코드를 수정할 수없는 것. 코딩에 대해 많이 알지 못합니다. 어쩌면 스크립트 중 하나에 구현해야하는 매우 쉬운 일일 수도 있지만 실제로 무엇인지 모르겠습니다.

나를 도와 주시면 감사하겠습니다.

대단히 감사드립니다.

<!DOCTYPE html> 
<html> 
<head> 
    <!--Read the source Luke!--> 
    <title>Answer bot</title> 
    <style type="text/css"> 
     body, html 
     { 
      height: 100%; 
     } 
     .wrap 
     { 
      height: 80%; 
      overflow: auto; 
      max-height: 80%; 
      display: block; 
     } 
     .content 
     { 
      height: 100%; 
      display: table; 
      vertical-align: bottom; 
     } 
     .subcontent 
     { 
      display: table-cell; 
      vertical-align: bottom; 
     } 
     .answerbot-input 
     { 
      color: #1AA1E1; 
     } 
     .answerbot-ai 
     { 
      color: #CE5043; 
     } 
    </style> 
</head> 
<body> 
    <div style="width: 800px; height: 100%; margin: 0 auto;"> 
     <div id="wrap" class="wrap"> 
      <div class="content"> 
       <div class="subcontent" id='subcontent'> 
        <p class='answerbot-ai'> 
         Don't be afraid, talk to me. 
        </p> 
       </div> 
      </div> 
     </div> 
     <div> 
      <input type="text" name="inputtext" style="width: 100%;" onkeyup="keypressInput(this, event);"><br /> 
     </div> 
    </div> 
    <script type="text/javascript" src="scripts.js"></script> 
    <script type="text/javascript" src="data.js"></script> 
    <script type="text/javascript"> 
     var _answerBot = new answerBot(); 
     function keypressInput(sender, event) { 
      if (event.which == 13) { 
       document.getElementById('subcontent').innerHTML += _answerBot.processInput(sender.value); 
       sender.value = ''; 
       correctScroll("wrap"); 
      } 
     } 

     function correctScroll(elementId) { 
      var objDiv = document.getElementById(elementId); 
      objDiv.scrollTop = objDiv.scrollHeight; 
     } 
    </script> 
</body> 
</html> 

는 data.js입니다 :

if (answerBot) { 
    answerBot.prototype.specialContext = { 
     "wrongInput": ["I don't understand you.", "Could you rephrase the question?"], 
     "emptyInput": ["Please say something", "Speak louder", "Well i can't read minds."], 
     "rephrase": ["Can you tell me if your question was about one of the following things:"] 
    }; 

    answerBot.prototype.keywords = [ 
     { "keys": ["hi"], "value": 0 }, 
     { "keys": ["hello"], "value": 0 }, 
     { "keys": ["life", "universe", "everything"], "value": 1 }, 
     { "keys": ["software", "development"], "value": 2 }, 
     { "keys": ["software", "engineering"], "value": 2 }, 
     { "keys": ["who", "made", "you"], "value": 3 }, 
     { "keys": ["who", "wrote", "you"], "value": 3 }, 
     { "keys": ["who", "coded", "you"], "value": 3 }, 
     { "keys": ["is", "this", "real", "life"], "value": 4 }, 
     { "keys": ["who", "is", "deni"], "value": 5 }, 
     { "keys": ["tell", "me", "about", "deni"], "value": 5 }, 
     { "keys": ["tell", "me", "about", "author"], "value": 5 }, 
     { "keys": ["show", "me", "author"], "value": 5 }, 
     { "keys": ["can", "see", "source"], "value": 6 }, 
     { "keys": ["can", "see", "sourcecode"], "value": 6 }, 
     { "keys": ["show", "me", "code"], "value": 6 }, 
     { "keys": ["how", "are", "you"], "value": 7 }, 
     { "keys": ["who", "is", "this"], "value": 8 }]; 

    answerBot.prototype.answers = [ 
     { 
      "description": "Hi!", 
      "values": ["Hello there!", "Hi how can I help you today", "Hi! What brings you here?"] 
     }, 
     { 
      "description": "What is the answer to life the universe and everything?", 
      "values": ["42"] 
     }, 
     { 
      "description": "What is software development?", 
      "values": ["Programming! Do you speak it?"] 
     }, 
     { 
      "description": "Who created me?", 
      "values": ["I was created by another <a href='http://about.me/deni' target='_blank'>bot</a>.", "The question is who sent you here?"] 
     }, 
     { 
      "description": "Is this real life?", 
      "values": ["No this is the internetz!", "Find out <a href='http://www.youtube.com/watch?v=txqiwrbYGrs' target='_blank'>yourself</a>!"] 
     }, 
     { 
      "description": "Who is Deni?", 
      "values": ["This is his <a href='https://plus.google.com/+DeniSpasovski/' target='_blank'>G+ profile</a>.", "This is his <a href='www.linkedin.com/in/denispasovski' target='_blank'>Linkedin profile</a>."] 
     }, 
     { 
      "description": "Where is your source?", 
      "values": ["Here is the <a href='https://github.com/denimf/Answer-bot' target='_blank'>source</a>."] 
     }, 
     { 
      "description": "How are you?", 
      "values": ["I'm good how are you?"] 
     }, 
     { 
      "description": "Who is this?", 
      "values": ["StackOverflow Exception occurred", "The question is who are you?"] 
     } 
     ]; 
} 

그리고 이것은 scripts.js입니다 :

var answerBot = function() { 
    var _this = this; 
    _this.processInput = function (text) { 
     updateUrl(text); 
     var _result = "<p class='answerbot-input'>" + text + "</p>"; 
     text = text.replace(new RegExp("[^a-zA-Z ]", "g"), " "); 
     text = text.replace(new RegExp("[ ]{2,}", "g"), " "); 
     var _words = text.toLowerCase().split(" "); 
     var _answers = []; 
     var _title = ""; 
     if (_words.length === 0 || _words.toString() === '') { //if the input is empty 
      _answers = _this.specialContext.emptyInput; 
      _title = _this.specialContext.emptyInput; 
     } else { 
      var _possibleAnswers = findMatches(_words); 
      if (_possibleAnswers.length === 0) { //if no answer found 
       _answers = _this.specialContext.wrongInput; 
       _title = _this.specialContext.wrongInput; 
      } 
      if (_possibleAnswers.length == 1) { //context recognized 
       _answers = _this.answers[_possibleAnswers[0]].values; 
       _title = _this.answers[_possibleAnswers[0]].description; 
      } 
      if (_possibleAnswers.length > 1) { 
       _result += formatText(_this.specialContext.rephrase, _this.specialContext.rephrase); 
       for (var i = 0; i < _possibleAnswers.length; i++) { 
        _result += formatText(_this.answers[_possibleAnswers[i]].description, _this.answers[_possibleAnswers[i]].description); 
       } 
      } 
     } 
     if (_answers.length > 0) { 
      var _rand = Math.floor((Math.random() - 0.001) * _answers.length); 
      _result += formatText(_answers[_rand], _title); 
     } 
     return _result; 
    }; 

    function formatText(text, title) { 
     return "<p class=\'answerbot-ai\' title=\'" + title + "\'>" + text + "</p>"; 
    } 

    function findMatches(words) { 
     var foundKeywords = []; 
     var _possibleAnswers = []; 
     for (var i = 0; i < _this.keywords.length; i++) { 
      foundKeywords[i] = 0; 
      for (var j = 0; j < words.length; j++) { 
       if (_this.keywords[i].keys.indexOf(words[j]) >= 0) { 
        foundKeywords[i]++; 
        if (foundKeywords[i] == _this.keywords[i].keys.length) { 
         return [_this.keywords[i].value]; 
        } 
       } 
      } 
      if (foundKeywords[i] * 2 > _this.keywords[i].keys.length) { 
       _possibleAnswers.push(_this.keywords[i].value); 
      } 
     } 
     return _possibleAnswers.filter(function (elem, pos) { 
      return _possibleAnswers.indexOf(elem) == pos; 
     }); 
    } 

    function updateUrl(text){ 
     history.pushState(null, null, "#question=" + encodeURIComponent(text)); 
     if(typeof ga === "function")//google analytics 
      ga('send', 'event', 'question', text); 
    } 
}; 
+1

무엇이 문제입니까? 무엇이 작동합니까/작동하지 않습니까? – Pointy

+0

Alex, 브라우저의 콘솔에서 테스트했는데 특수 문자에 문제가없는 것 같습니다. HTML 코드에''을 추가하십시오. –

답변

1

세 가지 가능한 문제 :

는 HTML입니다

1) 행 :

text = text.replace(new RegExp("[^a-zA-Z ]", "g"), " "); 

"일반"알파벳이 아닌 것은 공백으로 대체합니다. 분음 부호가 포함 된 문자를 포함하도록 어느 시점에서 대체하도록 선택할 수도 있지만 작동 시키려면 단순히 주석 처리 만 할 수 있습니다.

2) 파일 (특히 data.js)이 UTF-8 인코딩으로 저장되었는지 확인하십시오.

3) answers 배열에 적절한 값이 있는지 확인한 다음 keywords.value에 색인을 사용하십시오. 예 :

{ 
     "description": "cómo estás?", 
     "values": ["muy bien"] 
    } 
+0

정말 고마워요 !!!! – Alex