【问题标题】:Retrieving Fields of study using Microsoft Academic Graph API使用 Microsoft Academic Graph API 检索学习领域
【发布时间】:2020-01-21 15:15:30
【问题描述】:

我正在尝试从 Microsoft Academic Graph 中检索级别 0 FieldOfStudy“计算机科学”的整个“学习领域”列表。到目前为止,我有以下 curl 代码来检索字段:

curl -X POST \
  https://api.labs.cognitive.microsoft.com/academic/v1.0/evaluate \
  -H 'Cache-Control: no-cache' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -H 'Ocp-Apim-Subscription-Key: my_subscription_key' \
  -d 'expr=Ty%3D'\''6'\''&attributes=Id%2CFL%2CFN%2CFC.FN%2CFP.FN%2CFC.FId%2CFP.FId'

这不会引发任何错误,但需要进一步修改以便检索:

  1. “计算机科学”研究领域的所有研究子领域(子、孙等)。
  2. 不限于前 1000 个研究领域(POST 评估的最大限制)。

虽然我在 curl 中执行此操作,但我也愿意接受 python 方法,以防它是更好的选择。

【问题讨论】:

    标签: curl microsoft-cognitive academic-graph


    【解决方案1】:

    如果您的目标是枚举计算机科学下的所有后代研究领域,则您需要进行递归调用,因为每个研究领域(即父母和子女,而不是祖父母或孙子女)只为直接级别编制索引。

    幸运的是,使用查询表达式“Composite(FP.FId=parent_fos_id)”完成这项工作相当简单。

    这里是一些示例 C# 代码,用于获取所有后代的研究领域(抱歉,我不精通 Python,但应该很容易弄清楚我在做什么):

    static void GetAllDescendantFieldsOfStudy(long fieldOfStudyId, int level, ref SortedSet<long> descendants)
    {
        var client = new HttpClient();
        client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "_subscription_key_");
    
        var jsonString = 
            client
            .GetStringAsync(
                new Uri($"https://api.labs.cognitive.microsoft.com/academic/v1.0/evaluate?expr=Composite(FP.FId={fieldOfStudyId})&model=latest&count=1000&offset=0&attributes=Id,DFN"))
            .Result;
    
        var jsonObject = Newtonsoft.Json.Linq.JObject.Parse(jsonString);
    
        var childCount = jsonObject["entities"].Count();
    
        if (childCount > 0)
        {
            var children = jsonObject["entities"];
    
            foreach (var child in children)
            {
                var childId = child.Value<long>("Id");
                if (!descendants.Contains(childId))
                {
                    descendants.Add(childId);
    
                    Console.WriteLine($"{new String('\t', level)}Expanding {child.Value<string>("DFN")}");
    
                    GetAllDescendantFieldsOfStudy(childId, level + 1, ref descendants);
                }
            }
        }
    }
    

    要使用它,只需使用计算机科学 ID 调用它,即:

    var descendants = new SortedSet<long>();
    
    GetAllDescendantFieldsOfStudy(41008148, 0, ref descendants);
    

    不幸的是,没有办法绕过 1000 的最大结果计数。您只需使用偏移量来分解您的请求。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多