Поиск по нескольким документам для общих слов

306
ioloiol

У меня есть текст песни. (.текст)

У меня также есть текст к 50 другим песням.

Я ищу способ проанализировать / найти эти 50 текстов песен с текстами первой песни и найти, какой из 50 наиболее похож на первый (на основе общих слов / словарного запаса).

Прошу прощения за выступление непрофессионала - это не моя область знаний (!)

Любая помощь или указатели будут высоко ценится

1
Это помогло бы получить некоторый пример ввода и желаемый результат. fedorqui 9 лет назад 0
Перенесите текст в топ-лист Billboard 50. Если вы взяли текущий Billboard номер 1 и искали его текст по 50 другим песням, вы можете получить следующий результат: SORTED BY MOST SIMILAR 1) "MAROON 5 - ANIMALS", общие слова = проблема, шляпа, пальто, я, кто-то, помощь, торт, и т. д. 2) «ХУДОЖНИК - ИМЯ ПЕСНИ», общие слова = красивая, никогда, только, помощь и т. д. 3) «ХУДОЖНИК - ИМЯ ПЕСНИ», общие wrds: x, x , х, х. Идеальный желаемый результат - 50 песен, отсортированных по «сходству» с выделенными общими словами. Надеюсь, это поможет, я работаю в видео - комплексный поиск вне моей зоны комфорта! ура ioloiol 9 лет назад 0
Одна проблема заключается в том, что вам нужно игнорировать список слов, таких как «The», «La La La» и т. Д. Jack 8 лет назад 0

1 ответ на вопрос

0
Jack

Here's my solution, I presumed that you only care how many words match rather how many times they match (E.g. 'Baby' 5 times in both songs is worth 5x as many 'points).

First:

cat songname.txt | sed ':a;N;$!ba;s/\n/ /g' | tr -cd '[[:alnum:]]\ ' | sed 's#\ \ #\ #g' | sed 's#\ #\n#g' | sort | uniq -i > songnamewords.txt 

This turns all newlines into spaces, removes all non-alphanumeric characters (Commas), removes any double spaces, puts every word on a seperate line, sorts them and removes duplicate lines.

You need to do this to all the songs you want to compare, then secondly:

cat songname1words.txt songname2words.txt | sort | uniq -d | wc -l 

This will give you a number of how many words matched.

I tried a few examples:

Maroon 5's Animals and Justin Bieber's Baby share 29 words.

Maroon 5's Animals and Opeth's Grand Conjuration share 10 words.

These are the kind of results you'd expect.

Also, here's how you would compare it against all other lyrics files:

a="songname1words.txt" && for f in *; do if [[ "$f" != "$a" ]]; then printf $(cat "$a" "$f" | sort | uniq -d | wc -l) && echo " - $f" | sort; fi; done 

Where 'songname1words.txt' is the filename you want to compare them all against.

This compares all other text files against this one, skipping comparing itself to itself, it then sorts them all by score so that the number 1 match is at the top.

It gives output like this:

29 - bieberwords.txt

10 - opethwords.txt