Finding related entities from three list2019 Community Moderator ElectionHow do I check if a list is empty?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow to randomly select an item from a list?How do you split a list into evenly sized chunks?How to make a flat list out of list of lists?How to get the number of elements in a list in Python?How to concatenate two lists in Python?How to clone or copy a list?How do I list all files of a directory?
How can I handle a player who pre-plans arguments about my rulings on RAW?
Was it really inappropriate to write a pull request for the company I interviewed with?
Relationship between the symmetry number of a molecule as used in rotational spectroscopy and point group
Practical reasons to have both a large police force and bounty hunting network?
Can I solder 12/2 Romex to extend wire 5 ft?
Why doesn't "adolescent" take any articles in "listen to adolescent agonising"?
When was drinking water recognized as crucial in marathon running?
Can a space-faring robot still function over a billion years?
PTIJ: Mordechai mourning
Should I use HTTPS on a domain that will only be used for redirection?
Can an earth elemental drown/bury its opponent underground using earth glide?
Why do phishing e-mails use faked e-mail addresses instead of the real one?
Plagiarism of code by other PhD student
Wardrobe above a wall with fuse boxes
How to fix my table, centering of columns
Find maximum of the output from reduce
Is every open circuit a capacitor?
How do I deal with being envious of my own players?
function only contains jump discontinuity but is not piecewise continuous
Can a Trickery Domain cleric cast a spell through the Invoke Duplicity clone while inside a Forcecage?
Why did the Cray-1 have 8 parity bits per word?
It doesn't matter the side you see it
Called into a meeting and told we are being made redundant (laid off) and "not to share outside". Can I tell my partner?
A bug in Excel? Conditional formatting for marking duplicates also highlights unique value
Finding related entities from three list
2019 Community Moderator ElectionHow do I check if a list is empty?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow to randomly select an item from a list?How do you split a list into evenly sized chunks?How to make a flat list out of list of lists?How to get the number of elements in a list in Python?How to concatenate two lists in Python?How to clone or copy a list?How do I list all files of a directory?
I have three lists that contain the following data:
Entities: ['Ashraf', 'Afghanistan', 'Afghanistan', 'Kabul']
Relations: ['Born', 'President', 'employee', 'Capital', 'Located', 'Lecturer', 'University']
sentence_list: ['Ashraf','Born', 'in', 'Kabul', '.' 'Ashraf', 'is', 'the', 'president', 'of', 'Afghanistan', '.', ...]
As the sentence_list
is a list of sentences. In each sentence, I want to check if any word of Entities
and Relations
, the combination of specific words should be added in another list. such as, (Ashraf, born, Kabul
) in the first sentence.
What I did:
First incomplete Solution:
# read file
with open('../data/parse.txt', 'r') as myfile:
json_data = json.load(myfile)
for i in range(len(json_data)): # the dataset was in json format
if json_data[i]['word'] in relation(json_data)[0]: # I extract the relations
print(json_data[i]['word'])
if json_data[i]['word'] in entities(json_data)[0]:
print(json[i]['word'])
The output: (Ashraf, Born, Ashraf)
, where I want (Ashraf, Born, Kabul)
The next incomplete solution: I stored json_data
to a list and then I did this:
json_data2 = []
for i in range(len(json_data)):
json2_data.append(json_data[i]['word'])
print(json_data2)
'''
Now I tried if I can find any element of `Entities` list and `Relations` list
in each sentence of `sentence_list`. And then it should store matched
entities and relations based on sentence to a list. '''
for line in json_data2:
for rel in relation(obj):
for ent in entities(obj):
match = re.findall(rel, line['word'])
if match:
print('word matched relations: %s ==> word: %s' % (rel, line['address']))
match2 = re.findall(ent, line['word'])
if match2:
print('word matched entities: %s ==> word: %s' % (ent, line['address']))
Unfortunately, does not work?
python list
add a comment |
I have three lists that contain the following data:
Entities: ['Ashraf', 'Afghanistan', 'Afghanistan', 'Kabul']
Relations: ['Born', 'President', 'employee', 'Capital', 'Located', 'Lecturer', 'University']
sentence_list: ['Ashraf','Born', 'in', 'Kabul', '.' 'Ashraf', 'is', 'the', 'president', 'of', 'Afghanistan', '.', ...]
As the sentence_list
is a list of sentences. In each sentence, I want to check if any word of Entities
and Relations
, the combination of specific words should be added in another list. such as, (Ashraf, born, Kabul
) in the first sentence.
What I did:
First incomplete Solution:
# read file
with open('../data/parse.txt', 'r') as myfile:
json_data = json.load(myfile)
for i in range(len(json_data)): # the dataset was in json format
if json_data[i]['word'] in relation(json_data)[0]: # I extract the relations
print(json_data[i]['word'])
if json_data[i]['word'] in entities(json_data)[0]:
print(json[i]['word'])
The output: (Ashraf, Born, Ashraf)
, where I want (Ashraf, Born, Kabul)
The next incomplete solution: I stored json_data
to a list and then I did this:
json_data2 = []
for i in range(len(json_data)):
json2_data.append(json_data[i]['word'])
print(json_data2)
'''
Now I tried if I can find any element of `Entities` list and `Relations` list
in each sentence of `sentence_list`. And then it should store matched
entities and relations based on sentence to a list. '''
for line in json_data2:
for rel in relation(obj):
for ent in entities(obj):
match = re.findall(rel, line['word'])
if match:
print('word matched relations: %s ==> word: %s' % (rel, line['address']))
match2 = re.findall(ent, line['word'])
if match2:
print('word matched entities: %s ==> word: %s' % (ent, line['address']))
Unfortunately, does not work?
python list
add a comment |
I have three lists that contain the following data:
Entities: ['Ashraf', 'Afghanistan', 'Afghanistan', 'Kabul']
Relations: ['Born', 'President', 'employee', 'Capital', 'Located', 'Lecturer', 'University']
sentence_list: ['Ashraf','Born', 'in', 'Kabul', '.' 'Ashraf', 'is', 'the', 'president', 'of', 'Afghanistan', '.', ...]
As the sentence_list
is a list of sentences. In each sentence, I want to check if any word of Entities
and Relations
, the combination of specific words should be added in another list. such as, (Ashraf, born, Kabul
) in the first sentence.
What I did:
First incomplete Solution:
# read file
with open('../data/parse.txt', 'r') as myfile:
json_data = json.load(myfile)
for i in range(len(json_data)): # the dataset was in json format
if json_data[i]['word'] in relation(json_data)[0]: # I extract the relations
print(json_data[i]['word'])
if json_data[i]['word'] in entities(json_data)[0]:
print(json[i]['word'])
The output: (Ashraf, Born, Ashraf)
, where I want (Ashraf, Born, Kabul)
The next incomplete solution: I stored json_data
to a list and then I did this:
json_data2 = []
for i in range(len(json_data)):
json2_data.append(json_data[i]['word'])
print(json_data2)
'''
Now I tried if I can find any element of `Entities` list and `Relations` list
in each sentence of `sentence_list`. And then it should store matched
entities and relations based on sentence to a list. '''
for line in json_data2:
for rel in relation(obj):
for ent in entities(obj):
match = re.findall(rel, line['word'])
if match:
print('word matched relations: %s ==> word: %s' % (rel, line['address']))
match2 = re.findall(ent, line['word'])
if match2:
print('word matched entities: %s ==> word: %s' % (ent, line['address']))
Unfortunately, does not work?
python list
I have three lists that contain the following data:
Entities: ['Ashraf', 'Afghanistan', 'Afghanistan', 'Kabul']
Relations: ['Born', 'President', 'employee', 'Capital', 'Located', 'Lecturer', 'University']
sentence_list: ['Ashraf','Born', 'in', 'Kabul', '.' 'Ashraf', 'is', 'the', 'president', 'of', 'Afghanistan', '.', ...]
As the sentence_list
is a list of sentences. In each sentence, I want to check if any word of Entities
and Relations
, the combination of specific words should be added in another list. such as, (Ashraf, born, Kabul
) in the first sentence.
What I did:
First incomplete Solution:
# read file
with open('../data/parse.txt', 'r') as myfile:
json_data = json.load(myfile)
for i in range(len(json_data)): # the dataset was in json format
if json_data[i]['word'] in relation(json_data)[0]: # I extract the relations
print(json_data[i]['word'])
if json_data[i]['word'] in entities(json_data)[0]:
print(json[i]['word'])
The output: (Ashraf, Born, Ashraf)
, where I want (Ashraf, Born, Kabul)
The next incomplete solution: I stored json_data
to a list and then I did this:
json_data2 = []
for i in range(len(json_data)):
json2_data.append(json_data[i]['word'])
print(json_data2)
'''
Now I tried if I can find any element of `Entities` list and `Relations` list
in each sentence of `sentence_list`. And then it should store matched
entities and relations based on sentence to a list. '''
for line in json_data2:
for rel in relation(obj):
for ent in entities(obj):
match = re.findall(rel, line['word'])
if match:
print('word matched relations: %s ==> word: %s' % (rel, line['address']))
match2 = re.findall(ent, line['word'])
if match2:
print('word matched entities: %s ==> word: %s' % (ent, line['address']))
Unfortunately, does not work?
python list
python list
edited 11 hours ago
yatu
12.4k31341
12.4k31341
asked 18 hours ago
user2988094user2988094
199
199
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You could use the following list comprehension:
to_match = set(Entities+Relations)
l = [j for j in to_match if j in i
for i in ' '.join(sentence_list).split('.')[:-1]]
Output
['Ashraf', 'Born', 'Kabul', 'Afghanistan', 'Ashraf']
Note that I'm, returning a list of sets
to avoid duplicate values, given that for instance in Entities
Afghanistan
appears twice.
Useful reads:
List comprehensions
sets — Unordered collections of unique elements
string methods
Thank you, but in the second or more index of the list, it takes repeated elements fromEntities list
. As you defined each sentence by.split('.')
but it should select from both'Entities and Relations'
based on its existence in thesentence-list
. How to fix that?
– user2988094
18 hours ago
IIUC the update should fix that
– yatu
18 hours ago
Thank you very much. It's solved by usingset
.
– user2988094
18 hours ago
1
I upvoted, thank you again
– user2988094
17 hours ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55021537%2ffinding-related-entities-from-three-list%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could use the following list comprehension:
to_match = set(Entities+Relations)
l = [j for j in to_match if j in i
for i in ' '.join(sentence_list).split('.')[:-1]]
Output
['Ashraf', 'Born', 'Kabul', 'Afghanistan', 'Ashraf']
Note that I'm, returning a list of sets
to avoid duplicate values, given that for instance in Entities
Afghanistan
appears twice.
Useful reads:
List comprehensions
sets — Unordered collections of unique elements
string methods
Thank you, but in the second or more index of the list, it takes repeated elements fromEntities list
. As you defined each sentence by.split('.')
but it should select from both'Entities and Relations'
based on its existence in thesentence-list
. How to fix that?
– user2988094
18 hours ago
IIUC the update should fix that
– yatu
18 hours ago
Thank you very much. It's solved by usingset
.
– user2988094
18 hours ago
1
I upvoted, thank you again
– user2988094
17 hours ago
add a comment |
You could use the following list comprehension:
to_match = set(Entities+Relations)
l = [j for j in to_match if j in i
for i in ' '.join(sentence_list).split('.')[:-1]]
Output
['Ashraf', 'Born', 'Kabul', 'Afghanistan', 'Ashraf']
Note that I'm, returning a list of sets
to avoid duplicate values, given that for instance in Entities
Afghanistan
appears twice.
Useful reads:
List comprehensions
sets — Unordered collections of unique elements
string methods
Thank you, but in the second or more index of the list, it takes repeated elements fromEntities list
. As you defined each sentence by.split('.')
but it should select from both'Entities and Relations'
based on its existence in thesentence-list
. How to fix that?
– user2988094
18 hours ago
IIUC the update should fix that
– yatu
18 hours ago
Thank you very much. It's solved by usingset
.
– user2988094
18 hours ago
1
I upvoted, thank you again
– user2988094
17 hours ago
add a comment |
You could use the following list comprehension:
to_match = set(Entities+Relations)
l = [j for j in to_match if j in i
for i in ' '.join(sentence_list).split('.')[:-1]]
Output
['Ashraf', 'Born', 'Kabul', 'Afghanistan', 'Ashraf']
Note that I'm, returning a list of sets
to avoid duplicate values, given that for instance in Entities
Afghanistan
appears twice.
Useful reads:
List comprehensions
sets — Unordered collections of unique elements
string methods
You could use the following list comprehension:
to_match = set(Entities+Relations)
l = [j for j in to_match if j in i
for i in ' '.join(sentence_list).split('.')[:-1]]
Output
['Ashraf', 'Born', 'Kabul', 'Afghanistan', 'Ashraf']
Note that I'm, returning a list of sets
to avoid duplicate values, given that for instance in Entities
Afghanistan
appears twice.
Useful reads:
List comprehensions
sets — Unordered collections of unique elements
string methods
edited 18 hours ago
answered 18 hours ago
yatuyatu
12.4k31341
12.4k31341
Thank you, but in the second or more index of the list, it takes repeated elements fromEntities list
. As you defined each sentence by.split('.')
but it should select from both'Entities and Relations'
based on its existence in thesentence-list
. How to fix that?
– user2988094
18 hours ago
IIUC the update should fix that
– yatu
18 hours ago
Thank you very much. It's solved by usingset
.
– user2988094
18 hours ago
1
I upvoted, thank you again
– user2988094
17 hours ago
add a comment |
Thank you, but in the second or more index of the list, it takes repeated elements fromEntities list
. As you defined each sentence by.split('.')
but it should select from both'Entities and Relations'
based on its existence in thesentence-list
. How to fix that?
– user2988094
18 hours ago
IIUC the update should fix that
– yatu
18 hours ago
Thank you very much. It's solved by usingset
.
– user2988094
18 hours ago
1
I upvoted, thank you again
– user2988094
17 hours ago
Thank you, but in the second or more index of the list, it takes repeated elements from
Entities list
. As you defined each sentence by .split('.')
but it should select from both 'Entities and Relations'
based on its existence in the sentence-list
. How to fix that?– user2988094
18 hours ago
Thank you, but in the second or more index of the list, it takes repeated elements from
Entities list
. As you defined each sentence by .split('.')
but it should select from both 'Entities and Relations'
based on its existence in the sentence-list
. How to fix that?– user2988094
18 hours ago
IIUC the update should fix that
– yatu
18 hours ago
IIUC the update should fix that
– yatu
18 hours ago
Thank you very much. It's solved by using
set
.– user2988094
18 hours ago
Thank you very much. It's solved by using
set
.– user2988094
18 hours ago
1
1
I upvoted, thank you again
– user2988094
17 hours ago
I upvoted, thank you again
– user2988094
17 hours ago
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55021537%2ffinding-related-entities-from-three-list%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown