Mrrrr's Forum (VIEW ONLY)
Un forum care ofera solutii pentru unele probleme legate in general de PC. Pe langa solutii, aici puteti gasi si alte lucruri interesante // A forum that offers solutions to some PC related issues. Besides these, here you can find more interesting stuff.
Lista Forumurilor Pe Tematici
Mrrrr's Forum (VIEW ONLY) | Reguli | Inregistrare | Login

POZE MRRRR'S FORUM (VIEW ONLY)

Nu sunteti logat.
Nou pe simpatie:
oana89 la Simpatie.ro
Femeie
25 ani
Bucuresti
cauta Barbat
25 - 46 ani
Mrrrr's Forum (VIEW ONLY) / Tutoriale si Ghiduri Utile // Tutorials and useful guides / [EXCEL/PYTHON] Extract Keywords from Given Text Moderat de TRaP, TonyTzu
Autor
Mesaj Pagini: 1
TRaP
Moderator

Inregistrat: acum 8 ani
Postari: 931
I have lots of paragraphs of text and would like a few options to extract keywords from them. Even if I would have to check and edit those keyword lists afterwards, it would be far easier than having to read and manually write the keywords myself.

A. If there is a given list of keywords

Assuming I have a list of keywords I am looking for in a text, and want to see which paragraphs contain which keywords, as simple Excel formula would suffice.

I provided the desired text in cell A1, and the list of keywords in Table2[Given list]


=TEXTJOIN(", "; TRUE; FILTER(Table2[Given list]; ISNUMBER(SEARCH(Table2[Given list]; A1)); ""))


B. Without a given list of keywords - extracting substantives - 1. In Excel 365, with Python

Type =PY in any cell, and press TAB, and if PY appears in front of the fx field, then you have Python in Excel and can run the following code.
Input is done Ctrl + Enter to commit the Python code.
After input, right click the cell with the code, go to Python Output and select Excel Value to display the results.


import nltk
from nltk.corpus import stopwords
import re

# 1. Grab your data
raw_data = xl("MyData[TextColumn]")

# 2. Get the list of English filler words
stop_words = set(stopwords.words('english'))

def extract_keywords(text):
    # Handle empty, blank, or non-string cells safely
    if not isinstance(text, str) or not text.strip():
        return ""
   
    # Remove punctuation and convert to lowercase
    cleaned_text = re.sub(r'[^\w\s]', '', text).lower()
   
    # Split into individual words
    words = cleaned_text.split()
   
    # Filter out filler words and short words (like "it", "is", "by")
    keywords = [word for word in words if word not in stop_words and len(word) > 2]
   
    # Remove duplicates while keeping the original order
    unique_keywords = list(dict.fromkeys(keywords))
   
    # Join them back with commas
    return ", ".join(unique_keywords)

# 3. Handle data dynamically whether it is a DataFrame, a Series, a List, or a single string
if hasattr(raw_data, 'values'):
    # If it is a DataFrame/Table, process each cell
    result = [extract_keywords(row[0]) for row in raw_data.values]
elif isinstance(raw_data, list):
    # If it is a basic list
    result = [extract_keywords(item) for item in raw_data]
elif isinstance(raw_data, str):
    # If it's just a single text string
    result = extract_keywords(raw_data)
else:
    result = ""

# Output the final list back to Excel
result


B. Without a given list of keywords - extracting substantives - 2. In Google environment, with Python

1. Go to colab.research.google.com and sign in with any Google account.

2. Click New Notebook.

3. Copy and paste the following code into the code cell:

import pandas as pd
import spacy
from google.colab import files

# 1. Ask you to upload your Excel file
print("Please upload your Excel file:")
uploaded = files.upload()
file_name = list(uploaded.keys())[0]

# 2. Read the Excel file (assumes text is in a column named 'Text')
# Change 'Text' to match your actual column header
df = pd.read_excel(file_name)
column_to_process = 'Text'

# 3. Load the Natural Language processing model
nlp = spacy.load("en_core_web_sm")

def extract_nouns(text):
    if pd.isna(text):
        return ""
    doc = nlp(str(text))
    # Grab nouns (NOUN) and proper nouns (PROPN)
    keywords = [token.text.lower() for token in doc if token.pos_ in ["NOUN", "PROPN"]]
    return ", ".join(list(dict.fromkeys(keywords)))

# 4. Extract keywords
print("Extracting keywords...")
df['Extracted Keywords'] = df[column_to_process].apply(extract_nouns)

# 5. Download the updated Excel file back to your computer
output_file = "extracted_keywords.xlsx"
df.to_excel(output_file, index=False)
files.download(output_file)
print("Done! Check your Downloads folder.")

4. Click the Play button (Run cell) on the left of the code.

5. It will prompt you to select and upload your Excel file from your computer, extract the keywords (nouns), and automatically prompt you to save the updated file back to your computer.

Source: Gemini Flash 3.5


pus acum 3 zile
   
TRaP
Moderator

Inregistrat: acum 8 ani
Postari: 931
Google Colab python code for entire Word documents (docx format).
It generates a new Word document with the keywords instead of the original paragraphs. You can then look at the original docx and the keywords docx in parallel.


# 1. Install the python-docx library
!pip install python-docx

import docx
import spacy
from google.colab import files

# Load spaCy NLP model (English)
nlp = spacy.load("en_core_web_sm")

def extract_keywords(text):
    if not text or not text.strip():
        return ""
    doc = nlp(text)

    # Extract nouns (NOUN) and proper nouns (PROPN)
    keywords = [token.text for token in doc if token.pos_ in ["NOUN", "PROPN"]]

    # Remove duplicates preserving original reading order
    seen = set()
    unique_keywords = []
    for word in keywords:
        word_lower = word.lower()
        if word_lower not in seen and len(word) > 2:
            seen.add(word_lower)
            unique_keywords.append(word)
    return ", ".join(unique_keywords)

# Prompt to upload your original Word file
print("Please upload your Word (.docx) file:")
uploaded = files.upload()
file_name = list(uploaded.keys())[0]

# Open and process the document
doc = docx.Document(file_name)
print("Extracting keywords paragraph-by-paragraph...")

for paragraph in doc.paragraphs:
    if paragraph.text.strip():
        # Replace original paragraph text with extracted keywords
        paragraph.text = extract_keywords(paragraph.text)

# Save and download the modified document
output_filename = "keywords_" + file_name
doc.save(output_filename)
files.download(output_filename)
print("Finished! Check your browser's Downloads folder.")


pus acum 2 zile
   
TRaP
Moderator

Inregistrat: acum 8 ani
Postari: 931
Same as above for a PDF source file.
However, this one takes the PDF line by line and thus returns line-by-line keywords. So this code must be refined.


# 1. Install required libraries
!pip install python-docx pypdf

import pypdf
import docx
import spacy
from google.colab import files

# Load spaCy NLP model
nlp = spacy.load("en_core_web_sm")

def extract_keywords(text):
    if not text or not text.strip():
        return ""
    doc = nlp(text)
    # Extract nouns and proper nouns
    keywords = [token.text for token in doc if token.pos_ in ["NOUN", "PROPN"]]

    # Remove duplicates preserving order
    seen = set()
    unique_keywords = []
    for word in keywords:
        word_lower = word.lower()
        if word_lower not in seen and len(word) > 2:
            seen.add(word_lower)
            unique_keywords.append(word)
    return ", ".join(unique_keywords)

# Prompt to upload PDF file
print("Please upload your PDF file:")
uploaded = files.upload()
file_name = list(uploaded.keys())[0]

# Read PDF
print("Reading PDF...")
reader = pypdf.PdfReader(file_name)

# Create a new Word Document
output_doc = docx.Document()
output_doc.add_heading("Extracted Keywords Report", level=1)
output_doc.add_paragraph("Original Source: " + file_name)

print("Processing page by page...")
for i, page in enumerate(reader.pages):
    text = page.extract_text()
    if text and text.strip():

        output_doc.add_heading(f"Page {i+1}", level=2)

        # Split page content into lines/paragraphs
        lines = text.split("\n")
        for line in lines:
            if line.strip():
                keywords = extract_keywords(line)
                if keywords:
                    output_doc.add_paragraph(keywords)

# Save and download
output_filename = file_name.replace(".pdf", "_keywords.docx")
output_doc.save(output_filename)
files.download(output_filename)
print("Finished! Check your browser's Downloads folder.")


pus acum 2 zile
   
Pagini: 1  

Mergi la