[FIXED] Rule based Classification based on a dictionary

Issue

Apologies in advance if this has already been questioned/answered, but I couldn't find any answer close to my problem. I am also somewhat new to Python, so sorry for any wrong formatting or wording. I am struggling with a task to label/classify a list of documents based on a list of dictionary. The list of dictionary looks like this:

| Category | Keywords       |
| -------- | -------------- |
| Reg      | A, B, C        |
| Gov      | D,E,F,G        |


Document1 = [Sentences1, Sentences 2,...]
Document2 = [Sentences3, Sentences 4,...]

I would like to label Documents per Category if keywords belong to this category appear in the document. I have searched through stackoverflow and github but have not found similar problem.


Solution

Your question seems to ask this:

  • For a collection of documents, each with multiple sentences, mark each document as belonging to one or more categories if, for any of the given categories, one or more sentences in the document contains at least one keyword from the category.

Here is a way to do that:

categories = {'Reg':['A', 'B', 'C'], 'Gov':['D', 'E', 'F', 'G']}
Document1 = ['A quick brown fox.', 'He got a B in calculus.']
Document2 = ['My next job will be in the C suite.', 'You can get there on the D train.']
docs = {'Doc1':Document1, 'Doc2':Document2}
docsByCategory = {}
for doc, sentences in docs.items():
    cats = set()
    for sentence in sentences:
        words = sentence.split()
        for category, keywords in categories.items():
            if any(keyword in words for keyword in keywords):
                cats.add(category)
    docsByCategory[doc] = list(cats)
print(docsByCategory)

Output:

{'Doc1': ['Reg'], 'Doc2': ['Gov', 'Reg']}

Explanation:

  • Iterate over the document names and contents using docs.items()
  • Iterate over the list of sentences the make up each document's contents
  • Split each sentence into words
  • Iterate over the category names and keywords using categories.items()
  • Use any() with a comprehension (loop) over keywords to see if any of them are found in the sentence's words, in which case add the category name to the variable cats which is a set, so that multiple calls to add() for the same category will result in only a single entry in cats for that category
  • Convert cats to a list and add it to the result dictionary docsByCategory for the current document name.

An alternative solution is this:

categories = {'Reg':['A', 'B', 'C'], 'Gov':['D', 'E', 'F', 'G']}
Document1 = ['A quick brown fox.', 'He got a B in calculus.']
Document2 = ['My next job will be in the C suite.', 'You can get there on the D train.']
docs = {'Doc1':Document1, 'Doc2':Document2}
docsByCategory = {}
for doc, sentences in docs.items():
    sentenceWordSets = [set(sentence.split()) for sentence in sentences]
    cats = set()
    for category, keywords in categories.items():
        keywordSet = set(keywords)
        if any(wordSet & keywordSet for wordSet in sentenceWordSets):
            cats.add(category)
    docsByCategory[doc] = list(cats)
print(docsByCategory)


Answered By - constantstranger
Answer Checked By - Dawn Plyler (Python.Easybugfix Volunteer)

Related Posts

Post a Comment