How I can define function to integrate lists using python?

Ahmad Alismail

I am trying to define a function that iterates through three kind of list of lists, each list has the following structure:

sentiment_labels =  [['B_S', 'O', 'O', 'O'], ['O', 'O', 'B_S', 'O']]
aspect_labels =     [['O', 'B_A', 'O', 'O'], ['O', 'O', 'O', 'B_A']]
modifier_labels =   [['O', 'O', 'BM', 'O'], ['O', 'O', 'O', 'O']]

# those lists contain 'B_A', 'I_S', 'B_S', 'I_S', 'BM', 'IM', and 'O' labels (IOB Tagging)

the target result must be like:

labels = [['B_S', 'B_A', 'BM', 'O'], ['O', 'O', 'B_S', 'B_A'] ]

For this purpose, I have defined the following function:

# define function to integrate sentiments, aspects, and modifiers lists

def integrate_labels(sentiments_lists, aspects_lists, modifiers_lists):
  all_labels = []
  integrated_label = []

  # iterate through each list, then iterate through each element 
  for sentiment_list, aspect_list, modifier_list in zip(sentiments_lists, aspects_lists, modifiers_lists):
    
    for sentiment, aspect, modifier in zip(sentiment_list, aspect_list, modifier_list):

      # if the element is a sentiment label append it to the integrated_label list
      if sentiment != 'O':
        integrated_label.append(sentiment)

      # if the element is an aspect label append it to the integrated_label list
      elif aspect != 'O':
        integrated_label.append(aspect)

      # if the element is a modifier label append it to the integrated_label list
      elif modifier != 'O':
        integrated_label.append(modifier)

      else:
        integrated_label.append('O')
        
    # now append each integrated_label list to all_labels list
    all_labels.append(integrated_label)
  
  return all_labels

But I have this result:

integrate_labels(sentiment_labels, aspect_labels, modifier_labels)

[['B_S', 'B_A', 'BM', 'O', 'O', 'O', 'B_S', 'B_A'],
 ['B_S', 'B_A', 'BM', 'O', 'O', 'O', 'B_S', 'B_A']]

How I could change integrate_labels function to get the target result ?

Thanks in advance!

Yehonatan harmatz

change to

# now append each integrated_label list to all_labels list
all_labels.append(integrated_label.copy())
integrated_label = []

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how i can define a function in python 3

How can i define a function that reads and returns all values in the list using Python?

How can I define a function using unkown number of variables?

How can i define a shared list in the recursive function in python?

How can I define a function called "import" in Python?

How do I define a function in Python using a "with open" logic?

How can you filter between two lists using a function in Python?

How can I integrate Python mido and asyncio?

How can I integrate xgboost in spark? (Python)

How can I define a javascript variable on a webpage in selenium using Python?

How can I define default values using context manger in Python

How can I define a paramter in a function.

How can I make for loop of #define function?

How can I define `unlines` function generically?

How can I define a function in JavaScript?

How can I define a function of type Integral a => a?

How can I define a function with this in Haskell?

Can I define a function inside print() function or regular if-else statements & not using Ternary Operators in Python?

How can i use define preprocessor to define function-pointer?

How can I define a widget using javascript

How can I define a class in Python?

How can I define multidimensional arrays in python?

How can I integrate spark streaming and kafka using broadcast variable?

How can I integrate complexity verification while using a PasswordBox?

How can I integrate Jacoco reports with SonarQube without using maven?

Using inversify-express-utils, how can i integrate websockets?

How can I define Connection string in Azure function using Visual Studio 2015?

How do i integrate a Python Lambda Function into the Pipeline of AWS Amplify

How can I have references of lists in Python

TOP Ranking

HotTag

Archive