Python WordCloud ไม่ได้ลบ Stopwords

ฉันกำลังพยายามสร้าง Wordcloud ที่ดึงคำจากรายละเอียดงานโดยอัตโนมัติและสร้าง wordcloud หากคุณมี stopwords=None ก็ควรจะลบรายการคำหยุดที่รู้จักของ wordcloud แต่โปรแกรมของฉันไม่ได้ลบ ฉันเชื่อว่ามันอาจจะเกี่ยวข้องกับการดึงลักษณะงานด้วยซุปที่สวยงาม ฉันต้องการความช่วยเหลือในการดึงคำให้แตกต่างออกไปด้วย beautifulsoup หรือฉันใช้คำหยุดไม่ถูกต้อง

import requests
# pip install bs4
from bs4 import BeautifulSoup
# pip install wordcloud
from wordcloud import WordCloud
import matplotlib.pyplot as plt

# Goes to a job description
url = "https://career.benteler.jobs/job/Paderborn-Head-of-Finance-&-Controlling-North-America-NW/604307901/?locale=en_US"
html_text = requests.get(url).text
soup = BeautifulSoup(html_text, 'html.parser')

# Goes through all the words in the beautiful soup text
combinedWords = ''

for words in soup.find_all('span'):
    separatedWords = words.text.split(' ')
    combinedWords += " ".join(separatedWords) + ' '

# creates wordcloud
resumeCloud = WordCloud(stopwords=None, background_color='white', max_words=75, max_font_size=75, random_state=1).generate(combinedWords)

plt.figure(figsize=(8, 4))
plt.imshow(resumeCloud)
plt.axis('off')
plt.show()

person Brandon Jacobson    schedule 01.07.2020    source แหล่งที่มา
comment
สิ่งนี้ตอบคำถามของคุณหรือไม่? เหตุใดจึง หยุดคำที่ไม่ถูกแยกออกจากคำว่า cloud เมื่อใช้ไลบรารี wordcloud ของ Python?   -  person barny    schedule 01.07.2020
comment
ซ้ำกับ stackoverflow.com/questions/61953788/   -  person barny    schedule 01.07.2020
comment
@barny อันที่สองช่วยได้อย่างแน่นอน การตั้งค่า collocations=False ทำงาน ขอบคุณ.   -  person Brandon Jacobson    schedule 01.07.2020


คำตอบ (1)


ปัญหาหลักคือโค้ดทั้งหมดอยู่ในบล็อกเดียว ลองแยกตรรกะออกเป็นวิธีการและทดสอบแต่ละบิตแยกกัน คำขอ ไม่ตรวจสอบข้อผิดพลาด (เช่น เซิร์ฟเวอร์อาจไม่พร้อมใช้งาน แต่ไม่น่าจะเป็นปัญหาในขณะนี้)

BeautifulSoup กำลังแยกองค์ประกอบ span ทั้งหมดบนเพจ หมายความว่าจะรวมเมนู/ส่วนท้ายไว้ด้วย หากคุณต้องการคำอธิบายงาน คุณอาจต้องเลือกช่วงที่มีชื่อคลาส jobdescription หลังจากนั้นคุณสามารถเรียก ข้อความ เพื่อลบ html ฉันไม่แน่ใจว่าคุณจำเป็นต้องลบสิ่งอื่นๆ เช่น เครื่องหมายจุลภาคและจุดเต็มหรือไม่

ฉันไม่มีประสบการณ์กับ Word Cloud เลย อย่างไรก็ตามในโค้ดด้านล่างมันส่งคืนสิ่งที่ดูเหมือนผลลัพธ์

import requests
from bs4 import BeautifulSoup
from wordcloud import WordCloud
import matplotlib.pyplot as plt

def get_job_html(url):
    response = requests.get(url)
    response.raise_for_status() # check for 4xx & 5xx errors
    return response.text

def extract_combined_words(html):
    soup = BeautifulSoup(html, 'html.parser')
    job_description = soup.find("span", {"class": "jobdescription"}).text.replace('\n', ' ') # Target span with class jobdescription. text will strip out html.
    print(job_description) # TODO - Check this is the results you expect?
    return job_description

def create_resume_cloud(combinedWords):
    return WordCloud(stopwords=None, background_color='white', max_words=75, max_font_size=75, random_state=1).generate(combinedWords)

def plot_resume_cloud(resumeCloud):
    plt.figure(figsize=(8, 4))
    plt.imshow(resumeCloud)
    plt.axis('off')
    plt.show()

def run(url):
    html = get_job_html(url)
    combinedWords = extract_combined_words(html)
    resumeCloud = create_resume_cloud(combinedWords)
    plt = plot_resume_cloud(resumeCloud)
    return plt # TODO - not sure how the results gets consumed

if __name__ == '__main__':
    run("https://career.benteler.jobs/job/Paderborn-Head-of-Finance-&-Controlling-North-America-NW/604307901/?locale=en_US")
person Greg    schedule 01.07.2020
comment
นี่คือสิ่งที่ฉันกำลังมองหาเพื่อล้างข้อมูล นอกจากนี้ ยังมีคนอื่นมอบโซลูชัน WordCloud ให้ฉันด้วย ขอบคุณ!!!!! - person Brandon Jacobson; 01.07.2020