You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spark/Scala相似词分组统计:处理词尾s/ly的合并计数需求

Got it, let's fix this up for you! The key here is to add a word normalization step before counting—we need to strip off those s or ly suffixes to group related words correctly.

First, we'll create a helper function to handle the suffix replacement. We check for ly first (since it's a longer suffix) to avoid edge cases where a word ending with ly might mistakenly get treated as just ending with s. Then we check for the s suffix.

Here's the updated code:

# Define a function to normalize words by stripping target suffixes
def normalize_word(word):
    # Prioritize longer suffix 'ly' first
    if word.endswith('ly'):
        return word[:-2]
    # Then handle words ending with 's'
    elif word.endswith('s'):
        return word[:-1]
    # Keep the original word if no matching suffix
    else:
        return word

# Assume your input data is already an RDD (adjust if your data source is different)
data = sc.parallelize(["hi", "yes", "love", "know", "hi", "knows", "loves", "lovely"])

# Apply normalization, then perform counting
result = data.map(lambda word: (normalize_word(word), 1)) \
             .reduceByKey(lambda a, b: a + b) \
             .collect()

print(result)

Let's walk through what this does:

  • The normalize_word function adjusts each input word:
    • Turns lovely into love by removing the last two characters
    • Turns knows into know and loves into love by removing the final s
    • Leaves words like hi and yes unchanged
  • We map each normalized word to a tuple with a count of 1 (using the normalized word as the key instead of the original)
  • reduceByKey then sums up the counts for each normalized key

When you run this, you'll get the exact output you expected: [('hi', 2), ('yes', 1), ('love', 3), ('know', 2)]

内容的提问来源于stack exchange,提问作者dreddy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 06:32:42