[split] (lack of) word groups
#18
(01-07-2019, 06:27 PM)Koen G wrote: You are not allowed to view links. Register or Login to view.I guess essentially I'd need a code that does TTR for x-word chunks.

Your wish is my command. Smile

I just concatenated the x last words with a "." separator. The third and last parameter is x.

Code:
import java.io.*; import java.util.*; public class xMATTR {     static int window;     static List<String> tokens = new ArrayList<String>();     static HashMap<String, Integer> types;     static int nTokens;     static int sumTypes;     static int nLastWords;     static int nLastWordsMinusOne;     static String[] lastWords;     public static void main(String args[])     {         try         {             if (args.length == 3)             {                 nLastWords = Integer.parseInt(args[2]);                 nLastWordsMinusOne = nLastWords - 1;                 lastWords = new String[nLastWords];                 window = Integer.parseInt(args[1]);                 processFileOrFolder(new File(args[0]));             }         }         catch (Exception e)         {             e.printStackTrace();         }     }     public static void processFileOrFolder(File f) throws Exception     {         if (f.exists())         {             if (f.isDirectory())             {                 String[] children = f.list();                 if (children != null)                 {                     for (String filename : children)                     {                         String pathname = f.getPath()+File.separator+filename;                         processFileOrFolder(new File(pathname));                     }                 }             }             else             {                 loadFile(f);                 System.out.println(f.getPath()+"\t"+computeMATTR());             }         }     }     static void loadFile(File f) throws Exception     {         tokens.clear();         InputStreamReader isreader = new InputStreamReader(new FileInputStream(f), "UTF-8");         BufferedReader reader = new BufferedReader(isreader);         String line;         while((line = reader.readLine()) != null)         {             if (!line.isEmpty())             {                 String[] words = line.toLowerCase().replaceAll("\\p{P}", " ").split("[0-9 \t]+");                 for (String word: words)                 {                     if (!word.isEmpty())                     {                         System.arraycopy(lastWords, 1, lastWords, 0, nLastWordsMinusOne);                         lastWords[nLastWordsMinusOne] = word;                         if (lastWords[0] != null)                         {                             String lastWordsAsOne = "";                             for (String lastWord: lastWords)                                 lastWordsAsOne += lastWord + ".";                             tokens.add(lastWordsAsOne);                         }                     }                 }             }         }         reader.close();     }     static double computeMATTR()     {         if (tokens.size() < window)             return 0;         nTokens = 0;         types = new HashMap<String, Integer>(window);         for (String token: tokens)             addToken(token);         double m = (double)sumTypes / window / (tokens.size() - window + 1);         return m;     }     static void addToken(String word)     {         incType(word);         nTokens++;         if (nTokens > window)         {             decType(tokens.get(nTokens - window - 1));             sumTypes += types.size();         }         else if (nTokens == window)         {             sumTypes = types.size();         }     }     static void incType(String word)     {         Integer count = types.get(word);         if (count == null)             types.put(word, 1);         else             types.put(word, count + 1);     }     static void decType(String word)     {         Integer count = types.get(word);         if (count == 1)             types.remove(word);         else             types.put(word, count - 1);     } }
[+] 1 user says Thank You to nablator for this post
Reply


Messages In This Thread
[split] (lack of) word groups - by -JKP- - 01-07-2019, 12:07 PM
RE: [split] (lack of) word groups - by Koen G - 03-07-2019, 10:54 AM
RE: [split] (lack of) word groups - by nablator - 03-07-2019, 11:11 AM
RE: [split] (lack of) word groups - by Koen G - 03-07-2019, 03:16 PM
RE: [split] (lack of) word groups - by Koen G - 03-07-2019, 04:15 PM
RE: [split] (lack of) word groups - by Torsten - 03-07-2019, 06:00 PM
RE: [split] (lack of) word groups - by Koen G - 03-07-2019, 04:49 PM
RE: [split] (lack of) word groups - by Koen G - 03-07-2019, 06:16 PM
RE: [split] (lack of) word groups - by Torsten - 03-07-2019, 07:44 PM
RE: [split] (lack of) word groups - by Koen G - 03-07-2019, 08:00 PM
RE: [split] (lack of) word groups - by Koen G - 03-07-2019, 08:49 PM
RE: [split] (lack of) word groups - by Torsten - 03-07-2019, 09:14 PM
RE: [split] (lack of) word groups - by Koen G - 03-07-2019, 09:27 PM
[split] (lack of) word groups - by nablator - 01-07-2019, 12:12 PM
RE: [split] (lack of) word groups - by Koen G - 01-07-2019, 05:13 PM
RE: [split] (lack of) word groups - by Koen G - 01-07-2019, 05:26 PM
RE: [split] (lack of) word groups - by Torsten - 01-07-2019, 05:35 PM
RE: [split] (lack of) word groups - by Koen G - 01-07-2019, 05:57 PM
RE: [split] (lack of) word groups - by Torsten - 01-07-2019, 06:04 PM
RE: [split] (lack of) word groups - by MarcoP - 01-07-2019, 06:13 PM
RE: [split] (lack of) word groups - by Koen G - 01-07-2019, 06:27 PM
RE: [split] (lack of) word groups - by Torsten - 01-07-2019, 07:06 PM
RE: [split] (lack of) word groups - by Koen G - 01-07-2019, 07:12 PM
RE: [split] (lack of) word groups - by Torsten - 01-07-2019, 07:25 PM
RE: [split] (lack of) word groups - by nablator - 01-07-2019, 09:03 PM
RE: [split] (lack of) word groups - by Koen G - 02-07-2019, 09:16 AM
RE: [split] (lack of) word groups - by Koen G - 02-07-2019, 10:12 AM
RE: [split] (lack of) word groups - by nablator - 02-07-2019, 11:42 AM
RE: [split] (lack of) word groups - by Koen G - 02-07-2019, 11:55 AM
RE: [split] (lack of) word groups - by nablator - 02-07-2019, 12:08 PM
RE: [split] (lack of) word groups - by Koen G - 02-07-2019, 10:06 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] (lack of) scribal mistakes / corrections -JKP- 88 73,962 29-03-2026, 10:14 PM
Last Post: Torsten
  [split] Word length autocorrelation ReneZ 22 10,908 06-09-2025, 11:13 AM
Last Post: Jorge_Stolfi
  [split] Percentage of word types that occur more than once bi3mw 37 34,815 25-06-2020, 05:41 PM
Last Post: Torsten
  [split] Line padding, layout, Currier's "groups of letters"... Torsten 37 38,671 16-03-2020, 06:33 AM
Last Post: Wladimir D
  [split] recurring word sequences doranchak 7 11,772 06-04-2018, 12:16 PM
Last Post: MarcoP

Forum Jump:


Users browsing this thread: 1 Guest(s)