0

我正在尝试使用 OpenCalais 标记内容。以下是我正在使用与 API 通信的代码:

import httplib2
import json
import ast

# Some local values needed for the call
LOCAL_API_KEY = '***********************' # Aquire this by registering at the Calais site
CALAIS_TAG_API = 'https://api.thomsonreuters.com/permid/calais'

# Some sample text from a news story to pass to Calais for analysis
test_body = 'Samsung is closing its Milk Music streaming service'

# header information need by Calais.
# For more info see http://www.opencalais.com/documentation/calais-web-service-api/api-invocation/rest
headers = {
    'X-AG-Access-Token': LOCAL_API_KEY,
    'Content-Type': 'text/raw',
    'outputFormat': 'application/json',
}

# Create your http object
http = httplib2.Http()
# Make the http post request, passing the body and headers as needed.
response, content = http.request(CALAIS_TAG_API, 'POST', headers=headers, body=test_body)

jcontent = json.loads(content) # Parse the json return into a python dict
output = json.dumps(jcontent, indent=4) # Pretty print the resulting dictionary returned.
print output

无论如何,这很好用,因为我能够获得以下输出(打印输出)。

{
    "http://d.opencalais.com/dochash-1/8a7adab6-d07e-38e6-b0c9-5db5220336c1/cat/1": {
        "score": 1, 
        "forenduserdisplay": "false", 
        "name": "Business_Finance", 
        "_typeGroup": "topics"
    }, 
    "http://d.opencalais.com/dochash-1/8a7adab6-d07e-38e6-b0c9-5db5220336c1/cat/2": {
        "score": 1, 
        "forenduserdisplay": "false", 
        "name": "Entertainment_Culture", 
        "_typeGroup": "topics"
    }, 
    "http://d.opencalais.com/dochash-1/8a7adab6-d07e-38e6-b0c9-5db5220336c1/lid/DefaultLangId": {
        "forenduserdisplay": "false", 
        "language": "http://d.opencalais.com/lid/DefaultLangId/InputTextTooShort", 
        "_typeGroup": "language"
    }, 
    "http://d.opencalais.com/dochash-1/8a7adab6-d07e-38e6-b0c9-5db5220336c1/Industry/1": {
        "name": "Phones & Handheld Devices - NEC", 
        "permid": "4294951233", 
        "forenduserdisplay": "false", 
        "_typeGroup": "industry", 
        "relevance": 0.8, 
        "rcscode": "B:1768"
    }, 
    "http://d.opencalais.com/comphash-1/c021d644-16e9-3060-96fe-b3be0cd4ae1e": {
        "_typeReference": "http://s.opencalais.com/1/type/em/e/Company", 
        "_type": "Company", 
        "name": "Samsung", 
        "confidence": {
            "aggregate": "0.905", 
            "resolution": "1.0", 
            "statisticalfeature": "0.876", 
            "dblookup": "0.0"
        }, 
        "_typeGroup": "entities", 
        "instances": [
            {
                "detection": "[]Samsung[ is closing its Milk Music streaming]", 
                "length": 7, 
                "exact": "Samsung", 
                "suffix": " is closing its Milk Music streaming", 
                "offset": 0
            }
        ], 
        "confidencelevel": "0.905", 
        "relevance": 0.8, 
        "nationality": "N/A", 
        "resolutions": [
            {
                "name": "SAMSUNG ELECTRONICS CO,.LTD", 
                "permid": "4295882451", 
                "commonname": "Samsung Elec", 
                "primaryric": "005930.KS", 
                "score": 1, 
                "ticker": "005930", 
                "id": "https://permid.org/1-4295882451"
            }
        ], 
        "forenduserdisplay": "false"
    }, 
    "doc": {
        "info": {
            "document": "Samsung is closing its Milk Music streaming service", 
            "docId": "http://d.opencalais.com/dochash-1/8a7adab6-d07e-38e6-b0c9-5db5220336c1", 
            "docDate": "2016-08-22 13:02:01.814", 
            "docTitle": "", 
            "ontology": "http://mdaas-virtual-onecalais.int.thomsonreuters.com/owlschema/9.8/onecalais.owl.allmetadata.xml", 
            "calaisRequestID": "eef490a6-2e3e-7cac-156b-257ebcf3beba", 
            "id": "http://id.opencalais.com/b*RzenPxfvWZmjCvQqpzNA"
        }, 
        "meta": {
            "stagsVer": "OneCalais_9.8-RELEASE-b6-2016-07-18_14:00:15", 
            "contentType": "text/raw", 
            "language": "InputTextTooShort", 
            "serverVersion": "OneCalais_9.8-RELEASE:109", 
            "submissionDate": "2016-08-22 13:02:01.679", 
            "processingVer": "AllMetadata", 
            "submitterCode": "0ca6a864-5659-789d-5f32-f365f695e757", 
            "signature": "digestalg-1|BovyytInhxJhSerNjEFvOZNAHJQ=|Q5g9GCOSi7+FnERjgY9y4B9oJukYPjYeTl6v+Zu81BJLwOBcIZZ/eA=="
        }
    }, 
    "http://d.opencalais.com/dochash-1/8a7adab6-d07e-38e6-b0c9-5db5220336c1/ComponentVersions": {
        "version": [
            "Deals Index:201608221149:201608221149", 
            "index-refineries:201608202306:201608202306", 
            "config-physicalAssets-powerStations:480:480", 
            "OA Index:201608212349:201608212349", 
            "NextTags:OneCalais_9.8-RELEASE:109", 
            "config-sca-DataPackage:38:38", 
            "com.clearforest.infoext.dial4j.plugins-basistechconfig:OneCalais_9.8-RELEASE:109", 
            "People Index:201608221124:201608221124", 
            "config-negativeSignature:480:480", 
            "Dial4J:OneCalais_8.6-RELEASE:209", 
            "OA Override:507:507", 
            "People Override:480:480", 
            "index-vessels:201608201644:201608201644", 
            "config-refineries:480:480", 
            "config-cse:507:507", 
            "config-vessels:480:480", 
            "OneCalais:OneCalais_9.8-RELEASE:109", 
            "config-physicalAssets-mines:480:480", 
            "SocialTags Index:201608212334:201608212334", 
            "BlackList:504:504", 
            "index-ports:201608202256:201608202256", 
            "config-physicalAssets-ports:480:480", 
            "config-drugs:480:480"
        ], 
        "_typeGroup": "versions"
    }, 
    "http://d.opencalais.com/comphash-1/e89d0187-8b46-3f8d-9f6b-4995a709c85e": {
        "_typeReference": "http://s.opencalais.com/1/type/em/e/Company", 
        "_type": "Company", 
        "name": "Milk Music", 
        "confidence": {
            "aggregate": "0.499", 
            "resolution": "0.0", 
            "statisticalfeature": "0.775", 
            "dblookup": "0.0"
        }, 
        "_typeGroup": "entities", 
        "instances": [
            {
                "suffix": " streaming service", 
                "prefix": "Samsung is closing its ", 
                "detection": "[Samsung is closing its ]Milk Music[ streaming service]", 
                "length": 10, 
                "offset": 23, 
                "exact": "Milk Music"
            }
        ], 
        "confidencelevel": "0.499", 
        "relevance": 0.8, 
        "nationality": "N/A", 
        "forenduserdisplay": "false"
    }, 
    "http://d.opencalais.com/dochash-1/8a7adab6-d07e-38e6-b0c9-5db5220336c1/cat/3": {
        "score": 1, 
        "forenduserdisplay": "false", 
        "name": "Technology_Internet", 
        "_typeGroup": "topics"
    }, 
    "http://d.opencalais.com/dochash-1/8a7adab6-d07e-38e6-b0c9-5db5220336c1/SocialTag/1": {
        "name": "Streaming music services", 
        "importance": "1", 
        "_typeGroup": "socialTag", 
        "originalValue": "Streaming music services", 
        "socialTag": "http://d.opencalais.com/genericHasher-1/d1447a37-4c52-3b2f-a9d2-40984014685b", 
        "forenduserdisplay": "true", 
        "id": "http://d.opencalais.com/dochash-1/8a7adab6-d07e-38e6-b0c9-5db5220336c1/SocialTag/1"
    }, 
    "http://d.opencalais.com/dochash-1/8a7adab6-d07e-38e6-b0c9-5db5220336c1/SocialTag/3": {
        "name": "Milk Music", 
        "importance": "1", 
        "_typeGroup": "socialTag", 
        "originalValue": "Milk Music (streaming service)", 
        "socialTag": "http://d.opencalais.com/genericHasher-1/471ee9b8-9f72-3a81-aa13-2a7d44658521", 
        "forenduserdisplay": "true", 
        "id": "http://d.opencalais.com/dochash-1/8a7adab6-d07e-38e6-b0c9-5db5220336c1/SocialTag/3"
    }, 
    "http://d.opencalais.com/dochash-1/8a7adab6-d07e-38e6-b0c9-5db5220336c1/SocialTag/2": {
        "name": "Digital audio", 
        "importance": "1", 
        "_typeGroup": "socialTag", 
        "originalValue": "Digital audio", 
        "socialTag": "http://d.opencalais.com/genericHasher-1/64447afb-045b-34db-9a52-dae5bed0254e", 
        "forenduserdisplay": "true", 
        "id": "http://d.opencalais.com/dochash-1/8a7adab6-d07e-38e6-b0c9-5db5220336c1/SocialTag/2"
    }, 
    "http://d.opencalais.com/dochash-1/8a7adab6-d07e-38e6-b0c9-5db5220336c1/SocialTag/5": {
        "name": "Smartphones", 
        "importance": "2", 
        "_typeGroup": "socialTag", 
        "originalValue": "Smartphones", 
        "socialTag": "http://d.opencalais.com/genericHasher-1/e42d9d7b-150b-3c30-974f-87a1fba000ef", 
        "forenduserdisplay": "true", 
        "id": "http://d.opencalais.com/dochash-1/8a7adab6-d07e-38e6-b0c9-5db5220336c1/SocialTag/5"
    }, 
    "http://d.opencalais.com/dochash-1/8a7adab6-d07e-38e6-b0c9-5db5220336c1/SocialTag/4": {
        "name": "Samsung", 
        "importance": "2", 
        "_typeGroup": "socialTag", 
        "originalValue": "Samsung", 
        "socialTag": "http://d.opencalais.com/genericHasher-1/97370f53-c2f8-31b8-bbcf-aa685e504714", 
        "forenduserdisplay": "true", 
        "id": "http://d.opencalais.com/dochash-1/8a7adab6-d07e-38e6-b0c9-5db5220336c1/SocialTag/4"
    }, 
    "http://d.opencalais.com/dochash-1/8a7adab6-d07e-38e6-b0c9-5db5220336c1/SocialTag/7": {
        "name": "Samsung Galaxy", 
        "importance": "2", 
        "_typeGroup": "socialTag", 
        "originalValue": "Samsung Galaxy", 
        "socialTag": "http://d.opencalais.com/genericHasher-1/64b8e664-bbdc-3731-b712-eb30990eab6f", 
        "forenduserdisplay": "true", 
        "id": "http://d.opencalais.com/dochash-1/8a7adab6-d07e-38e6-b0c9-5db5220336c1/SocialTag/7"
    }, 
    "http://d.opencalais.com/dochash-1/8a7adab6-d07e-38e6-b0c9-5db5220336c1/SocialTag/6": {
        "name": "Samsung Music Hub", 
        "importance": "2", 
        "_typeGroup": "socialTag", 
        "originalValue": "Samsung Music Hub", 
        "socialTag": "http://d.opencalais.com/genericHasher-1/a3310a01-ef6f-314e-90b2-a303822b965c", 
        "forenduserdisplay": "true", 
        "id": "http://d.opencalais.com/dochash-1/8a7adab6-d07e-38e6-b0c9-5db5220336c1/SocialTag/6"
    }
}

我注意到的是键都是超链接。无论如何,我想从输出中打印所有的 socialTags。为此,我编写了以下代码:

# Print all the social tags
for key, value in ast.literal_eval(output).items():
    if value["_typeGroup"] == 'socialTag':
        print value["name"]

但是,我收到此错误:

Traceback (most recent call last):
  File "opencal.py", line 30, in <module>
    if value["_typeGroup"] == 'socialTag':
KeyError: '_typeGroup'

这是什么错误?或者更准确地说,获取 socialTags 的正确方法是什么?谢谢。

4

0 回答 0