简体   繁体   English

通过遍历数组搜索文档中的关键字

[英]Searching for keywords in a document by looping through an array

I am working on a script to read and search for keywords in a text file that is uploaded to a website. 我正在编写一个脚本,以读取和搜索上载到网站的文本文件中的关键字。 I have an idea about how to read a string and report the number of occurrences of a word using RegEx, but I can't figure out how to access the text from fileReader once it is loaded. 我有一个关于如何使用RegEx读取字符串并报告单词出现次数的想法,但是我无法弄清楚一旦加载了FileReader则如何访问文本。 I want to call this script by clicking on the "Start Coding" button after the FileReader has done its work. 我想通过FileReader完成工作后单击“开始编码”按钮来调用此脚本。 I created a function to make this work, but I can't figure out how to access the file reader text. 我创建了一个函数来完成这项工作,但是我不知道如何访问文件阅读器文本。 Thanks for your help! 谢谢你的帮助!

I got the script for the fileReader from here How to read text file in JavaScript 我从这里获得了fileReader的脚本如何在JavaScript中读取文本文件

  <!DOCTYPE html>
<html>
  <head>
    <title>Read File (via User Input selection)</title>
    <button onclick="myFunction()">Start Coding</button>
    <script type="text/javascript">
    alert ("Please insert your text in the box below and we will begin searching")

    var reader; //GLOBAL File Reader object for demo purpose only

    /**
     * Check for the various File API support.
     */
    function checkFileAPI() {
        if (window.File && window.FileReader && window.FileList && window.Blob) {
            reader = new FileReader();
            return true; 
        } else {
            alert('The File APIs are not fully supported by your browser. Fallback required.');
            return false;
        }
    }

    /**
     * read text input
     */
    function readText(filePath) {
        var output = ""; //placeholder for text output
        if(filePath.files && filePath.files[0]) {           
            reader.onload = function (e) {
                output = e.target.result;
                displayContents(output);
            };//end onload()
            reader.readAsText(filePath.files[0]);
        }//end if html5 filelist support
        else if(ActiveXObject && filePath) { //fallback to IE 6-8 support via ActiveX
            try {
                reader = new ActiveXObject("Scripting.FileSystemObject");
                var file = reader.OpenTextFile(filePath, 1); //ActiveX File Object
                output = file.ReadAll(); //text contents of file
                file.Close(); //close file "input stream"
                displayContents(output);
            } catch (e) {
                if (e.number == -2146827859) {
                    alert('Unable to access local files due to browser security settings. ' + 
                     'To overcome this, go to Tools->Internet Options->Security->Custom Level. ' + 
                     'Find the setting for "Initialize and script ActiveX controls not marked as safe" and change it to "Enable" or "Prompt"'); 
                }
            }       
        }
        else { //this is where you could fallback to Java Applet, Flash or similar
            return false;
        }       
        return true;
    }   

    /**
     * display content using a basic HTML replacement
     */
    function displayContents(txt) {
        var el = document.getElementById('main'); 
        el.innerHTML = txt; //display output in DOM
    }   

    function myFunction() {
        alert("We will start by highlighting all the required search terms then we will being our search by looking for the instances of the word 'pilot'")
        var count = (txt.match(/program/g) || []).length

        alert ("your word occured " + count + " times.")
     }


</script>
</head>
<body onload="checkFileAPI();">
    <div id="container">    
        <input type="file" onchange='readText(this)' />
        <br/>
        <hr/>   
        <h3>Contents of the Text file:</h3>
        <div id="main">
            ...
        </div>
    </div>
</body>
</html>

Update 更新资料

I figured out how to connect to the documents that were loaded onto the site thanks to NinjaM and some more research. 由于有了NinjaM和更多研究,我找到了如何连接到已加载到站点上的文档的方法。 I had to connect to the ID and used the .innerHTML method to connect with the uploaded document. 我必须连接到ID,并使用.innerHTML方法连接上载的文档。 Everything is working fine, except I now have a new problem with getting my loop to work properly. 一切工作正常,除了我现在遇到使循环正常工作的新问题。

I want to have users upload a document to this site and then have them click through a series of buttons that search for keywords in the document. 我想让用户将文档上载到此站点,然后让他们单击一系列在文档中搜索关键字的按钮。 I work with an organization that is coding documents on climate change and I'm trying to speed up the process. 我与一个组织有关气候变化的文档编码的组织合作,并且我试图加快这一过程。 Typically, a person reads through the documents and then uses control-f to search for certain climate change keywords. 通常,一个人通读文档,然后使用control-f搜索某些气候变化关键字。 If the keywords are in the document in the right context, then the user makes a note of it in our database. 如果关键字在正确的上下文中位于文档中,则用户在我们的数据库中对其进行注释。 This site will automate that process and reduce some of the risk that people will not search for the appropriate term or won't search all of the terms. 该站点将使该过程自动化,并减少了人们不会搜索适当术语或不会搜索所有术语的某些风险。

I have a series of RegEx expressions that I would like to put through a loop and search for in the document. 我有一系列RegEx表达式,我想循环使用它们并在文档中进行搜索。 I made the loop and the expressions but I would like for my loop to start over again after it has search through each item in the array. 我制作了循环和表达式,但是我希望循环在数组中搜索完每个项目后重新开始。 For example, once it has searched the document for the key word pilot I would then like for it to start at the beginning again and search the whole document for the keyword project . 例如,一旦它在文档中搜索了关键字pilot ,那么我希望它从头开始,并在整个文档中搜索关键字project Right now the search looks for pilot in the document and then once it finds the last instance of pilot , it moves on to project . 现在,搜索将在文档中寻找试点 ,然后一旦找到试点的最后一个实例,便进入项目 This is the portion of the code that I would like help to fix : function windowFind(){ var pilotWords = ["pilot","project"]; for (var i = 0; i < pilotWords.length; i++) { ("Find -> are = " + window.find(pilotWords[i]))} } 这是我希望帮助解决的部分代码: function windowFind(){ var pilotWords = ["pilot","project"]; for (var i = 0; i < pilotWords.length; i++) { ("Find -> are = " + window.find(pilotWords[i]))} } function windowFind(){ var pilotWords = ["pilot","project"]; for (var i = 0; i < pilotWords.length; i++) { ("Find -> are = " + window.find(pilotWords[i]))} }

You can check out a basic version of the site on github https://calebdow.github.io/ You will have to find upload a test file 您可以在github https://calebdow.github.io/上查看该网站的基本版本。您将必须找到上传测试文件

The contents of the text file would be displayed in 文本文件的内容将显示在

<div id="main">
    ...
</div>

or in plain text when you run the open the html file. 或以纯文本格式运行时打开html文件。 As far as using the text in Javascript, it would be place in a variable named output which is defined in the code. 至于使用Javascript中的文本,它将放置在代码中定义的名为output的变量中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM