簡體   English   中英

創建一個頻率表以捕獲一定長度的字符串中的流行子字符串-Python

[英]Create a frequency table that captures popular substrings within strings of a certain length - Python

我正在嘗試對正在編譯的斯瓦希里語料庫進行頻率分析。 目前,這是我所擁有的:

import os
import sys
from collections import Counter
import re


path = 'C:\Python27\corpus\\'
cnt = Counter()
listing = os.listdir(path)
for infile in listing:
    print "Currently parsing: " + path + infile
    corpus = open(path+infile, "r")
    for lines in corpus:
        for words in lines.split(' '):
            if len(words) >= 2 and re.match("^[A-Za-z]*$", words):
                words = words.strip()
                cnt[words] += 1
    print "Completed parsing: " + path + infile
    #output = open(n + ".out", "w")
    #print "current file is: " + infile

    corpus.close()
    #output.close()
for (counter, content) in enumerate(cnt.most_common(1000)):
    print str(counter+1) + " " + str(content)

因此,該程序將遍歷給定路徑中的所有文件,讀入每個文件的文本,並顯示1000個最常用的單詞。 問題是:斯瓦希里語是一種凝集性語言,這意味着在單詞中添加了中綴,后綴和前綴,以傳達諸如時態,因果關系,虛擬語氣,介詞等內容。

因此,像“ -fanya”這樣的動詞詞根意思是“要做”,可能是nitakufanya-“我要去做你”。 結果,該頻率列表偏向於連接諸如“ for”,“ in”,“ out”之類的不使用所述中綴的詞。

是否有一種簡單的方法來查看諸如“ nitakufanya”或“ tunafanya”之類的單詞,並將“ fanya”一詞包括在總數中?

一些潛在的事情要看:

  1. 動詞詞根將在單詞的結尾
  2. 單詞開頭的主題標記可以是以下之一:'ni'(I),'u'(you),'a'(he / she),'wa'(they),'tu'(我們),“ m”(你們所有人)
  3. 主題標記后面是時態標記,它們是:“ na”(當前),“ li”(過去),“ ta”(未來),“ ji”(反身),“ nge”(有條件)

謝謝

首先進行頻率分析,而不必擔心前綴。 然后從頻率列表中固定前綴。 為此,可以根據單詞對列表進行排序,以使具有相同前綴的單詞彼此相鄰。 這將使手動修剪變得非常容易。

你可以做:

root_words = [re.sub(
    '^(ni|u|a|wa|tu|m)(na|li|ta|ji|nge)',
    '', x) for word in words]

刪除每個單詞的前綴,但是如果根單詞也以這些序列開頭,那么您將無能為力。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM