#🔒 CLTK and Maths

5 messages · Page 1 of 1 (latest)

rustic violetBOT
#

@tender kestrel

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

tender kestrel
#
import string
from cltk import NLP 
from boltons.strutils import split_punct_ws
from collections import Counter

KOINE_FUNCTION_WORDS = {shortened for discord}
greek_text ="Ὁ Σωκράτης ἐδίδασκεν τοὺς νέους ἐν Ἀθήναις. Ἡ Ἀθηνᾶ ἐστὶ θεὰ σοφίας. Ὁ Πλάτων ἦν μαθητὴς τοῦ Σωκράτους. Οἱ Ἀθηναῖοι ἐτίμων τὴν σοφίαν καὶ τὴν δικαιοσύνην."
cltk_nlp = NLP(language="grc", suppress_banner=True)
#
def LexicalFreq(Word_List, Top_NF=None, Top_NN = None, Top_NL = 2 ,n = 2):

    Word_List = [
        word for word in Word_List
        if word.string.strip(string.punctuation) != ""
    ]


    
    Lemmas = []
    Total_Char = 0
    Word_Strings = []
    Function_Word_Freq = []
    


    for word in Word_List:
        Lemmas.append(word.lemma)
        Word_Strings.append(word.string)
        Total_Char += len(word.string)
        if word.lemma in KOINE_FUNCTION_WORDS:
            Function_Word_Freq.append(word.lemma)

            

    lemma_counts = Counter(Lemmas)

    Type_Token_Ratio = len(set(Lemmas))/len(Lemmas)
    Hapax_Legomena = (sum(1 for count in lemma_counts.values() if count == 1)/len(Lemmas))
    Avg_Word_Len = Total_Char/len(Word_List)
    Function_Word_Freq = Counter(Function_Word_Freq)

    if Top_NF:
        Function_Word_Freq = Function_Word_Freq.most_common(Top_NF)

    lemma_counts = Counter(Lemmas)
    N = sum(lemma_counts.values())  
    freq_of_freq = Counter(lemma_counts.values())

    sum_i2_vi = sum((i**2) * vi for i, vi in freq_of_freq.items())
    Yules_K= 10000 * ((sum_i2_vi - N) / (N ** 2))


    N_Grams= zip(*[Word_Strings[i:] for i in range(n)])
    N_Grams_Heat = Counter(N_Grams)
    if Top_NN:
        N_Grams_Heat=N_Grams_Heat.most_common(Top_NN)

    Lemma_Frequency = Counter(Lemmas).most_common(Top_NL)


    Result_Data = {
        'Type_Token_Ratio': Type_Token_Ratio,
        'Hapax_Legomena_Ratio': Hapax_Legomena,
        'Avg_Word_Length': Avg_Word_Len,
        "Function_Word_Frequency": Function_Word_Freq,
        "Yules_K":Yules_K,
        "N_Grams_Frequency": N_Grams_Heat,
        "Lemma_Frequency": Lemma_Frequency
    }

    return Result_Data

    


def LexicalFeatures(grcStr):
    Doc = cltk_nlp.analyze(grcStr)
    Word_List = []

    for Words in Doc.words:
        Word_List.append(Words)

    return LexicalFreq(Word_List, 3, 3, 2)
print(LexicalFeatures(greek_text))
rustic violetBOT
#
Python help channel closed using Discord native close action

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.