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?










2















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?










share|improve this question




























    2















    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?










    share|improve this question


























      2












      2








      2








      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?










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 11 hours ago









      yatu

      12.4k31341




      12.4k31341










      asked 18 hours ago









      user2988094user2988094

      199




      199






















          1 Answer
          1






          active

          oldest

          votes


















          1














          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






          share|improve this answer

























          • 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











          • Thank you very much. It's solved by using set.

            – user2988094
            18 hours ago






          • 1





            I upvoted, thank you again

            – user2988094
            17 hours ago










          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
          );



          );













          draft saved

          draft discarded


















          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









          1














          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






          share|improve this answer

























          • 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











          • Thank you very much. It's solved by using set.

            – user2988094
            18 hours ago






          • 1





            I upvoted, thank you again

            – user2988094
            17 hours ago















          1














          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






          share|improve this answer

























          • 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











          • Thank you very much. It's solved by using set.

            – user2988094
            18 hours ago






          • 1





            I upvoted, thank you again

            – user2988094
            17 hours ago













          1












          1








          1







          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






          share|improve this answer















          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







          share|improve this answer














          share|improve this answer



          share|improve this answer








          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 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











          • Thank you very much. It's solved by using set.

            – 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












          • IIUC the update should fix that

            – yatu
            18 hours ago











          • Thank you very much. It's solved by using set.

            – 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



















          draft saved

          draft discarded
















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

          Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

          2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived