Alexa 技能开发

Alexa skill development

我正在寻找一种方法,将 Alexa 用作网站新发布的论文和公告的通知和词典系统。

为此,我会在 Raspberry 上使用 Node.js 实例来定期抓取新的 PDF。

我是 Alexa 环境的新手,正在寻找一些方向。

问:有没有办法让 Alexa 像维基百科查询技巧一样查找这些 PDF 并阅读所询问关键字的定义? 问:让 raspberry 不 public 在互联网上可用,而是定期将数据推送到由 alexa 查询的云数据库会更好吗? 问:我必须以机器可读的格式解析它吗? Q:有没有更好的爬取方式?

感谢您的任何建议

我相信您是在问如何制作可以进行查询的 Alexa 技能 "are there new papers about ?"

你是对的,一个好的设计是让你的爬虫独立并发布到数据库。然后,您可以创建一项技能,该技能使用带有 AMAZON.SearchQuery 槽的意图来捕获用户查询。您的技能代码可以执行数据库查找并决定如何响应。

您可能会发现以下内容有帮助:https://forums.developer.amazon.com/questions/128538/sample-skill-using-amazonsearchquery.html

Q1。是的,有一种方法可以让 Alexa 查找这些 PDF 并阅读定义。 Amazon Alexa 支持 lambda 函数。 Lambda 支持 .Net Core。 Foxit PDF SDK 6.4 适用于 .Net Core。 Foxit PDF SDK 6.4 支持在 PDF 中搜索关键字。您可以使用 Foxxit PDF SDK 搜索关键字并尝试解析 PDF 中的文本数据以获取定义。

此解决方案需要 Foxit PDF SDK 6.4 for .net。您可以在 link 找到评估包的请求:https://developers.foxitsoftware.com/pdf-sdk/free-trial

开始添加fsdk_dotnet.dll 作为对Visual Studio "AWS Lambda Project(.Net Core - C#)." 的引用 fsdk_dotnet.dll 位于评估包的lib 目录中。完成后,您可以添加以下 using 语句。

using foxit;
using foxit.common;
using foxit.common.fxcrt;
using foxit.pdf;

对于你的功能,它看起来像这样。

public string SearchPDF(string inputPDF, string searchTerm)//inputPDF is the PDF path with the PDF itself and its .pdf extension.  the serachTerm is the term you want to search.
{
    string sn = "SNValue"; //the SN value provided in the evaluation package at lib\gsdk_sn.txt
    string key = "SignKeyValue"; //the Sign value provided in evaluation package at lib\gsdk_key.txt
    ErrorCode error_code;
    try
    {
        error_code = Library.Initialize(sn, key);  //Unlocks the library to be used.  Make sure you update the sn and key file accordingly.
        if (error_code != ErrorCode.e_ErrSuccess)
        {
            return error_code.ToString();
        }
        PDFDoc doc = new PDFDoc(inputPDF); 
        error_code = doc.Load(null); //Loads the PDF into the Foxit PDF SDK
        if (error_code != ErrorCode.e_ErrSuccess)
        {
            return error_code.ToString(); //Returns a error code if loading the document fails
        }

        using (TextSearch search = new TextSearch(doc, null))
        {
            int start_index = 0;
            int end_index = doc.GetPageCount() - 1;
            search.SetStartPage(0);
            search.SetEndPage(doc.GetPageCount() - 1);
            search.SetPattern(searchTerm); //Sets the search term to be search in the PDF

            Int32 flags = (int)TextSearch.SearchFlags.e_SearchNormal;
            // if want to specify flags, you can do as followings:
            // flags |= TextSearch::e_SearchMatchCase;
            // flags |= TextSearch::e_SearchMatchWholeWord;
            // flags |= TextSearch::e_SearchConsecutive;

            int match_count = 0;
            while (search.FindNext())
            {
                RectFArray rect_array = search.GetMatchRects()
                string sentenceWithSearchTerm = search.GetMatchSentence();// Gets the sentence with the search term
                match_count++;
            }
        }

        doc.Dispose();
        Library.Release();
    }
    catch (foxit.PDFException e)
    {
        return e.Message;
    }
    catch (Exception e)
    {
        return e.Message;
    }
    return error_code.ToString().ToUpper(); //If successful this will return the "E_ERRSUCCESS." Please check out the headers for other error codes.
}

Q2:上面的解决方案使用了AWS Lambda,需要联网。这不使用数据库,但是,如果您愿意,可以将 PDF 页面中的文本提取到数据库中。上面的代码显示了如何使用字符串数据获取句子。如果您想获取PDF中的所有文本,请参阅下面的代码。

 using (var doc = new PDFDoc(inputPDF)){
    error_code = doc.Load(null);
    if (error_code != ErrorCode.e_ErrSuccess)
    {
        return error_code.ToString();
    }

    // Get page count
    int pageCount = doc.GetPageCount();
    for (int i = 0; i < pageCount; i++) //A loop that goes through each page
    {
        using (var page = doc.GetPage(i))
        {
            // Parse page
            page.StartParse((int)PDFPage.ParseFlags.e_ParsePageNormal, null, false);
            // Get the text select object.
            using (var text_select = new TextPage(page, (int)TextPage.TextParseFlags.e_ParseTextNormal))
            {
                int count = text_select.GetCharCount();
                if (count > 0)
                {
                    String chars = text_select.GetChars(0, count); //gets the text on the PDF page.
                }
            }
        }
    }   
}

Q3。我不确定您所说的机器可读格式是什么意思,但是 Foxit PDF SDK 可以提供字符串格式的文本。

Q4。抓取 PDF 文本数据的最佳方式是使用我上面提供的解决方案。