Package: conceptnet-lite
conceptnet-lite [maintainance mode]Conceptnet-lite is a Python library for working with ConceptNet offline without the need for PostgreSQL.The library comes with Apache License 2.0, and is separate from ConceptNet itself. The ConceptNet is available underCC-BY-SA-4.0license, which also applies to the formatted database file that we provide. Seeherefor the list of conditions for using ConceptNet data.This is the official citation for ConceptNet if you use it in research:Robyn Speer, Joshua Chin, and Catherine Havasi. 2017. "ConceptNet 5.5: An Open Multilingual Graph of General Knowledge." In proceedings of AAAI 31.StatusConceptNet was not updated since September 2021, so Conceptnet-lite won't be updated either. Please do not use it in any new projects.InstallationTo installconceptnet-liteusepip:$pipinstallconceptnet-liteConnecting to the databaseBefore you can useconceptnet-lite, you will need to obtain ConceptNet dabase file. You have two options: download pre-made one or build it yourself from the raw ConceptNet assertions file.Downloading the ConceptNet databaseConceptNet releases happen once a year. You can useconceptnet-liteto build your own database from the raw assertions file (see below), but if there is a pre-built file it will be faster to just get that one.conceptnet-litecan download and unpack it to the specified folder automatically.Here is alinkto a compressed database for ConceptNet 5.7. This link is used automatically if you do not supply the alternative.importconceptnet_liteconceptnet_lite.connect("/path/to/conceptnet.db")This command both downloads the resource (our build for ConceptNet 5.7) and connects to the database. If path specified as the first argument does not exist, it will be created (unless there is a permissions problem). Note that the database file is quite large (over 9 Gb).If your internet connection is intermittent, the built-in download function may give you errors. If so, just download the file separately, unpack it to the directory of your choice and provide the path to the.connect()method as described below.Building the database for a new release.If a database file is not found in the folder specified in thedb_pathargument,conceptnet-litewill attempt to automatically download the raw assertions file fromhereand build the database. This takes a couple of hours, so we recommend getting the pre-built file.If you provide a path, this is where the database will be built. Note that the database file is quite large (over 9 Gb). Note that you need to passdb_download_url=Noneto force the library build the database from dump.importconceptnet_liteconceptnet_lite.connect("/path/to/conceptnet.db",db_download_url=None)If the specified does not exist, it will be created (unless there is a permissions problem). If no path is specified, and no database file is not found in the current working directory,conceptnet-litewill attempt to build one in the current working directory.Once the database is built,conceptnet-litewill connect to it automatically.Loading the ConceptNet databaseOnce you have the database file, all you need to do is to pass the path to it to the.connect()method.importconceptnet_liteconceptnet_lite.connect("/path/to/conceptnet.db")If no path is specified,conceptnet-litewill check if a database file exists in the current working directory. If it is not found, it will trigger the process of downloading the pre-built database (see above).Accessing conceptsConcepts objects are created by looking for every entry that matches the input string exactly.
If none is found, thepeewee.DoesNotExistexception will be raised.fromconceptnet_liteimportLabelcat_concepts=Label.get(text='cat').conceptsforcincat_concepts:print("    Concept URI:",c.uri)print("    Concept text:",c.text)Concept URI: /c/en/catConcept text: catConcept URI: /c/en/cat/nConcept text: catConcept URI: /c/en/cat/n/wn/animalConcept text: catConcept URI: /c/en/cat/n/wn/person...concept.uriprovides access to ConceptNet URIs, as describedhere. You can also retrieve only the text of the entry byconcept.text.Working with languagesYou can limit the languages to search for matches. Label.get() takes an optionallanguageattribute that is expected to be an instanceLanguage, which in turn is created by callingLanguage.get()withnameargument.
List of available languages and their codes are describedhere.fromconceptnet_liteimportLabelcat_concepts=Label.get(text='cat',language='en').conceptsforcincat_concepts:print("    Concept URI:",c.uri)print("    Concept text:",c.text)print("    Concept language:",c.language.name)Concept URI: /c/en/catConcept text: catConcept language: enConcept URI: /c/en/cat/nConcept text: catConcept language: enConcept URI: /c/en/cat/n/wn/animalConcept text: catConcept language: enConcept URI: /c/en/cat/n/wn/personConcept text: catConcept language: en...Querying edges between conceptsTo retrieve the set of relations between two concepts, you need to create the concept objects (optionally specifying the language as described above).cn.edges_between()method retrieves all edges between the specified concepts. You can access its URI and a number of attributes, as shown below.Some ConceptNet relations are symmetrical: for example, the antonymy betweenwhiteandblackworks both ways. Some relations are asymmetrical: e.g. the relation betweencatandmammalis either hyponymy or hyperonymy, depending on the direction. Thetwo_wayargument lets you choose whether the query should be symmetrical or not.fromconceptnet_liteimportLabel,edges_betweenintrovert_concepts=Label.get(text='introvert',language='en').conceptsextrovert_concepts=Label.get(text='extrovert',language='en').conceptsforeinedges_between(introvert_concepts,extrovert_concepts,two_way=False):print("  Edge URI:",e.uri)print("  Edge name:",e.relation.name)print("  Edge start node:",e.start.text)print("  Edge end node:",e.end.text)print("  Edge metadata:",e.etc)Edge URI: /a/[/r/antonym/,/c/en/introvert/n/,/c/en/extrovert/]Edge name: antonymEdge start node: introvertEdge end node: extrovertEdge metadata: {'dataset': '/d/wiktionary/en', 'license': 'cc:by-sa/4.0', 'sources': [{'contributor': '/s/resource/wiktionary/en', 'process': '/s/process/wikiparsec/2'}, {'contributor': '/s/resource/wiktionary/fr', 'process': '/s/process/wikiparsec/2'}], 'weight': 2.0}e.relation.name: the name of ConceptNet relation. Full listhere.e.start.text, e.end.text: the source and the target concepts in the edgee.etc: the ConceptNetmetadatadictionary contains the source dataset, sources, weight, and license. For example, the introvert:extrovert edge for English contains the following metadata:{"dataset":"/d/wiktionary/en","license":"cc:by-sa/4.0","sources":[{"contributor":"/s/resource/wiktionary/en","process":"/s/process/wikiparsec/2"},{"contributor":"/s/resource/wiktionary/fr","process":"/s/process/wikiparsec/2"}],"weight":2.0}Accessing all relations for a given conceptsYou can also retrieve all relations between a given concepts and all other concepts, with the same options as above:fromconceptnet_liteimportLabel,edges_forforeinedges_for(Label.get(text='introvert',language='en').concepts,same_language=True):print(e.start.text,"::",e.end.text,"|",e.relation.name)extrovert :: introvert | antonymintrovert :: extrovert | antonymoutrovert :: introvert | antonymreflection :: introvert | at_locationintroverse :: introvert | derived_fromintroversible :: introvert | derived_fromintroversion :: introvert | derived_fromintroversion :: introvert | derived_fromintroversive :: introvert | derived_fromintroverted :: introvert | derived_from...The same set of edge attributes are available foredges_betweenandedges_for(e.uri, e.relation.name, e.start.text, e.end.text, e.etc).Note that we have used optional argumentsame_language=True. By supplying this argument we makeedges_forreturn
relations, both ends of which are in the same language. If this argument is skipped it is possible to get edges to
concepts in languages other than the source concepts language. For example, the same command as above withsame_language=Falsewill include the following in the output:kääntyä_sisäänpäin :: introvert | synonymsulkeutua :: introvert | synonymsulkeutunut :: introvert | synonymintroverti :: introvert | synonymasociale :: introvert | synonymintroverso :: introvert | synonymintrovertito :: introvert | synonym内向 :: introvert | synonymAccessing concept edges with a given relation directionYou can also query the relations that have a specific concept as target or source. This is achieved withconcept.edges_outandconcept.edges_in, as follows:fromconceptnet_liteimportLabelconcepts=Label.get(text='introvert',language='en').conceptsforcinconcepts:print("    Concept text:",c.text)ifc.edges_out:print("      Edges out:")foreinc.edges_out:print("        Edge URI:",e.uri)print("        Relation:",e.relation.name)print("        End:",e.end.text)ifc.edges_in:print("      Edges in:")foreinc.edges_in:print("        Edge URI:",e.uri)print("        Relation:",e.relation.name)print("        End:",e.end.text)Concept text: introvertEdges out:Edge URI: /a/[/r/etymologically_derived_from/,/c/en/introvert/,/c/la/introvertere/]Relation: etymologically_derived_fromEnd: introvertere...Edges in:Edge URI: /a/[/r/antonym/,/c/cs/extrovert/n/,/c/en/introvert/]Relation: antonymEnd: introvert...Traversing all the data for a languageYou can go over all concepts for a given language. For illustration, let us try Old Norse, a "small" language with the code "non" and vocab size of 7868, according to theConceptNet language statistics.fromconceptnet_liteimportLanguagemylanguage=Language.get(name='non')forlinmylanguage.labels:print("  Label:",l.text)forcinl.concepts:print("    Concept URI:",c.uri)ifc.edges_out:print("      Edges out:")foreinc.edges_out:print("        Edge URI:",e.uri)ifc.edges_in:print("      Edges in:")foreinc.edges_in:print("        Edge URI:",e.uri)Label: andsœlisConcept URI: /c/non/andsœlis/rEdges out:Edge URI: /a/[/r/antonym/,/c/non/andsœlis/r/,/c/non/réttsœlis/]Edge URI: /a/[/r/related_to/,/c/non/andsœlis/r/,/c/en/against/]Edge URI: /a/[/r/related_to/,/c/non/andsœlis/r/,/c/en/course/]Edge URI: /a/[/r/related_to/,/c/non/andsœlis/r/,/c/en/sun/]Edge URI: /a/[/r/related_to/,/c/non/andsœlis/r/,/c/en/widdershins/]Edge URI: /a/[/r/synonym/,/c/non/andsœlis/r/,/c/non/rangsœlis/]Concept URI: /c/non/andsœlisEdges out:Edge URI: /a/[/r/external_url/,/c/non/andsœlis/,/c/en.wiktionary.org/wiki/andsœlis/]Label: réttsœlisConcept URI: /c/non/réttsœlisEdges in:Edge URI: /a/[/r/antonym/,/c/non/andsœlis/r/,/c/non/réttsœlis/]...Accessing Concepts by URIYou can access concept ORM objects directly by providing a desired ConceptNet URI. This is done as follows:fromconceptnet_liteimportConceptedge_object=Edge.get(start='/c/en/example')concept_object=Concept.get(uri='/c/en/example')