Я пытаюсь построить положение и частоту повторения n-грамм в тексте. Идея состоит в том, чтобы идентифицировать точки в тексте, когда автор начинает повторное использование условий. Некоторые жанры должны иметь более короткий диапазон уникальности, чем другие.Q Как расположить позиции n-графа в тексте
Слово 1 ... n, размещенное на оси X. Поскольку частота появления n-грамма становится> 1, на графике появляется точка, где X - ее положение, Y - частота, а цвет - единственный n-грамм. Из приведенного ниже кода 2-граммовый «хороший спорт» будет отображаться как (7, 2, RED).
В: Как создать np.array с 1.unique n-gram, 2. частота и 3. позиция в тексте?
from sklearn.feature_extraction.text import CountVectorizer
import nltk
words = "good day, good night, good sport, good sport charlie"
clean=re.sub("[^\w\d'\s]+",'',words)
vectorizer2 = CountVectorizer(ngram_range=(2,2), tokenizer=word_tokenize, stop_words='english')
analyzer = vectorizer2.build_analyzer()
two_grams=analyzer(clean)
# Get the set of unique words.
uniques = []
for word in two_grams:
if word not in uniques:
uniques.append(word)
# Make a list of (count, unique) tuples.
counts = []
for unique in uniques:
count = 0 # Initialize the count to zero.
for word in two_grams: # Iterate over the words.
if word == unique: # Is this word equal to the current unique?
count += 1 # If so, increment the count
counts.append((count, unique))
counts.sort() # Sorting the list puts the lowest counts first.
counts.reverse() # Reverse it, putting the highest counts first.
# Print the ten words with the highest counts.
for i in range(min(10, len(counts))):
count, word = counts[i]
print('%s %d' % (word, count))
#Scatterplot
#plt.scatter(count, count, s=area, c=colors, alpha=0.5)
####plt.show()
высоко ценятся! – lrn2code