{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-10T12:59:50.614108Z",
     "start_time": "2019-09-10T12:59:49.504907Z"
    }
   },
   "outputs": [],
   "source": [
    "from nltk.corpus import wordnet as wn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-10T13:07:33.697721Z",
     "start_time": "2019-09-10T13:07:33.684194Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n",
      "['便携式计算器', '加数器', '加法器', '加法器', '加法机', '加法计算器', '手摇计算器', '算术计算机', '算盘', '计数器', '计算机']\n",
      "['家用电脑', '家用计算机', '数字计算机', '模拟计算机', '网站', '网络站点']\n",
      "**********\n"
     ]
    }
   ],
   "source": [
    "def synonyms(word, lang):\n",
    "    for synset in wn.synsets(word, lang=lang):\n",
    "        types_of_computer = synset.hyponyms()\n",
    "        print(sorted([lemma.name() for synset in types_of_computer for lemma in synset.lemmas('cmn')]))\n",
    "\n",
    "synonyms('计算机', 'cmn')\n",
    "print('*'*10)\n",
    "synonyms('计算机', 'qcn')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-10T13:01:06.202978Z",
     "start_time": "2019-09-10T13:01:06.191883Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "加数器\n",
      "加法器\n",
      "加法机\n",
      "加法计算器\n",
      "算术计算机\n",
      "计算机\n",
      "计算器\n",
      "计算机\n",
      "电子计算机\n",
      "电脑\n",
      "计算机\n"
     ]
    }
   ],
   "source": [
    "for synset in wn.synsets(u'计算机', lang='cmn'):\n",
    "    for lemma in synset.lemma_names('cmn'):\n",
    "        print(lemma)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-10T13:01:47.993154Z",
     "start_time": "2019-09-10T13:01:47.985086Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.25\n"
     ]
    }
   ],
   "source": [
    "# 相似度\n",
    "select = wn.synsets(u'选择', lang='cmn')[0]\n",
    "selectn3= wn.synsets(u'找出', lang='cmn')[0]\n",
    "print(select.path_similarity(selectn3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-10T13:03:03.519783Z",
     "start_time": "2019-09-10T13:03:03.508322Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Synset('canine.n.02'), Synset('domestic_animal.n.01')]\n",
      "[Synset('basenji.n.01'), Synset('corgi.n.01'), Synset('cur.n.01'), Synset('dalmatian.n.02'), Synset('great_pyrenees.n.01'), Synset('griffon.n.02'), Synset('hunting_dog.n.01'), Synset('lapdog.n.01'), Synset('leonberg.n.01'), Synset('mexican_hairless.n.01'), Synset('newfoundland.n.01'), Synset('pooch.n.01'), Synset('poodle.n.01'), Synset('pug.n.01'), Synset('puppy.n.01'), Synset('spitz.n.01'), Synset('toy_dog.n.01'), Synset('working_dog.n.01')]\n"
     ]
    }
   ],
   "source": [
    "dog = wn.synset('dog.n.01')  #　狗的概念\n",
    "print(dog.hypernyms()) #　狗的父类（上位词）\n",
    "print(dog.hyponyms())  #　狗的子类（下位词）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-10T13:04:43.930551Z",
     "start_time": "2019-09-10T13:04:43.923215Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Synset('flag.n.07')] [] [Synset('canis.n.01'), Synset('pack.n.06')]\n"
     ]
    }
   ],
   "source": [
    "print(dog.part_meronyms(),\n",
    "      dog.substance_meronyms(),\n",
    "      dog.member_holonyms()\n",
    "     )"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
