Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relation creation between two entities #289

Open
bschembri-UoM opened this issue Jun 19, 2023 · 2 comments
Open

Relation creation between two entities #289

bschembri-UoM opened this issue Jun 19, 2023 · 2 comments

Comments

@bschembri-UoM
Copy link

Do you have any guidelines how to create a relation between two entities.
The entities all ready exist in the cas I am creating some relations between existing named entities based on some logic.
Have tried the bellow but I am not able to make it work;

`
Relation = typesystem.get_type('webanno.custom.Relation')
NamedEntity = typesystem.get_type('de.tudarmstadt.ukp.dkpro.core.api.ner.type.NamedEntity')

for i in range(1, maxSent + 1):
    # Let's find RESTCHAIN (subject) sells (object) PROD (predicate) in each sentence
    df_subj = entities.df_entities.query("SentRef == @i & NamedEntity == 'RESTCHAIN'")
    if (df_subj.shape[0]) > 0:
        df_pred = entities.df_entities.query("SentRef == @i & NamedEntity == 'PROD'")
        if df_pred.shape[0] > 0:
            for index_subj, row_subj in df_subj.iterrows():
                for index_pred, row_pred in df_pred.iterrows():
                    namedentity = NamedEntity().get(str(row_pred['id']))
                    relation = Relation(begin=str(row_pred['Begin']), end=str(row_pred['End']), Dependent=namedentity,
                                        Governor=str(row_subj['id']), Feature="sells")
                    cas.add(relation)

                    print(f"{row_subj['EntityText']} at {row_subj['Begin']}, {row_subj['End']} "
                          f"sells "
                          f"{row_pred['EntityText']} at {row_pred['Begin']}, {row_pred['End']}")

cas.to_xmi(config.xmi_path)

`

@bschembri-UoM
Copy link
Author

I managed to create the relations between entities.
I am not sure if there is a better way to do this however this is what I did;

df_ent = pd.DataFrame()
for ent in cas.select('de.tudarmstadt.ukp.dkpro.core.api.ner.type.NamedEntity'):
    print(ent)
    df_row = pd.DataFrame([{'id': ent.xmiID, 'ent': ent}])
    df_ent = pd.concat([df_ent, df_row])

Relation = typesystem.get_type('webanno.custom.Relation')
NamedEntity = typesystem.get_type('de.tudarmstadt.ukp.dkpro.core.api.ner.type.NamedEntity')

for i in range(1, maxSent + 1):
    # Let's find RESTCHAIN (subject) sells (object) PROD (predicate) in each sentence
    df_subj = entities.df_entities.query("SentRef == @i & NamedEntity == 'RESTCHAIN'")
    if (df_subj.shape[0]) > 0:
        df_pred = entities.df_entities.query("SentRef == @i & NamedEntity == 'PROD'")
        if df_pred.shape[0] > 0:
            for index_subj, row_subj in df_subj.iterrows():
                for index_pred, row_pred in df_pred.iterrows():
                    dependententity = df_ent.loc[df_ent['id'] == row_pred['id']].ent.item()
                    governorententity = df_ent.loc[df_ent['id'] == row_subj['id']].ent.item()
                    relation = Relation(begin=dependententity['begin'], end=dependententity['end'], Dependent=dependententity,
                                        Governor=governorententity, Feature="sells")
                    cas.add(relation)

                    print(f"{row_subj['EntityText']} at {row_subj['Begin']}, {row_subj['End']} "
                          f"sells "
                          f"{row_pred['EntityText']} at {row_pred['Begin']}, {row_pred['End']}")

cas.to_xmi(config.xmi_path)`

@reckart
Copy link
Member

reckart commented Jun 19, 2023

relation = Relation(begin=dependententity['begin'], end=dependententity['end'], Dependent=dependententity,
                                        Governor=governorententity, Feature="sells")

Looks good to me.

The rest of the code basically depends on the data you are processing, so I don't have any comments on that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants