Skip to content

Instantly share code, notes, and snippets.

@ppham27
Last active April 1, 2024 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ppham27/5b98df5ad5caf704362b04e0a4ae2506 to your computer and use it in GitHub Desktop.
Save ppham27/5b98df5ad5caf704362b04e0a4ae2506 to your computer and use it in GitHub Desktop.
Negating Sentences

Attempt to negate a sentence.

Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"[nltk_data] Downloading package wordnet to\n",
"[nltk_data] /Users/phillypham/nltk_data...\n",
"[nltk_data] Package wordnet is already up-to-date!\n"
]
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import os\n",
"from delphin import ace\n",
"from delphin.codecs import simplemrs\n",
"import nltk\n",
"\n",
"nltk.download('wordnet')"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"ACE_PATH = os.path.join(os.environ['HOME'], 'delphin', 'ace')\n",
"ERG_PATH = os.path.join(os.environ['HOME'], 'delphin', 'erg-2018-osx-0.9.31.dat')\n",
"os.environ['PATH'] = f'{ACE_PATH}:os.environ[\"PATH\"]'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Parse sentence to find the verb phrase."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(['S',\n",
" ['NP', ['N', ['N', ['Aletheia']]]],\n",
" ['VP',\n",
" ['V', ['V', ['cares']]],\n",
" ['PP',\n",
" ['P', ['for']],\n",
" ['NP',\n",
" ['N',\n",
" ['N', ['N', ['N', ['people']]]],\n",
" ['PP',\n",
" ['P', ['in']],\n",
" ['NP',\n",
" ['DET', ['an']],\n",
" ['N', ['AP', ['abstract']], ['N', ['N', ['N', ['way']]]]]]]]]]]],\n",
" [<EP object (h4:proper_q(ARG0 x3, RSTR h5, BODY h6)) at 4694809096>,\n",
" <EP object (h7:named(CARG Aletheia, ARG0 x3)) at 4694808992>,\n",
" <EP object (h1:_care_v_for(ARG0 e2, ARG1 x3, ARG2 x9)) at 4694808784>,\n",
" <EP object (h10:udef_q(ARG0 x9, RSTR h11, BODY h12)) at 4694809824>,\n",
" <EP object (h13:_people_n_of(ARG0 x9, ARG1 i14)) at 4694809928>,\n",
" <EP object (h13:_in_p_loc(ARG0 e15, ARG1 x9, ARG2 x16)) at 4694810032>,\n",
" <EP object (h17:_a_q(ARG0 x16, RSTR h18, BODY h19)) at 4694810136>,\n",
" <EP object (h20:_abstract_a_1(ARG0 e21, ARG1 x16)) at 4694810240>,\n",
" <EP object (h20:_way_n_of(ARG0 x16, ARG1 i22)) at 4694810344>])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"TEXT = 'Aletheia cares for people in an abstract way'\n",
"with ace.ACEParser(ERG_PATH) as parser:\n",
" response = parser.interact(TEXT)\n",
"response.result(0).tree(), response.result(0).mrs().predications"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Append `does not` and lemmatize the verb."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Aletheia does not care for people in an abstract way'"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"for predication in response.result(0).mrs().predications:\n",
" if predication.label == 'h1':\n",
" break\n",
"predication\n",
"NEGATED_TEXT = (\n",
" TEXT[:predication.cfrom] +\n",
" 'does not ' +\n",
" nltk.stem.WordNetLemmatizer().lemmatize(TEXT[predication.cfrom:predication.cto]) +\n",
" TEXT[predication.cto:])\n",
"NEGATED_TEXT"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Generate paraphrases of the negation."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"with ace.ACEParser(ERG_PATH) as parser:\n",
" response = parser.interact(NEGATED_TEXT)\n",
"mrs = response.result(0).mrs()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Aletheia does not care for peoples in an abstract way.\n",
"Aletheia does not care for people in an abstract way.\n",
"Peoples in an abstract way are not cared for by Aletheia.\n",
"People in an abstract way are not cared for by Aletheia.\n",
"Aletheia does not care for peoples in an abstract way.\n",
"Aletheia does not care for people in an abstract way.\n",
"Peoples in an abstract way are not cared for by Aletheia.\n",
"People in an abstract way are not cared for by Aletheia.\n",
"Peoples in an abstract way are non cared for by Aletheia.\n",
"People in an abstract way are non cared for by Aletheia.\n",
"Aletheia doesn't care for peoples in an abstract way.\n",
"Aletheia doesn't care for people in an abstract way.\n",
"Peoples in an abstract way are not cared for by Aletheia.\n",
"People in an abstract way are not cared for by Aletheia.\n",
"Aletheia doesn't care for peoples in an abstract way.\n",
"Aletheia doesn't care for people in an abstract way.\n",
"Peoples in an abstract way aren't cared for by Aletheia.\n",
"People in an abstract way aren't cared for by Aletheia.\n",
"Peoples in an abstract way are non cared for by Aletheia.\n",
"People in an abstract way are non cared for by Aletheia.\n",
"Peoples in an abstract way are not cared for by Aletheia.\n",
"People in an abstract way are not cared for by Aletheia.\n",
"Aletheia does not care for peoples in an abstract way.\n",
"Aletheia does not care for people in an abstract way.\n",
"Peoples in an abstract way aren't cared for by Aletheia.\n",
"People in an abstract way aren't cared for by Aletheia.\n",
"Aletheia does not care for peoples in an abstract way.\n",
"Aletheia does not care for people in an abstract way.\n"
]
}
],
"source": [
"with ace.ACEGenerator(ERG_PATH) as generator:\n",
" response = generator.interact(simplemrs.encode(mrs))\n",
" for result in response.results(): \n",
" print(result['surface'])"
]
}
],
"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.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment